diff --git a/People.txt b/People.txt new file mode 100644 index 0000000..34177ca --- /dev/null +++ b/People.txt @@ -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 \ No newline at end of file diff --git a/src/main/java/logics/java8/model/Person1.java b/src/main/java/logics/java8/model/Person1.java new file mode 100644 index 0000000..05f0363 --- /dev/null +++ b/src/main/java/logics/java8/model/Person1.java @@ -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 + "]"; + } + +} diff --git a/src/main/java/logics/java8/test/CollectionExample.java b/src/main/java/logics/java8/test/CollectionExample.java new file mode 100644 index 0000000..a1d5312 --- /dev/null +++ b/src/main/java/logics/java8/test/CollectionExample.java @@ -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 persons = new ArrayList(); + + 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 stream1 = persons.stream(); + + Optional 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 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> map = persons.stream() + .collect(Collectors.groupingBy(Person1::getAge)); + + System.out.println(map); + + Map map1 = persons.stream() + .collect(Collectors.groupingBy(Person1::getAge, Collectors.counting())); + + System.out.println(map1); + + Map> map2 = persons.stream() + .collect(Collectors.groupingBy(Person1::getAge, Collectors.mapping(Person1::getName, Collectors.toSet()))); + + System.out.println(map2); + + Map map3 = persons.stream() + .collect(Collectors.groupingBy(Person1::getAge, Collectors.mapping(Person1::getName, Collectors.joining(", ")))); + + System.out.println(map3); + + Map 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); + }); + + } + +} diff --git a/src/main/java/logics/java8/test/FlatMapExample.java b/src/main/java/logics/java8/test/FlatMapExample.java new file mode 100644 index 0000000..afb1522 --- /dev/null +++ b/src/main/java/logics/java8/test/FlatMapExample.java @@ -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 list1 = Arrays.asList(1, 2, 3, 4, 5, 6, 7); + List list2 = Arrays.asList(2, 4, 6); + List list3 = Arrays.asList(3, 5, 7); + + List> list = Arrays.asList(list1, list2, list3); + + System.out.println("The list is:\n" + list); + + //Fuction for normal map + Function, Integer> size = List::size; // same as l -> l.size() + + //Function for flatMap + Function, Stream> 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); + + } + +} diff --git a/src/main/java/logics/java8/test/StreamTest.java b/src/main/java/logics/java8/test/StreamTest.java new file mode 100644 index 0000000..3d1e45d --- /dev/null +++ b/src/main/java/logics/java8/test/StreamTest.java @@ -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 stream1 = Stream.of(1, 2, 3, 4); + @SuppressWarnings("unused") + Stream stream2 = Stream.of(new Integer[] { 1, 2, 3, 4 }); + + // Create sequential and parallel streams + List myList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + Stream sequentialStream = myList.stream(); + System.out.print("The sequential stream is: "); + sequentialStream.forEach(s -> System.out.print(s + " ")); + + Stream parallelStream = myList.parallelStream(); + System.out.print("\nThe parallel stream is: "); + parallelStream.forEach(s -> System.out.print(s + " ")); + + // Create stream using generate and iterate + // Stream stream3 = Stream.generate(() -> { return "abc"; }); + // Creates a infinite sequential stream + // Stream 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 intStream = Stream.of(1, 2, 3, 4); + List intList = intStream.collect(Collectors.toList()); + System.out.println("\nThe list using collect is: " + intList); + intStream = Stream.of(1, 2, 3, 4); + Map intMap = intStream.collect(Collectors.toMap(i -> i, i -> i * i)); + System.out.println("The map using collect is: " + intMap); + + Stream 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 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 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 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> namesOriginalList = Stream.of(Arrays.asList("Sujay", "Sawant"), + Arrays.asList("Vilas", "Sawant"), Arrays.asList("Supriya", "Sawant")); + Stream 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 numbers = Stream.of(1, 2, 3, 4, 5); + Optional intOptional = numbers.reduce(Math::multiplyExact); + intOptional.ifPresent(s -> System.out.print("\nThe multiplication of all the elements is: " + s)); + + // Count example + Stream 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 numbers2 = Stream.of(1, 2, 3, 4, 5); + System.out.println("Stream contains 4? Answer: " + numbers2.anyMatch(n -> n == 4)); + + Stream numbers3 = Stream.of(1, 2, 3, 4, 5); + System.out.println("Stream does not contains 4? Answer: " + numbers3.noneMatch(n -> n == 4)); + + Stream 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 names3 = Stream.of("Sujay", "Vilas", "Basker", "Arjun", "Stuti"); + Optional firstNameWithA = names3.filter(name -> name.startsWith("P")).findFirst(); + System.out.println("The first Name with P is " + firstNameWithA.orElse("not there")); + + } + +}