java8
转换类型:把list类型转换为其他数据类型
// 1. Array
String[] strArray1 = stream.toArray(String[]::new);
// 2. Collection
List<String> list1 = stream.collect(Collectors.toList());
List<String> list2 = stream.collect(Collectors.toCollection(ArrayList::new));
Set set1 = stream.collect(Collectors.toSet());
Stack stack1 = stream.collect(Collectors.toCollection(Stack::new));
// 3. String
String str = stream.collect(Collectors.joining()).toString();
以下是对collection相关操作 :
1、map(s ->s.toUpperCase()) 对List进
ls=ls.stream().map(String::toUpperCase).collect(Collectors.toList());
ls=ls.stream().map(Friend::setName).collect(Collectors.toList());
ls = ls.stream().map(s->s.toUpperCase()).collect(Collectors.toList());
2、flatMap(lsc -> .stream()); 对List 进行操作
Stream<String> stream = Stream.of(Arrays.asList("a","b"),
Arrays.asList("aa","bb","cc"), Arrays.asList("aaa","bbb","ccc","ddd")).flatMap(lsc -> lsc.stream());
ls=stream.collect(Collectors.toList());
注意:对同一个Stream操作时,如果是使用了最终方法,则这个流就被关闭,得重新被打开,如下:
Stream<String> stream = Stream.of(Arrays.asList("a","b"),
Arrays.asList("aa","bb","cc"),
Arrays.asList("aaa","bbb","ccc","ddd")).flatMap(lsc -> lsc.stream());
// ls = stream.collect(Collectors.toList()); 这个流在这里已经关闭了,不能进行后续操作哦
ls=stream.map(String::toUpperCase).collect(Collectors.toList());
3、stream.forEach():对List中每个对象进行操作
stream.forEach(p -> {
//
System.out.println();
});
// 把第一个参数的字符和后面的追加在一起,
stream.reduce("",String::concat)
//如果第一个参数为空则会输出Optional[10]对象
Stream.of(1,2,3,4).reduce(Integer::sum);
// 同上
Stream.of(1,2,3,4).reduce(10,Integer::sum)
// 使用get则会取Optional得值
Stream.of(1,2,3,4).reduce(Integer::sum).get()
4、区某区域的数据
ls = stream.skip(1).collect(Collectors.toList()); 跳过前1个
ls = stream.limit(3).collect(Collectors.toList()); 只要前3个
ls=stream.skip(3).limit(3).collect(Collectors.toList()); 也可以联合使用 按照顺序执行
5、Match
Stream 有三个 match 方法,从语义上说:
allMatch:Stream 中全部元素符合传入的 predicate,返回 true
anyMatch:Stream 中只要有一个元素符合传入的 predicate,返回 true
noneMatch:Stream 中没有一个元素符合传入的 predicate,返回 true
List<Friend> fri = new ArrayList<>();
for(int i = 0;i<10;i++){
Friend f = new Friend();
f.setAge(i);
fri.add(f);
}
System.out.println(fri.stream().sorted((a1,a2)->a1.getAge()>a2.getAge()?1:2).anyMatch(a -> a.getAge()>5)); //--->true
6、groupingBy 使用:对流按照关键字分组
Map<Integer,List<Friend>> map = fri.stream().collect(Collectors.groupingBy(Friend::getAge));
for (List<Friend> f:map.values()) {
System.out.println(f);
}
7、partitioningBy使用:按照条件分组
Map<Boolean,List<Friend>> map = fri.stream().collect(Collectors.partitioningBy(a -> a.getAge()>5));
for (List<Friend> f:map.values()) {
System.out.println(f);
}
8、 自定义生成流
使用IntStream.generate 生成随机数流:
生成随机数并输出:
Supplier<Integer> random = new Random()::nextInt;
IntStream.generate(new Random()::nextInt).limit(10).forEach(System.out::println);
使用Stream.generate生成自定义流:
private static class Inner implements Supplier{
int index = 0;
Random rd = new Random();
@Override
public Object get() {
return new Friend("name" +index, rd.nextInt(100));
}
}
生成自定义对象流并输出:
Stream.generate(new Inner()).limit(10).forEach(p -> System.out.println(p));
实际操作使用
使用friends.stream().collect(Collectors.toMap)将list转换为map
List<Friend> friends = new ArrayList<Friend>();
//将List<Friend> friends 转换为Map<Stirng,Friend>
friends.stream().collect(Collectors.toMap(a -> a.getName(), b -> b));
//将List<Friend> friends 转换为Map<Stirng,String>
friends.stream().collect(Collectors.toMap(a -> a.getName(), b -> b.getName()));