java8提供了许多的新特性,最近工作恰好用到了steam,总结一下,自用: Filter过滤集合中不符合条件的元素
@Test
public void testStreamFilter(){
List<String> list = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> stringList = list.stream().filter(str -> str.contains("f")).collect(Collectors.toList());
stringList.forEach(System.out::println);
}
输出结果
efg
去重
@Test
public void testStreamDistinct(){
List<String> list = Arrays.asList("abc", "", "abc", "efg", "abcd","", "jkl");
List<String> strings = list.stream().distinct().collect(Collectors.toList());
System.out.println(strings);
}
输出:
[abc, , efg, abcd, jkl]
limit获取前几个元素:
@Test
public void testLimit(){
List<String> list = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> limited = list.stream().limit(3).collect(Collectors.toList());
System.out.println(limited);
}
输出:
[abc, , bc]
skip 除去流中的前几个元素
@Test
public void testSkip(){
List<String> list = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> skiped = list.stream().skip(2).collect(Collectors.toList());
System.out.println(skiped);
}
输出:
[bc, efg, abcd, , jkl]
map 对集合中的元素统一处理
@Test
public void testMap(){
List<String> list = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> mapped = list.stream().map(str -> str.concat("_java")).collect(Collectors.toList());
System.out.println(mapped);
}
输出:
[abc_java, _java, bc_java, efg_java, abcd_java, _java, jkl_java]
sort对集合进行排序
@Test
public void testSort(){
List<String> strings1 = Arrays.asList("abc", "abd", "aba", "efg", "abcd","jkl", "jkl");
List<String> strings2 = Arrays.asList("张三", "李四", "王五", "赵柳", "张哥","李哥", "王哥");
List<Integer> strings3 = Arrays.asList(10, 2, 30, 22, 1,0, -9);
List<String> sorted1 = strings1.stream().sorted().collect(Collectors.toList());
List<String> sorted2 = strings2.stream().sorted(Collections.reverseOrder(Collator.getInstance(Locale.CHINA))).collect(Collectors.toList());
List<Integer> sorted3 = strings3.stream().sorted().collect(Collectors.toList());
System.out.println(sorted1);
System.out.println(sorted2);
System.out.println(sorted3);
}
输出:
[aba, abc, abcd, abd, efg, jkl, jkl]
[赵柳, 张三, 张哥, 王五, 王哥, 李四, 李哥]
[-9, 0, 1, 2, 10, 22, 30]
AnyMatch 测试集合中至少有一个满足条件
@Test
public void testAnyMatch(){
List<String> strings = Arrays.asList("abc", "abd", "aba", "efg", "abcd","jkl", "jkl");
boolean b = strings.stream().anyMatch(o -> o == "abc");
System.out.println(b);
}
输出:
true
AllMatch 集合中的所以元素都满足条件
@Test
public void testAllMatch(){
List<String> strings = Arrays.asList("abc", "abd", "aba", "efg", "abcd","jkl", "jkl");
boolean b = strings.stream().allMatch(o -> o == "abc");
System.out.println(b);
}
输出:
false
NonMatch 是否一个都不匹配
@Test
public void testNonMatch(){
List<String> strings = Arrays.asList("abc", "abd", "aba", "efg", "abcd","jkl", "jkl");
boolean b = strings.stream().noneMatch(o -> o == "abcdc");
System.out.println(b);
}
输出:
true
FindAny 返回任意元素
@Test
public void testFindAny(){
List<String> strings = Arrays.asList("abc", "abd", "aba", "efg", "abcd","jkl", "jkl");
Optional<String> any = strings.stream().findAny();
if (any.isPresent()){
System.out.println(any);
}
}
输出:
Optional[abc]
FindFirst 返回集合中第一个元素
@Test
public void testFindFirst(){
List<String> strings = Arrays.asList("abc", "abd", "aba", "efg", "abcd","jkl", "jkl");
Optional<String> first = strings.stream().findFirst();
if (first.isPresent()){
System.out.println(first);
}
}
输出:
Optional[abc]
|