Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. package com.scalefocus;
  2.  
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.util.Arrays;
  7. import java.util.HashMap;
  8. import java.util.IntSummaryStatistics;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.stream.Collectors;
  12. import java.util.stream.IntStream;
  13. import java.util.stream.Stream;
  14.  
  15. public class StreamsExamples {
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.         /*// 1. Integer Stream
  19.                 IntStream
  20.                     .range(1, 10)
  21.                     .forEach(System.out::print);
  22.                 System.out.println();
  23.                
  24.                 // 2. Integer Stream with skip
  25.                 IntStream
  26.                     .range(1, 10)
  27.                     .skip(5)
  28.                     .forEach(x -> System.out.println(x));
  29.                 System.out.println();
  30.                
  31.                 // 3. Integer Stream with sum
  32.                 System.out.println(
  33.                 IntStream
  34.                     .range(1, 5)
  35.                     .sum());
  36.                 System.out.println();*/
  37.                    
  38.                 // 4. Stream.of, sorted and findFirst
  39.                 Stream.of("Ava", "Aneri", "Alberto")
  40.                     .sorted()
  41.                     .findFirst()
  42.                     .ifPresent(System.out::println);
  43.                    
  44.             /*  // 5. Stream from Array, sort, filter and print
  45.                 String[] names = {"Al", "Ankit", "Kushal", "Brent", "Sarika", "amanda", "Hans", "Shivika", "Sarah"};
  46.                 Arrays.stream(names)    // same as Stream.of(names)
  47.                     .filter(x -> x.startsWith("S"))
  48.                     .sorted()
  49.                     .forEach(System.out::println);
  50.                            
  51.                 // 6. average of squares of an int array
  52.                 Arrays.stream(new int[] {2, 4, 6, 8, 10})
  53.                     .map(x -> x * x)
  54.                     .average()
  55.                     .ifPresent(System.out::println);
  56.                
  57.                 // 7. Stream from List, filter and print
  58.                 List<String> people = Arrays.asList("Al", "Ankit", "Brent", "Sarika", "amanda", "Hans", "Shivika", "Sarah");
  59.                 people
  60.                     .stream()
  61.                     .map(String::toLowerCase)
  62.                     .filter(x -> x.startsWith("a"))
  63.                     .forEach(System.out::println);
  64.                    
  65.                 // 8. Stream rows from text file, sort, filter, and print
  66.                 Stream<String> bands = Files.lines(Paths.get("bands.txt"));
  67.                 bands
  68.                     .sorted()
  69.                     .filter(x -> x.length() > 13)
  70.                     .forEach(System.out::println);
  71.                 bands.close();
  72.                
  73.                 // 9. Stream rows from text file and save to List
  74.                 List<String> bands2 = Files.lines(Paths.get("bands.txt"))
  75.                     .filter(x -> x.contains("jit"))
  76.                     .collect(Collectors.toList());
  77.                 bands2.forEach(x -> System.out.println(x));
  78.                
  79.                 // 10. Stream rows from CSV file and count
  80.                 Stream<String> rows1 = Files.lines(Paths.get("data.txt"));
  81.                 int rowCount = (int)rows1
  82.                     .map(x -> x.split(","))
  83.                     .filter(x -> x.length == 3)
  84.                     .count();
  85.                 System.out.println(rowCount + " rows.");
  86.                 rows1.close();
  87.                
  88.                 // 11. Stream rows from CSV file, parse data from rows
  89.                 Stream<String> rows2 = Files.lines(Paths.get("data.txt"));
  90.                 rows2
  91.                     .map(x -> x.split(","))
  92.                     .filter(x -> x.length == 3)
  93.                     .filter(x -> Integer.parseInt(x[1]) > 15)
  94.                     .forEach(x -> System.out.println(x[0] + "  " + x[1] + "  " + x[2]));
  95.                 rows2.close();
  96.                
  97.                 // 12. Stream rows from CSV file, store fields in HashMap
  98.                 Stream<String> rows3 = Files.lines(Paths.get("data.txt"));
  99.                 Map<String, Integer> map = new HashMap<>();
  100.                 map = rows3
  101.                     .map(x -> x.split(","))
  102.                     .filter(x -> x.length == 3)
  103.                     .filter(x -> Integer.parseInt(x[1]) > 15)
  104.                     .collect(Collectors.toMap(
  105.                         x -> x[0],
  106.                         x -> Integer.parseInt(x[1])));
  107.                 rows3.close();
  108.                 for (String key : map.keySet()) {
  109.                     System.out.println(key + "  " + map.get(key));
  110.                 }
  111.                    
  112.                 // 13. Reduction - sum
  113.                 double total = Stream.of(7.3, 1.5, 4.8)
  114.                     .reduce(0.0, (Double a, Double b) -> a + b);
  115.                 System.out.println("Total = " + total);
  116.                
  117.                 // 14. Reduction - summary statistics
  118.                 IntSummaryStatistics summary = IntStream.of(7, 2, 19, 88, 73, 4, 10)
  119.                     .summaryStatistics();
  120.                 System.out.println(summary);
  121.         */
  122.     }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement