Stream intermediate operation & filter
ArrayList<String> list = new ArrayList<>();
list.add("rfdgfdsf");
list.add("efdggfddfgsf");
list.add("wfdgfdgfdgsf");
list.add("afdgfdgsf");
list.add("dfdsf");
list.add("fdsgfdgf");
list.add("tfdsf");
list.add("fdsf");
list.stream().filter(s->s.startsWith("f")).filter(s->s.length()==4).forEach(System.out::println);
Stream intermediate operation & limit & skip
limit: the number of specified parameters before interception
skip: skip the data of the specified number of parameters, and return the data composed of the remaining elements of the stream
ArrayList<String> list = new ArrayList<>();
list.add("rfdgfdsf");
list.add("efdggfddfgsf");
list.add("wfdgfdgfdgsf");
list.add("afdgfdgsf");
list.add("dfdsf");
list.add("fdsgfdgf");
list.add("tfdsf");
list.add("fdsf");
list.stream().skip(2).limit(2).forEach(System.out::println);
Intermediate operation of stream &concat&distinct
concat: merge two streams into a new stream
distinct: remove duplicate elements in the stream
Stream<String> stringStream1 = list.stream().skip(2);
Stream<String> stringStream2 = list.stream().limit(2);
Stream<String> stringStream3 = Stream.concat(stringStream1, stringStream2);
stringStream3.distinct().forEach(System.out::println);
Stream intermediate operation & sort
sort: sort in natural order
ArrayList<String> list = new ArrayList<>();
list.add("rfdgfdsf");
list.add("efdggfddfgsf");
list.add("wfdgfdgfdgsf");
list.add("afdgfdgsf");
list.add("dfdsf");
list.add("fdsgfdgf");
list.add("tfdsf");
list.add("fdsf");
list.stream().sorted((s1, s2) -> {
int num = s1.length() - s2.length();//对首字母和字符串长度进行排序
int num2 = num == 0 ? s1.compareTo(s2) : num;
return num2;
}).forEach(System.out::println);
Intermediate operation of stream &map&sum
ArrayList<String> list = new ArrayList<>();
list.add("6546");
list.add("6546");
list.add("435435");
list.add("6546");
list.stream().map(Integer::parseInt).forEach(System.out::println);
int sum = list.stream().mapToInt(Integer::parseInt).sum();
System.out.println(sum);
Stream collection method
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(33);
list1.add(44);
list1.add(55);
list1.add(44);
//得到数字大于22的流并转成set集合打印输出
list1.stream().filter(s -> s>22).collect(Collectors.toSet()).forEach(System.out::println);
String[] list2={
"科比,41","杜兰特,30","詹姆斯,35"};
//得到字符串中数据大于28的流
Stream<String> stringStream = Stream.of(list2).filter(s -> Integer.parseInt(s.split(",")[1]) > 28);
//把获取的流收集到map集合中,key做建,value做值
Map<String, Integer> map = stringStream.collect(Collectors.toMap(s -> s.split(",")[0], s -> Integer.parseInt(s.split(",")[1])));
Set<String> keySet = map.keySet();
for (String key : keySet) {
Integer value = map.get(key);
System.out.println(key+","+value);
}
}
How the stream is created
HashMap<Object, Object> map = new HashMap<>();
Stream<Object> keyStream = map.keySet().stream();
Stream<Object> valueStream = map.values().stream();
Stream<Map.Entry<Object, Object>> entryStream = map.entrySet().stream();
Stream<Integer> integerStream = Stream.of(1, 2, 3);
Stream<String> stringStream = Stream.of("dd", "dsd", "fdsf");