- import java.util.*;
- import java.util.stream.Collectors;
- public class TestExe1 {
- public static void main(String[] args) {
- List<String> myList1 =
- Arrays.asList("a1", "a1","a1","a2", "b1", "c2", "c1");
- List<String> teamIndia = Arrays.asList("i1", "i2", "i3");
- List<String> teamAustralia = Arrays.asList("a1", "a2", "a3");
- List<String> teamEngland = Arrays.asList("Alex", "Bell", "Broad");
- List<String> teamNewZeland = Arrays.asList("Kane", "Nathan", "Vettori");
- List<String> teamSouthAfrica = Arrays.asList("AB", "Amla", "Faf");
- List<String> teamWestIndies = Arrays.asList("Sammy", "Gayle", "Narine");
- List<String> teamSriLanka = Arrays.asList("Mahela", "Sanga", "Dilshan");
- List<String> teamPakistan = Arrays.asList("Misbah", "Afridi", "Shehzad");
- List<List<String>> playersInWorldCup2016 = new ArrayList<>();
- playersInWorldCup2016.add(teamIndia);
- playersInWorldCup2016.add(teamAustralia);
- playersInWorldCup2016.add(teamEngland);
- playersInWorldCup2016.add(teamNewZeland);
- playersInWorldCup2016.add(teamSouthAfrica);
- playersInWorldCup2016.add(teamWestIndies);
- playersInWorldCup2016.add(teamSriLanka);
- playersInWorldCup2016.add(teamPakistan);
- // Let's print all players before Java 8
- List<String> listOfAllPlayers = new ArrayList<>();
- for(List<String> team : playersInWorldCup2016){
- for(String name : team){
- listOfAllPlayers.add(name);
- }
- }
- System.out.println("Players playing in world cup 2016");
- System.out.println(listOfAllPlayers);
- List<String> myList =
- Arrays.asList("a1", "a1","a1","a2", "b1", "c2", "c1");
- // Now let's do this in Java 8 using FlatMap
- List<String> flatMapList = playersInWorldCup2016.stream()
- .flatMap(x -> x.stream())
- .collect(Collectors.toList());
- System.out.println("List of all Players using Java 8"); System.out.println(flatMapList);
- myList
- .stream()
- .filter(s1 -> s1.startsWith("c"))
- .map(String::toUpperCase)
- .sorted()
- .skip(1)
- .forEach(System.out::println);
- System.out.println("Using Java 7: ");
- // Count empty strings
- List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
- System.out.println("List: " +strings);
- long count = getCountEmptyStringUsingJava7(strings);
- System.out.println("Empty Strings: " + count);
- count = getCountLength3UsingJava7(strings);
- System.out.println("Strings of length 3: " + count);
- //Eliminate empty string
- List<String> filtered = deleteEmptyStringsUsingJava7(strings);
- System.out.println("Filtered List: " + filtered);
- //Eliminate empty string and join using comma.
- String mergedString = getMergedStringUsingJava7(strings,", ");
- System.out.println("Merged String: " + mergedString);
- List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
- //get list of square of distinct numbers
- List<Integer> squaresList = getSquares(numbers);
- System.out.println("Squares List: " + squaresList);
- List<Integer> integers = Arrays.asList(1,2,13,4,15,6,17,8,19);
- System.out.println("List: " +integers);
- System.out.println("Highest number in List : " + getMax(integers));
- System.out.println("Lowest number in List : " + getMin(integers));
- System.out.println("Sum of all numbers : " + getSum(integers));
- System.out.println("Average of all numbers : " + getAverage(integers));
- System.out.println("Random Numbers: ");
- //print ten random numbers
- Random random = new Random();
- for(int i = 0; i < 10; i++) {
- System.out.println(random.nextInt());
- }
- System.out.println("Using Java 8: ");
- System.out.println("List: " +strings);
- count = strings
- .stream()
- .filter(v -> !(v.isEmpty()))
- .count();
- System.out.println("Empty Strings: " + count);
- count = strings.stream().filter(string -> string.length() == 3).count();
- System.out.println("Strings of length 3: " + count);
- filtered = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.toList());
- System.out.println("Filtered List: " + filtered);
- mergedString = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.joining(", "));
- System.out.println("Merged String: " + mergedString);
- squaresList = numbers.stream().map( i ->i*i).distinct().collect(Collectors.toList());
- System.out.println("Squares List: " + squaresList);
- System.out.println("List: " +integers);
- IntSummaryStatistics stats = integers.stream().mapToInt((x) ->x).summaryStatistics();
- System.out.println("Highest number in List : " + stats.getMax());
- System.out.println("Lowest number in List : " + stats.getMin());
- System.out.println("Sum of all numbers : " + stats.getSum());
- System.out.println("Average of all numbers : " + stats.getAverage());
- System.out.println("Random Numbers: ");
- random.ints().limit(10).sorted().forEach(System.out::println);
- //parallel processing
- count = strings.parallelStream().filter(string -> string.isEmpty()).count();
- System.out.println("Empty Strings: " + count);
- }
- private static int getCountEmptyStringUsingJava7(List<String> strings) {
- int count = 0;
- for(String string: strings) {
- if(string.isEmpty()) {
- count++;
- }
- }
- return count;
- }
- private static int getCountLength3UsingJava7(List<String> strings) {
- int count = 0;
- for(String string: strings) {
- if(string.length() == 3) {
- count++;
- }
- }
- return count;
- }
- private static List<String> deleteEmptyStringsUsingJava7(List<String> strings) {
- List<String> filteredList = new ArrayList<String>();
- for(String string: strings) {
- if(!string.isEmpty()) {
- filteredList.add(string);
- }
- }
- return filteredList;
- }
- private static String getMergedStringUsingJava7(List<String> strings, String separator) {
- StringBuilder stringBuilder = new StringBuilder();
- for(String string: strings) {
- if(!string.isEmpty()) {
- stringBuilder.append(string);
- stringBuilder.append(separator);
- }
- }
- String mergedString = stringBuilder.toString();
- return mergedString.substring(0, mergedString.length()-2);
- }
- private static List<Integer> getSquares(List<Integer> numbers) {
- List<Integer> squaresList = new ArrayList<Integer>();
- for(Integer number: numbers) {
- Integer square = new Integer(number.intValue() * number.intValue());
- if(!squaresList.contains(square)) {
- squaresList.add(square);
- }
- }
- return squaresList;
- }
- private static int getMax(List<Integer> numbers) {
- int max = numbers.get(0);
- for(int i = 1;i < numbers.size();i++) {
- Integer number = numbers.get(i);
- if(number.intValue() > max) {
- max = number.intValue();
- }
- }
- return max;
- }
- private static int getMin(List<Integer> numbers) {
- int min = numbers.get(0);
- for(int i= 1;i < numbers.size();i++) {
- Integer number = numbers.get(i);
- if(number.intValue() < min) {
- min = number.intValue();
- }
- }
- return min;
- }
- private static int getSum(List numbers) {
- int sum = (int)(numbers.get(0));
- for(int i = 1;i < numbers.size();i++) {
- sum += (int)numbers.get(i);
- }
- return sum;
- }
- private static int getAverage(List<Integer> numbers) {
- return getSum(numbers) / numbers.size();
- }
Thursday, June 28, 2018
Java 8 Stream API implementation
Subscribe to:
Post Comments (Atom)
How to run standalone mock server on local laptop
Please download the standalone wiremock server from Direct download section at the bottom of the page. Download and installation Feel fre...
-
Cryptography or cryptology (from Ancient Greek: kryptós "hidden, secret"; graphein, "to write") is the practice and stu...
-
Problem statement: In a dark room,there is a box of 18 white and 5 black gloves. You are allowed to pick one and then you are allowed to k...
-
Please download the standalone wiremock server from Direct download section at the bottom of the page. Download and installation Feel fre...
No comments:
Post a Comment