JAVA8新特性之Stream
为什么要使用Stream
现在平常的工作中,对集合的各种操作是必不可少的。比方说对集合中元素进行遍历后取值某个值,对集合中的元素进行过滤,排序,求和等操作都涉及多层循环遍历,非常麻烦并且可读性差。有了Stream,这一切一条Stream的函数式编程就可以搞定了。
其中filter,map,collect三个用的最多
1.filter
filter对函数进行过滤,filter()接收一个Predicate,重写里面的test方法,返回true就保留下来,返回false就过滤掉。
public static void main(String[] args) {
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5, 6);
//过滤掉奇数
list1.stream().filter(a -> {
if (a%2 == 0) {
return true;
}
return false;
}).forEach(System.out::println);
//可以简写为
list1.stream().filter(a -> a%2 == 0).forEach(System.out::println);
}
2.map和flatmap
map的主要功能是取集合中的某个属性值,map()接收一个Function,重写里面的apply方法,有一个入参和一个出参。而flatmap的作用是对流进行偏平化。
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5,6);
//自身乘以2
List<Integer> result1 = list.stream().map(a -> a*2).collect(Collectors.toList());
System.out.println(result1);
}
3.flatmap
flatmap的意义在于,一般的java方法都是返回一个结果,但是对于结果数量不确定的时候,用map这种java方法的方式,是不太灵活的,所以引入了flatmap。 我个人认为,flatMap的可以处理更深层次的数据,入参为多个list,结果可以返回为一个list,而map是一对一的,入参是多个list,结果返回必须是多个list。通俗的说,如果入参都是对象,那么flatMap可以操作对象里面的对象,而map只能操作第一层。
public static void main(String[] args) {
String[] strings = {"Hello" , "World"};
//为了得到H e l l o W o r l d
//第一种方法种方法(不可行)
List<String[]> collect = Arrays.asList(strings).stream()
.map(s -> s.split(""))
.collect(Collectors.toList());
System.out.println(collect);
//第二种方法(扁平化处理)
List<String> collect1 = Arrays.asList(strings).stream()
.map(s -> s.split(""))
.flatMap(s -> Arrays.stream(s))
.collect(Collectors.toList());
System.out.println(collect1);
}
4.collect
collect可以说是用法最多的,常见几种形式
- toList()
- toSet()
- toMap()
- 自定义
- 分组
/**
* toList
*/
public static void toListTest(){
List<Person> data = Data.getData();
List<String> collect = data.stream()
.map(Person::getName)
.collect(Collectors.toList());
}
/**
* toSet
*/
public static void toSetTest(){
List<Person> data = Data.getData();
Set<String> collect = data.stream()
.map(Person::getName)
.collect(Collectors.toSet());
}
/**
* toMap
*/
public static void toMapTest(){
List<Person> data = Data.getData();
Map<String, Integer> collect = data.stream()
.collect(
Collectors.toMap(Person::getName, Person::getAge)
);
data.stream()
.collect(Collectors.toMap(p->p.getName(), value->{
return value+"1";
}));
}
/**
* 指定类型
*/
public static void toTreeSetTest(){
List<Person> data = Data.getData();
TreeSet<PersonModel> collect = data.stream()
.collect(Collectors.toCollection(TreeSet::new));
System.out.println(collect);
}
/**
* 分组
*/
public static void toGroupTest(){
List<Person> data = Data.getData();
Map<Boolean, List<Person>> collect = data.stream()
.collect(Collectors.groupingBy(p -> "男".equals(p.getSex())));
System.out.println(collect);
}
/**
* 分隔
*/
public static void toJoiningTest(){
List<Person> data = Data.getData();
String collect = data.stream()
.map(p -> p.getName())
.collect(Collectors.joining(",", "{", "}"));
System.out.println(collect);
}
总结:学会了Stream可以大大提高我们的开发效率,本人在平安从事JAVA开发工作,实际的开发中发现如果能熟练的使用
Stream的话能大大的减少代码量,不用写枯燥无味的for循环,提高工作效率。如果觉得有帮助的话麻烦关注一波!我
将后续谈谈Optional,再也不用担心会报空指针异常了。