Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Paysafe Interns Valentin-Kaloyan
Java Functional Programming
aleksandar-stanchev-java-functional-programming-2023
Commits
c5ef2375
Commit
c5ef2375
authored
1 year ago
by
Aleksandar Stanchev
Browse files
Options
Download
Email Patches
Plain Diff
Done
parent
fdf65ae4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Task1.java
+41
-0
Task1.java
Task2.java
+22
-0
Task2.java
Task3.java
+26
-0
Task3.java
with
89 additions
and
0 deletions
+89
-0
Task1.java
0 → 100644
+
41
-
0
View file @
c5ef2375
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.stream.Collectors
;
public
class
Task1
{
public
static
void
main
(
String
[]
args
)
{
//=============input reading starts here==============
List
<
Integer
>
inputList
=
new
ArrayList
<>();
Integer
divider
=
Integer
.
parseInt
(
args
[
args
.
length
-
1
]);
for
(
int
i
=
0
;
i
<
args
.
length
-
1
;
i
++)
{
inputList
.
add
(
Integer
.
parseInt
(
args
[
i
]));
}
//==============input reading ends here================
List
<
Integer
>
output
=
task1
(
inputList
,
divider
);
///////task1 method is executed here
System
.
out
.
println
(
output
);
}
public
static
List
<
Integer
>
task1
(
List
<
Integer
>
inputList
,
Integer
divider
)
{
/* this also works
//reverse the list
Collections.reverse(inputList);
// then filter the numbers whose remainder is 0
List<Integer> listToReturn = inputList.stream()
.filter( (n) -> n % divider == 0 )
.collect(Collectors.toList());
*/
List
<
Integer
>
listToReturn
=
new
LinkedList
<>();
inputList
.
stream
()
.
collect
(
Collectors
.
toCollection
(
LinkedList:
:
new
))
//transform the collection to a Linked List
.
descendingIterator
()
// iterate through the elements in reversed order
.
forEachRemaining
(
// for each integer in the collection
integer
->
{
// if the remainder is 0, add it to a new list
if
(
integer
%
divider
==
0
)
listToReturn
.
add
(
integer
);
}
);
return
listToReturn
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Task2.java
0 → 100644
+
22
-
0
View file @
c5ef2375
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
public
class
Task2
{
public
static
void
main
(
String
[]
args
)
{
//=============input reading starts here==============
Integer
lengthLimit
=
Integer
.
parseInt
(
args
[
args
.
length
-
1
]);
List
<
String
>
inputList
=
new
ArrayList
<>(
Arrays
.
asList
(
args
).
subList
(
0
,
args
.
length
-
1
));
//==============input reading ends here================
List
<
String
>
output
=
task2
(
inputList
,
lengthLimit
);
System
.
out
.
println
(
output
);
///////task2 method is executed here
}
public
static
List
<
String
>
task2
(
List
<
String
>
inputList
,
Integer
lengthLimit
)
{
List
<
String
>
listToReturn
=
inputList
.
stream
()
.
filter
(
// filter out all string with length larger than the requested limit
(
stringArg
)
->
stringArg
.
length
()<=
lengthLimit
).
toList
();
return
listToReturn
;
}
}
This diff is collapsed.
Click to expand it.
Task3.java
0 → 100644
+
26
-
0
View file @
c5ef2375
import
java.util.concurrent.atomic.AtomicBoolean
;
import
java.util.stream.IntStream
;
public
class
Task3
{
public
static
void
main
(
String
[]
args
)
{
String
output
=
task3
(
Integer
.
parseInt
(
args
[
0
]));
System
.
out
.
println
(
output
);
///////task3 method is executed here
}
private
static
String
task3
(
int
number
)
{
AtomicBoolean
isPrime
=
new
AtomicBoolean
(
true
);
//just in case someone is trying to check numbers less than or equal to one
if
(
number
<=
1
)
{
isPrime
.
set
(
false
);
}
else
{
IntStream
.
range
(
2
,
number
/
2
)
// for all numbers between 2 and number/2
.
forEach
((
divider
)
->
{
// if the remainder is 0, then our number is not prime
if
(
number
%
divider
==
0
)
isPrime
.
set
(
false
);
});
}
return
"Is "
+
number
+
" a prime number? - "
+
isPrime
.
get
();
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help