JAVA 8 Stream In Java
参考资料:https://howtodoinjava.com/java8/java-streams-by-examples/
为什么要用Stream?
- 使代码更优雅
- 效率更高
不同的方式来创建流
-
Stream.of(val1, val2, val3….)
public class StreamBuilders { public static void main(String[] args) { Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9); stream.forEach(p -> System.out.println(p)); } }
-
Stream.of(arrayOfElements)
public class StreamBuilders { public static void main(String[] args) { Stream<Integer> stream = Stream.of( new Integer[]{1,2,3,4,5,6,7,8,9} ); stream.forEach(p -> System.out.println(p)); } }
-
List.stream()
public class StreamBuilders { public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); for(int i = 1; i< 10; i++){ list.add(i); } Stream<Integer> stream = list.stream(); stream.forEach(p -> System.out.println(p)); } }
-
Stream.generate() or Stream.iterate()
public class StreamBuilders { public static void main(String[] args) { Stream<Date> stream = Stream.generate(() -> { return new Date(); }); stream.forEach(p -> System.out.println(p)); } }
-
String chars or String tokens
public class StreamBuilders { public static void main(String[] args) { IntStream stream = "12345_abcdefg".chars(); stream.forEach(p -> System.out.println(p)); //OR Stream<String> stream = Stream.of("A$B$C".split("\\$")); stream.forEach(p -> System.out.println(p)); } }
流转换为集合
-
Convert Stream to List – Stream.collect( Collectors.toList() )
public static void main(String[] args){ List<Integer> list = new ArrayList<Integer>(); for(int i = 1; i< 10; i++){ list.add(i); } Stream<Integer> stream1 = list.stream(); List<Integer> evenNumbersList = stream1.filter(i -> i%2 == 0). collect(Collectors.toList()); Stream<Integer> stream2 = list.stream(); List<String> str1List = stream2.map(i -> String.valueOf(i)). collect(Collectors.toList()); Stream<Integer> stream3 = list.stream(); List<String> str2List = stream3.sorted(Comparator. comparing(Integer::intValue, (o1, o2)->{ //从大到小排序 o2-o1 //从小到大排序 o1-o2 return o2-o1; })).map(i -> String.valueOf(i)).collect(Collectors.toList()); System.out.print(evenNumbersList); System.out.print(str1List); System.out.println(str2List); }
-
Convert Stream to array – Stream.toArray( EntryType[]::new )
public class StreamBuilders { public static void main(String[] args){ List<Integer> list = new ArrayList<Integer>(); for(int i = 1; i< 10; i++){ list.add(i); } Stream<Integer> stream = list.stream(); Integer[] evenNumbersArr = stream.filter(i -> i%2 == 0).toArray(Integer[]::new); System.out.print(evenNumbersArr); } }
核心流操作
-
Stream.filter()
public class StreamBuilders { public static void main(String[] args){ List<String> memberNames = new ArrayList<>(); memberNames.add("Amitabh"); memberNames.add("Shekhar"); memberNames.add("Aman"); memberNames.add("Rahul"); memberNames.add("Shahrukh"); memberNames.add("Salman"); memberNames.add("Yana"); memberNames.add("Lokesh"); memberNames.stream().filter((s) -> s.startsWith("A")) .forEach(System.out::println); } }
-
Stream.map()
public class StreamBuilders { public static void main(String[] args){ List<String> memberNames = new ArrayList<>(); memberNames.add("Amitabh"); memberNames.add("Shekhar"); memberNames.add("Aman"); memberNames.add("Rahul"); memberNames.add("Shahrukh"); memberNames.add("Salman"); memberNames.add("Yana"); memberNames.add("Lokesh"); memberNames.stream().filter((s) -> s.startsWith("A")) .map(String::toUpperCase) .forEach(System.out::println); } }
-
Stream.sorted()
public class StreamBuilders { public static void main(String[] args){ List<String> memberNames = new ArrayList<>(); memberNames.add("Amitabh"); memberNames.add("Shekhar"); memberNames.add("Aman"); memberNames.add("Rahul"); memberNames.add("Shahrukh"); memberNames.add("Salman"); memberNames.add("Yana"); memberNames.add("Lokesh"); memberNames.stream().sorted() .map(String::toUpperCase) .forEach(System.out::println); } }
-
Stream.forEach()
memberNames.forEach(System.out::println);
-
Stream.collect()
List<String> memNamesInUppercase = memberNames.stream().sorted() .map(String::toUpperCase) .collect(Collectors.toList()); System.out.print(memNamesInUppercase);
-
Stream.match()
boolean matchedResult = memberNames.stream() .anyMatch((s) -> s.startsWith("A")); System.out.println(matchedResult); matchedResult = memberNames.stream() .allMatch((s) -> s.startsWith("A")); System.out.println(matchedResult); matchedResult = memberNames.stream() .noneMatch((s) -> s.startsWith("A")); System.out.println(matchedResult);
-
Stream.count()
long totalMatched = memberNames.stream() .filter((s) -> s.startsWith("A")) .count(); System.out.println(totalMatched);
-
Stream.reduce()
Optional<String> reduced = memberNames.stream() .reduce((s1,s2) -> s1 + "#" + s2); reduced.ifPresent(System.out::println);
-
Stream.anyMatch()
boolean matched = memberNames.stream() .anyMatch((s) -> s.startsWith("A")); System.out.println(matched);
-
Stream.findFirst()
String firstMatchedName = memberNames.stream() .filter((s) -> s.startsWith("L")) .findFirst().get(); System.out.println(firstMatchedName);