Stream常用方法
sort 排序
@Test
public void streamSort(){
List<Integer> list = Arrays.asList(1, 4, 3, 1);
List<Person> personList = Arrays.asList(new Person(1,"11"),new Person(2,"13"),new Person(3,"14"));
list = list.stream().sorted((o1, o2) -> o1-o2).collect(Collectors.toList());
personList.stream().sorted(Comparator.comparingInt(o -> Integer.valueOf(o.getAge())))
.collect(Collectors.toList());
personList.stream().sorted((o1, o2) -> Integer.valueOf(o1.getAge()) - Integer.valueOf(o2.getAge()))
.collect(Collectors.toList());
System.out.println(list.toString());
}
filter过滤器
@Test
public void streamFilter() {
List<String> list = Arrays.asList("", "a", "b", "c");
List<String> result = list.stream().filter(s -> !s.isEmpty()).collect(Collectors.toList());
System.out.println("filter" + result.toString());
}
foreach 迭代,limit截取流,skip跳过流(都是从开始)
@Test
public void streamForEach() {
List<String> list = Arrays.asList("", "a", "b", "c");
// 跳过第一个,总共处理size条
list.stream().skip(1).limit(list.size()).forEach(s -> System.out.println("foreach:" + s.toString()));
}
distinct去重
@Test
public void streamDistinct() {
// String去重
List<String> list = Lists.newArrayList("", "a", "b", "c", "a", "c");
List<String> result = list.stream().distinct().collect(Collectors.toList());
System.out.println("distinct" + result.toString());
// 对象去重
Set<Person> personSet = new TreeSet<>((o1, o2) -> o1.getId().compareTo(o2.getId()));
}
match匹配
@Test
public void streamMatch() {
List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5, 6);
boolean allMatch = list.stream().allMatch(v -> v.intValue() == 1);
boolean anyMatch = list.stream().anyMatch(v -> v.intValue() == 1);
boolean noneMatch = list.stream().noneMatch(v -> v.intValue() == 10);
System.out.println("allMatch:" + allMatch);
System.out.println("anyMatch:" + anyMatch);
System.out.println("noneMatch:" + noneMatch);
}
并行流
@Test
public void streamParallel() {
long i = 40000000l;
List<Long> list = Lists.newArrayList(1l, 2l, 3l, 4l, 5l, 6l);
while (i > 0) {
list.add(i);
i--;
}
// stramfork/join耗时
// 在数组容量小于10000的情况下,并行排序和串行排序消耗时间是差不多的,但是当数组容量在10000以上的时候,并行排序就体现出了它的优势。
// 在数数据量较大时使用parallelStream
long startTime = System.currentTimeMillis();
System.out.println("parallelStream result"
+ list.parallelStream().reduce((integer, integer2) -> integer + integer2).get());
long costParallel = System.currentTimeMillis() - startTime;
// stream耗时
long startTime2 = System.currentTimeMillis();
System.out.println("stream result" + list.stream().reduce((integer, integer2) -> integer + integer2).get());
long cost = System.currentTimeMillis() - startTime2;
// 普通for循环耗时
Long startTime3 = System.currentTimeMillis();
Long result = 0l;
for (Long aLong : list) {
result = result + aLong.longValue();
}
long costFor = System.currentTimeMillis() - startTime3;
System.out.println("foreach循环 result:" + result.longValue());
System.out.println("costParallel:" + costParallel);
System.out.println("cost:" + cost);
System.out.println("costFor:" + costFor);
}
Collector 使用
toList,toSet,joining
@Test
public void collector(){
List<String> list = Stream.of("1","2","3","4").collect(Collectors.toList());
Set<String> set = Stream.of("1","2","3","2","3").collect(Collectors.toSet());
//字符串相连
System.out.println(Stream.of("1","2","3","4").collect(Collectors.joining()));
//字符串拼接
System.out.println(Stream.of("1","2","3","4").collect(Collectors.joining(",","[","]")));
}
groupby 分组
@Test
public void groupbyCollector(){
//分组,key 为o.toString()的返回值
List<Person> personList = Arrays.asList(new Person(1,"11"),new Person(2,"13"),new Person(3,"14"),new Person(6,"11"));
Map<String,List<Integer>> result = Stream.of(1,2,3,4,5,1,23,32,4,5).collect(Collectors.groupingBy(o -> o.toString()));
//按照key 分组
Map<String,List<Person>> resultPerson = personList.stream().collect(Collectors.groupingBy(Person::getAge));
Map<String,List<Person>> resultPerson1 = personList.stream().collect(Collectors.groupingBy(t -> t.getAge(),Collectors.toList()));
Map<String,list<Integer>> listMap2 = personLisst.stream().collection.(Collectors.groupingBy(Person::getAge,Collectors.toMap(Person::getAge,Collection.toList())))
//List 转为Map,person2 覆盖person1 --null值会报错
Map<String, Person> listMap = personList.stream().collect(Collectors.toMap(Person::getAge,person -> person,(person, person2) -> person2));
result.forEach((s, integers) -> System.out.println("key:"+ s + " value:" + integers.toString()));
resultPerson.forEach((s, people) -> System.out.println("key:"+ s + " value:" + people.toString()));
resultPerson1.forEach((s, people) -> System.out.println("key:"+ s + " value:" + people.toString()));
}
lambda 方法引用
@Test
public void testLamdba(){
List<String> list = Lists.newArrayList("1","2","3");
//使用方法引用后的代码
//System.out::println == x -> System.out.println(x)
list.forEach(System.out::println);
}
newMap.forEach((key, value) -> result.computeIfAbsent(key, v -> Lists.newArrayList()).addAll(value));
result.computeIfAbsent(“水果”,s -> Lists.newArrayList()).add(“4”);