Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
java-8-stream-api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Muhammad Abdul Qadeer Farooqui
java-8-stream-api
Commits
53efd90e
Commit
53efd90e
authored
Aug 25, 2022
by
Muhammad Abdul Qadeer Farooqui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more examples
parent
f008ace6
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
119 additions
and
22 deletions
+119
-22
StreamImpl.java
src/main/java/StreamImpl.java
+107
-22
Testing.java
src/main/java/Testing.java
+12
-0
StreamImpl$MathOperations.class
target/classes/StreamImpl$MathOperations.class
+0
-0
StreamImpl.class
target/classes/StreamImpl.class
+0
-0
Testing.class
target/classes/Testing.class
+0
-0
No files found.
src/main/java/StreamImpl.java
View file @
53efd90e
...
...
@@ -15,57 +15,142 @@ Terminal Operations:
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.stream.Collectors
;
import
java.util.stream.IntStream
;
public
class
StreamImpl
{
public
static
void
main
(
String
[]
args
)
{
//
filter() method.
//
Example 1
//
////
filter() method.
////
Example 1
// int[] numbers = {1,2,3,4,5,6};
//
primitive to wrapper type is boxing.
//
wrapper to primitive is unboxing.
////
primitive to wrapper type is boxing.
////
wrapper to primitive is unboxing.
// Arrays.stream(numbers).boxed().filter(x -> x != 3).forEach(System.out::println);
//
// int[] myNumbers = IntStream.range(1,9).filter(x -> x != 3).toArray();
// System.out.println(Arrays.toString(myNumbers));
//
// // Example 2 Convert an array of string type numbers to int type.
// String[] stringNumbers = {"1","2","3","4","5","6"};
// int[] intNumbers = Arrays.stream(stringNumbers).mapToInt(Integer::parseInt).toArray();
// System.out.println(Arrays.toString(intNumbers));
//
//
// // Example 3 Convert array of primitive integers to List of Integers.
// int[] primitiveNumbers = {1,2,3,4,5,6};
// List<Integer> wrapperIntegers = Arrays.stream(primitiveNumbers).boxed().collect(Collectors.toList());
// System.out.println(wrapperIntegers);
//
// // Example 4 Mapping an array of string, replacing an element with its integer length.
// String[] words = {"Qadeer", "Naveed", "Adam", "Sam"};
// int[] wordLength = Arrays.stream(words).mapToInt(String::length).toArray();
// System.out.println(Arrays.toString(wordLength));
//
// // Example 5 Filter elements greater than a particular length.
// String[] word
s
= {"Qadeer", "Naveed", "Adam", "Sam"};
// String[] word
1
= {"Qadeer", "Naveed", "Adam", "Sam"};
// Object[] filteredWords = Arrays.stream(words).filter(s -> s.length() > 5).toArray();
// System.out.println(Arrays.toString(filteredWords));
//
// // Example 6 Convert array of primitive type to a List of Wrapper type.
// int[] array = {1,2,3,4,5,6};
// List<Integer> integerStream = IntStream.of(array).boxed().collect(Collectors.toList());
// System.out.println(integerStream);
// // Example 7
//
// // Example 7
list to array
// List<Integer> number = Arrays.asList(1,2,3,4,5);
// int[] newNum = number.stream().mapToInt(Integer::intValue).toArray();
// System.out.println(Arrays.toString(newNum));
// // Example 8
// String[] words = {"Qadeer", "Naveed", "Bilal", "Ben", "Qabeer", "Qad", "steer", "butter"};
// Object[] filteredWords = Arrays.stream(words).filter(s -> s.startsWith("Qa")).filter(s -> s.endsWith("er")).toArray();
//
// // Example 8
using filter() method
// String[] words
s
= {"Qadeer", "Naveed", "Bilal", "Ben", "Qabeer", "Qad", "steer", "butter"};
// Object[] filteredWords
s
= Arrays.stream(words).filter(s -> s.startsWith("Qa")).filter(s -> s.endsWith("er")).toArray();
// System.out.println(Arrays.toString(filteredWords));
//
// // Example 9 Count the number of elements in an array.
// int[] num = {1,2,3,4,5,6};
// int count = (int) Arrays.stream(num).count();
// System.out.println(count);
//
// // Example 10 Count all even numbers
// int[] nums = {1,2,5,6,7,8,9,1,2,4,100,200,231,432,345,211,223};
// int counts = (int) Arrays.stream(nums).filter(x -> x % 2 == 0).count();
// System.out.println(counts);
// // Example 11 Count all odd numbers
// int[] nums = {1,2,5,6,7,8,9,1,2,4,100,200,231,432,345,211,223};
// int oddCount = (int) Arrays.stream(nums).filter(x -> x % 2 !=0).count();
// System.out.println(oddCount);
// // Example 12 Debug a stream
// Arrays.stream(new int[]{5,4,3,1,2})
// .boxed()
// .sorted()
// .map(x -> x * 2)
// .forEach(System.out::println);
// String[] argss = {"20", "20"};
// IntStream.iterate(1, n -> n + 1)
// .skip(Integer.parseInt(argss[0]))
// .limit(Integer.parseInt(argss[1]))
// .filter(x -> x % 2 == 0)
// .forEach(System.out::println);
// // Example 13
// Map<String, String> books = new HashMap<>();
// books.put(
// "978-0201633610", "Design patterns : elements of reusable object-oriented software");
// books.put(
// "978-1617291999", "Java 8 in Action: Lambdas, Streams, and functional-style programming");
// books.put("978-0134685991", "Effective Java");
//
// Optional<String> isbn = books.entrySet().stream()
// .filter(e -> "Effective Java".equals(e.getValue()))
// .map(Map.Entry::getKey)
// .findFirst();
//
// isbn.ifPresent(System.out::println);
//
// Optional<String> secondIsbn = books.entrySet().stream()
// .filter(e -> "This does not exist in the map".equals(e.getValue()))
// .map(Map.Entry::getKey).findFirst();
// secondIsbn.ifPresent(System.out::println);
//
// List<Long> thirdIsbn = books.keySet().stream()
// .filter(s -> s.startsWith("978"))
// .map(s -> s.replace("-", ""))
// .mapToLong(Long::parseLong)
// .boxed()
// .collect(Collectors.toList());
// System.out.println(thirdIsbn);
//
// List<Long> isbn = books.keySet().stream()
// .filter(s -> s.startsWith("978"))
// .map(s -> s.replace("-", ""))
// .mapToLong(Long::parseLong)
// .boxed()
// .collect(Collectors.toList());
// // Example 14
// // 0 + 1 * 2
// // 2 + 2 * 2
// // 6 + 3 * 2
// // 12 + 4 * 2
// // 20
// // a = running sum
// // b = numbers[i]
// // reduce(initial value of a, (a, b) -> {logic})
// List<Integer> numbers = Arrays.asList(1,2,3,4);
// Integer product = numbers.stream().reduce(0, (a, b) -> {
// return a + (b * 2);
// });
// System.out.println(product);
// Example 15
List
<
Integer
>
numbers
=
Arrays
.
asList
(-
1
,
2
,
3
,
4
,
41
,
12
,
32
,
14
,
51
,
16
);
Integer
product
=
numbers
.
stream
().
reduce
(
Integer
.
MIN_VALUE
,
(
a
,
b
)
->
a
>
b
?
a
:
b
);
System
.
out
.
println
(
product
);
}
public
static
class
MathOperations
{
public
static
int
add
(
int
a
,
int
b
)
{
return
a
+
b
;
}
}
}
src/main/java/Testing.java
0 → 100644
View file @
53efd90e
import
java.util.Arrays
;
import
java.util.concurrent.atomic.AtomicInteger
;
public
class
Testing
{
public
static
void
main
(
String
[]
args
)
{
// AtomicInteger sums = new AtomicInteger();
//int sums = 0;
int
[]
numbers
=
{
1
,
2
,
3
,
4
,
5
,
6
};
int
sums
=
Arrays
.
stream
(
numbers
).
reduce
(
0
,
Integer:
:
sum
);
System
.
out
.
println
(
sums
);
}
}
target/classes/StreamImpl$MathOperations.class
0 → 100644
View file @
53efd90e
File added
target/classes/StreamImpl.class
View file @
53efd90e
No preview for this file type
target/classes/Testing.class
0 → 100644
View file @
53efd90e
File added
Write
Preview
Markdown
is supported
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