Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions People.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Sarah 15
Philip 21
Beth 23
Simon 24
Nina 23
Allan 28
Leonard 18
Barbara 26
Penelope 18
Albert 31
Lucy 42
Charles 26
Ella 24
Louis 32
Lisa 17
Frank 18
Amy 26
Nathan 33
38 changes: 38 additions & 0 deletions src/main/java/logics/java8/model/Person1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.java8.practice;

public class Person1 {

private String name;

private int age;

public Person1() {

}

public Person1(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String toString() {
return "Person [" + name + ", " + age + "]";
}

}
76 changes: 76 additions & 0 deletions src/main/java/logics/java8/test/CollectionExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.java8.practice;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CollectionExample {

public static void main(String[] args) throws IOException {

List<Person1> persons = new ArrayList<Person1>();

Files
.lines(Paths.get(System.getProperty("user.dir") + "/People.txt"))
.collect(Collectors.toList())
.stream()
.map(line -> {
String[] s = line.split(" ");
Person1 p = new Person1(s[0].trim(), Integer.parseInt(s[1]));
persons.add(p);
return p;
})
.forEach(System.out::println);

Stream<Person1> stream1 = persons.stream();

Optional<Person1> opt = stream1.filter(p -> p.getAge() >= 20)
.min(Comparator.comparing(Person1::getAge));

System.out.println("The yongest person in the group whose age is greater than 20 is " + opt.get().getName() + " and person's age is " + opt.get().getAge());

Optional<Person1> opt1 = persons.stream().max(Comparator.comparing(Person1::getAge));

System.out.println("The oldest person in the group is " + opt1.get().getName() + " and person's age is " + opt1.get().getAge());

Map<Integer, List<Person1>> map = persons.stream()
.collect(Collectors.groupingBy(Person1::getAge));

System.out.println(map);

Map<Integer, Long> map1 = persons.stream()
.collect(Collectors.groupingBy(Person1::getAge, Collectors.counting()));

System.out.println(map1);

Map<Integer, Set<String>> map2 = persons.stream()
.collect(Collectors.groupingBy(Person1::getAge, Collectors.mapping(Person1::getName, Collectors.toSet())));

System.out.println(map2);

Map<Integer, String> map3 = persons.stream()
.collect(Collectors.groupingBy(Person1::getAge, Collectors.mapping(Person1::getName, Collectors.joining(", "))));

System.out.println(map3);

Map<String, String> map4 = persons.stream()
.collect(Collectors.groupingBy(Person1::getName, Collectors.mapping(p -> String.valueOf(p.getAge()), Collectors.joining())));

System.out.println(map4);

map4.forEach((k,v) -> {
String format = "%-30s%s%n";
System.out.printf(format, "Person = " + k, "Age = " + v);
});

}

}
38 changes: 38 additions & 0 deletions src/main/java/logics/java8/test/FlatMapExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.java8.practice;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

public class FlatMapExample {

public static void main(String... args) {

List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
List<Integer> list2 = Arrays.asList(2, 4, 6);
List<Integer> list3 = Arrays.asList(3, 5, 7);

List<List<Integer>> list = Arrays.asList(list1, list2, list3);

System.out.println("The list is:\n" + list);

//Fuction for normal map
Function<List<?>, Integer> size = List::size; // same as l -> l.size()

//Function for flatMap
Function<List<Integer>, Stream<Integer>> flatMap = List::stream;

System.out.println("Size of each list:");
list.stream()
.map(size)
.forEach(System.out::println);

System.out.println("Content of each list:");
list.stream()
.flatMap(flatMap)
.forEach(System.out::println);

}

}
111 changes: 111 additions & 0 deletions src/main/java/logics/java8/test/StreamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.learning.stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;

public class StreamTest {

public static void main(String[] args) {

// Create Stream using of()
@SuppressWarnings("unused")
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4);
@SuppressWarnings("unused")
Stream<Integer> stream2 = Stream.of(new Integer[] { 1, 2, 3, 4 });

// Create sequential and parallel streams
List<Integer> myList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Stream<Integer> sequentialStream = myList.stream();
System.out.print("The sequential stream is: ");
sequentialStream.forEach(s -> System.out.print(s + " "));

Stream<Integer> parallelStream = myList.parallelStream();
System.out.print("\nThe parallel stream is: ");
parallelStream.forEach(s -> System.out.print(s + " "));

// Create stream using generate and iterate
// Stream<String> stream3 = Stream.generate(() -> { return "abc"; });
// Creates a infinite sequential stream
// Stream<String> stream4 = Stream.iterate("abc", i -> i); Creates a
// infinite sequential stream

LongStream ls = Arrays.stream(new long[] { 1, 2, 3, 4 });
System.out.print("\nThe Long stream is: ");
ls.forEach(s -> System.out.print(s + " "));

IntStream is = "abc".chars();
System.out.print("\nThe Integer stream is: ");
is.forEach(s -> System.out.print(s + " "));

// Collect the stream to a collection
Stream<Integer> intStream = Stream.of(1, 2, 3, 4);
List<Integer> intList = intStream.collect(Collectors.toList());
System.out.println("\nThe list using collect is: " + intList);
intStream = Stream.of(1, 2, 3, 4);
Map<Integer, Integer> intMap = intStream.collect(Collectors.toMap(i -> i, i -> i * i));
System.out.println("The map using collect is: " + intMap);

Stream<Integer> intStream1 = Stream.of(1, 2, 3, 4);
Integer[] intArray = intStream1.toArray(Integer[]::new);
System.out.println("The integer array is: " + Arrays.toString(intArray));

// Filter example
sequentialStream = myList.stream();
Stream<Integer> highNums = sequentialStream.filter(p -> p > 5);
System.out.print("The numbers greater than 5 are: ");
highNums.forEach(s -> System.out.print(s + " "));

// Map example
Stream<String> names = Stream.of("aBc", "d", "ef");
System.out.print("\nThe stream in Upper Case is: ");
names.map(String::toUpperCase).forEach(s -> System.out.print(s + " "));

// Sort example
Stream<String> names2 = Stream.of("aBc", "d", "ef", "123456");
System.out.print("\nThe sorted stream in reverse order is: ");
names2.sorted(Comparator.reverseOrder()).forEach(s -> System.out.print(s + " "));
names2 = Stream.of("aBc", "d", "ef", "123456");
System.out.print("\nThe sorted stream is: ");
names2.sorted().forEach(s -> System.out.print(s + " "));

// FlatMap example
Stream<List<String>> namesOriginalList = Stream.of(Arrays.asList("Sujay", "Sawant"),
Arrays.asList("Vilas", "Sawant"), Arrays.asList("Supriya", "Sawant"));
Stream<String> flatStream = namesOriginalList.flatMap(s -> s.stream());
System.out.print("\nAll the elements in the list are: ");
flatStream.forEach(s -> System.out.print(s + " "));

// Reduce example
Stream<Integer> numbers = Stream.of(1, 2, 3, 4, 5);
Optional<Integer> intOptional = numbers.reduce(Math::multiplyExact);
intOptional.ifPresent(s -> System.out.print("\nThe multiplication of all the elements is: " + s));

// Count example
Stream<Integer> numbers1 = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println("\nThe total number of elements are " + numbers1.count());

// Match example
Stream<Integer> numbers2 = Stream.of(1, 2, 3, 4, 5);
System.out.println("Stream contains 4? Answer: " + numbers2.anyMatch(n -> n == 4));

Stream<Integer> numbers3 = Stream.of(1, 2, 3, 4, 5);
System.out.println("Stream does not contains 4? Answer: " + numbers3.noneMatch(n -> n == 4));

Stream<Integer> numbers4 = Stream.of(1, 2, 3, 4, 5);
System.out.println("Stream contains all elements less than 4? Answer: " + numbers4.allMatch(n -> n < 4));

//find first example
Stream<String> names3 = Stream.of("Sujay", "Vilas", "Basker", "Arjun", "Stuti");
Optional<String> firstNameWithA = names3.filter(name -> name.startsWith("P")).findFirst();
System.out.println("The first Name with P is " + firstNameWithA.orElse("not there"));

}

}