1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| package note_file.other.other;
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import java.util.stream.Collectors; import java.util.stream.Stream;
public class Stream_demo { public static void main(String[] args) { ArrayList<String> array = new ArrayList<>(); array.add("charles"); array.add("张三丰"); array.add("张飞"); array.add("张无忌"); array.stream().filter(s -> s.startsWith("张")).filter(s -> s.length() == 3).forEach(System.out::println);
List<String> list = new ArrayList<>(); list.add("50"); list.add("10"); list.add("30"); list.add("40"); list.add("20");
Stream<String> list_stream = list.stream();
Set<Integer> set = new HashSet<>(); set.add(10); set.add(50); set.add(20); set.add(30); set.add(40); Stream<Integer> set_stream = set.stream(); Map<String, Integer> map = new HashMap<>(); map.put("charles", 21373191); map.put("Tom", 21373188); map.put("Jerry", 21373181); Stream<String> key_stream = map.keySet().stream(); Stream<Integer> value_stream = map.values().stream(); Stream<Entry<String, Integer>> entryset_stream = map.entrySet().stream();
String[] strArray = { "hello", "world", "NBA" }; Stream<String> strArray_stream = Stream.of(strArray);
array.stream().filter(s -> s.startsWith("张")).filter(s -> s.length() == 3).forEach(System.out::println); array.stream().limit(3).forEach(System.out::println); array.stream().skip(2).limit(2).forEach(System.out::println);
Stream<String> s1 = array.stream().limit(2); Stream<String> s2 = array.stream().skip(1); Stream.concat(s1, s2).distinct().forEach(System.out::println); strArray_stream.sorted().forEach(System.out::println); Stream.of(strArray).sorted((m1, m2) -> m1.length() - m2.length()).forEach(System.out::println); list.stream().map(Integer::parseInt).forEach(System.out::println); int result = list.stream().mapToInt(Integer::parseInt).sum(); System.out.println(result);
array.stream().forEach(System.out::println); long count = array.stream().filter(s -> s.startsWith("张")).count(); System.out.println(count);
Stream<String> filter = list.stream().filter(s -> s.length() == 3); List<String> names = filter.collect(Collectors.toList()); for (String name : names) { System.out.println(name); } Stream<Integer> filter2 = set.stream().filter(age -> age > 25); Set<Integer> ages = filter2.collect(Collectors.toSet()); for (int i : ages) { System.out.println(i); }
String[] nameArray = { "charles,19", "Lily,43", "long,50" }; Stream<String> filter3 = Stream.of(nameArray).filter(s -> Integer.parseInt(s.split(",")[1]) > 25); Map<String, Integer> map2 = filter3 .collect(Collectors.toMap((s -> s.split(",")[0]), s -> Integer.parseInt(s.split(",")[1]))); Set<Entry<String, Integer>> entrySet = map2.entrySet(); for (Entry<String, Integer> me : entrySet) { System.out.println(me.getKey() + "," + me.getValue()); }
} }
|