前年年底的时候我写了一些关于Java 8 Lambda和Stream的文章,本文应该在那个时候完成。后来忙于项目和写《Scala集合技术手册》(Scala Collections Cookbook)这本书,一直没来得及写Java Stream的文章,现在这篇文章算是对 Java Stream的一个总结吧。介绍
本节翻译整理自Javadoc,并对流的这些特性做了进一步的解释。
List l = new ArrayList(Arrays.asList("one", "two")); Stream sl = l.stream(); sl.forEach(s -> l.add("three")); |
List l = new ArrayList(Arrays.asList("one", "two")); Stream sl = l.stream(); l.add("three"); sl.forEach(System.out::println); |
List l = new CopyOnWriteArrayList<>(Arrays.asList("one", "two")); Stream sl = l.stream(); sl.forEach(s -> l.add("three")); |
List l = new ArrayList(Arrays.asList("one", "two", ……)); class State { boolean s; final State state = new State(); Stream sl = l.stream().map(e -> { if (state.s) return "OK"; else { state.s = true; return e; }); sl.forEach(System.out::println); |
ArrayList results = new ArrayList<>(); stream.filter(s -> pattern.matcher(s).matches()) .forEach(s -> results.add(s)); |
Listresults = stream.filter(s -> pattern.matcher(s).matches()) .collect(Collectors.toList()); |
(a op b) op c == a op (b op c) |
a op b op c op d == (a op b) op (c op d) |
List l = Stream.of("a","b","c","b") .distinct() .collect(Collectors.toList()); System.out.println(l); |
List l = IntStream.range(1,10) .filter( i -> i % 2 == 0) .boxed() .collect(Collectors.toList()); System.out.println(l); |
List l = Stream.of('a','b','c') .map( c -> c.hashCode()) .collect(Collectors.toList()); System.out.println(l); |
Stream flatMap(Function> mapper) |
String poetry = "Where, before me, are the ages that have gone?\n" + "And where, behind me, are the coming generations?\n" + "I think of heaven and earth, without limit, without end,\n" + "And I am all alone and my tears fall down."; Stream lines = Arrays.stream(poetry.split("\n")); Stream words = lines.flatMap(line -> Arrays.stream(line.split(" "))); List l = words.map( w -> { if (w.endsWith(",") || w.endsWith(".") || w.endsWith("?")) return w.substring(0,w.length() -1).trim().toLowerCase(); else return w.trim().toLowerCase(); }).distinct().sorted().collect(Collectors.toList()); System.out.println(l); |
List l = IntStream.range(1,100).limit(5) .boxed() .collect(Collectors.toList()); System.out.println(l); |
String[] arr = new String[]{"a","b","c","d"}; Arrays.stream(arr) .peek(System.out::println) .count(); |
String[] arr = new String[]{"b_123","c+342","b#632","d_123"}; List l = Arrays.stream(arr) .sorted((s1,s2) -> { if (s1.charAt(0) == s2.charAt(0)) return s1.substring(2).compareTo(s2.substring(2)); else return s1.charAt(0) - s2.charAt(0); .collect(Collectors.toList()); System.out.println(l); |
public boolean allMatch(Predicate predicate) public boolean anyMatch(Predicate predicate) public boolean noneMatch(Predicate predicate) |
System.out.println(Stream.of(1,2,3,4,5).allMatch( i -> i > 0)); System.out.println(Stream.of(1,2,3,4,5).anyMatch( i -> i > 0)); System.out.println(Stream.of(1,2,3,4,5).noneMatch( i -> i > 0)); System.out.println(Stream.empty().allMatch( i -> i > 0)); System.out.println(Stream.empty().anyMatch( i -> i > 0)); System.out.println(Stream.empty().noneMatch( i -> i > 0)); |
mapToLong(e -> 1L).sum(); |
R collect(Collector collector) R collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner) |
R result = supplier.get(); for (T element : this stream) accumulator.accept(result, element); return result; |
List asList = stringStream.collect(ArrayList::new, ArrayList::add, ArrayList::addAll); String concat = stringStream.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append) .toString(); |
Stream.of(1,2,3,4,5).forEach(System.out::println); |
pubic Optional reduce(BinaryOperator accumulator) pubic T reduce(T identity, BinaryOperator accumulator) pubic U reduce(U identity, BiFunction accumulator, BinaryOperator combiner) |
Optional total = Stream.of(1,2,3,4,5).reduce( (x, y) -> x +y); Integer total2 = Stream.of(1,2,3,4,5).reduce(0, (x, y) -> x +y); |
public static Stream concat(Stream a, Stream b) |
public static <t,c extends="" collection> Collector toCollection(Supplier collectionFactory) public static …… toConcurrentMap(……) public static Collector<t,?,list> toList()</t,?,list public static …… toMap(……) public static Collector<t,?,set> toSet()</t,?,set <t,?,set </t,?,set 【转载】仅作分享,侵删 |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |