001package edu.pdx.cs410J.java8; 002 003import edu.pdx.cs410J.rmi.Movie; 004 005import java.util.*; 006import java.util.function.*; 007import java.util.stream.Collectors; 008import java.util.stream.IntStream; 009import java.util.stream.Stream; 010 011public class StreamOperations { 012 013 public void printElementsInStream() { 014 Stream<String> stream = Arrays.asList("A", "B", "C").stream(); 015 stream.forEach(new Consumer<String>() { 016 @Override 017 public void accept(String s) { 018 System.out.println(s); 019 } 020 }); 021 022 stream.forEach(System.out::println); 023 } 024 025 public void sumIntsUsingStream() { 026 int[] ints = {1, 2, 3}; 027 IntStream stream = IntStream.of(ints); 028 long count = stream.count(); 029 030 stream = IntStream.of(ints); 031 long sum = stream.sum(); 032 033 stream = IntStream.of(ints); 034 IntSummaryStatistics stats = stream.summaryStatistics(); 035 } 036 037 public void findStringsWithMoreThanOneCharacter() { 038 Stream<String> strings = Stream.of("A", "BB", "C", ""); 039 Stream<String> moreThanTwoChars = strings.filter(new Predicate<String>() { 040 @Override 041 public boolean test(String s) { 042 return s.length() > 1; 043 } 044 }); 045 046 moreThanTwoChars = strings.filter(s -> s.length() > 1); 047 } 048 049 public void parseListOfStrings() { 050 Stream<String> strings = Stream.of("1", "2", "3"); 051 Stream<Integer> ints = strings.map(new Function<String, Integer>() { 052 @Override 053 public Integer apply(String s) { 054 return Integer.parseInt(s); 055 } 056 }); 057 058 IntStream stream = strings.mapToInt(Integer::parseInt); 059 } 060 061 public String getLongestString(Stream<String> strings) { 062 Optional<String> longest = strings.max((s1, s2) -> s1.length() > s2.length() ? 1 : 0); 063 return longest.orElse(""); 064 } 065 066 public Movie findMovieWithId(long id, Stream<Movie> movies) { 067 Optional<Movie> movie = movies.filter(m -> m.getId() == id).findAny(); 068 return movie.orElseThrow(() -> new IllegalArgumentException("Can't find movie")); 069 } 070 071 public long countMoviesWithActor(long actorId, Stream<Movie> movies) { 072 return movies.parallel() 073 .filter(m -> m.getActors().contains(actorId)) 074 .count(); 075 } 076 077 public List<Movie> getMoviesWithActorUnsafe(long actorId, Stream<Movie> allMovies) { 078 List<Movie> movies = new ArrayList<>(); 079 allMovies.parallel() 080 .filter(m -> m.getActors().contains(actorId)) 081 .forEach(m -> movies.add(m)); 082 return movies; 083 } 084 085 public List<Movie> getMoviesWithActorSafe(long actorId, Stream<Movie> allMovies) { 086 return allMovies.parallel() 087 .filter(m -> m.getActors().contains(actorId)) 088 .collect(Collectors.toList()); 089 } 090 091 public int countAwardsForMoviesWithActor(long actorId, Stream<Movie> allMovies) { 092 return allMovies.filter(m -> m.getActors().contains(actorId)) 093 .map(m -> m.getNumberOfAwards()) 094 .reduce(0, new BinaryOperator<Integer>() { 095 @Override 096 public Integer apply(Integer i1, Integer i2) { 097 return i1 + i2; 098 } 099 }); 100 } 101 102 public String concatenateStrings(Stream<String> stream) { 103 return stream.reduce("", (s1, s2) -> s1 + s2); 104 } 105 106 public String concatenateStrings2(Stream<String> stream) { 107 Supplier<StringBuilder> supplier = () -> new StringBuilder(); 108 BiConsumer<StringBuilder, StringBuilder> combiner = (sb1, sb2) -> sb1.append(sb2); 109 BiConsumer<StringBuilder, String> accumulator = (sb, s) -> sb.append(s); 110 StringBuilder sb = stream.collect(supplier, accumulator, combiner); 111 return sb.toString(); 112 } 113 114 public String concatenateStrings3(Stream<String> stream) { 115 return stream.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString(); 116 } 117 118 public String concatenateStrings4(Stream<String> stream) { 119 return stream.collect(Collectors.joining()); 120 } 121 122 public SortedSet<String> getSortedStrings(Stream<String> stream) { 123 return stream.collect(Collectors.toCollection(TreeSet::new)); 124 } 125 126 public double getAverageStringLength(Stream<String> stream) { 127 return stream.collect(Collectors.averagingInt(s -> s.length())); 128 } 129}