Java8 新特性之集合操做Stream

Java8 新特性之集合操做Streamjava

 

 

Stream简介

 

  • Java 8引入了全新的Stream API。这里的Stream和I/O流不一样,它更像具备Iterable的集合类,但行为和集合类又有所不一样。
  • stream是对集合对象功能的加强,它专一于对集合对象进行各类很是便利、高效的聚合操做,或者大批量数据操做。

为何要使用Stream

  • 函数式编程带来的好处尤其明显。这种代码更多地表达了业务逻辑的意图,而不是它的实现机制。易读的代码也易于维护、更可靠、更不容易出错。
  • 高端

 

使用实例:编程

测试数据:

public class Data { private static List<PersonModel> list = null; static { PersonModel wu = new PersonModel("wu qi", 18, "男"); PersonModel zhang = new PersonModel("zhang san", 19, "男"); PersonModel wang = new PersonModel("wang si", 20, "女"); PersonModel zhao = new PersonModel("zhao wu", 20, "男"); PersonModel chen = new PersonModel("chen liu", 21, "男"); list = Arrays.asList(wu, zhang, wang, zhao, chen); } public static List<PersonModel> getData() { return list; } } 

 

Filter

  • 遍历数据并检查其中的元素时使用。
  • filter接受一个函数做为参数,该函数用Lambda表达式表示。
保留年龄为 20 的 person 元素 list = list.stream() .filter(person -> person.getAge() == 20) .collect(toList()); 打印输出 [Person{name='jack', age=20}]

 

/** * 过滤全部的男性 */
    public static void fiterSex(){ List<PersonModel> data = Data.getData(); //old
        List<PersonModel> temp=new ArrayList<>(); for (PersonModel person:data) { if ("男".equals(person.getSex())){ temp.add(person); } } System.out.println(temp); //new
        List<PersonModel> collect = data .stream() .filter(person -> "男".equals(person.getSex())) .collect(toList()); System.out.println(collect); } /** * 过滤全部的男性 而且小于20岁 */
    public static void fiterSexAndAge(){ List<PersonModel> data = Data.getData(); //old
        List<PersonModel> temp=new ArrayList<>(); for (PersonModel person:data) { if ("男".equals(person.getSex())&&person.getAge()<20){ temp.add(person); } } //new 1
        List<PersonModel> collect = data .stream() .filter(person -> { if ("男".equals(person.getSex())&&person.getAge()<20){ return true; } return false; }) .collect(toList()); //new 2
        List<PersonModel> collect1 = data .stream() .filter(person -> ("男".equals(person.getSex())&&person.getAge()<20)) .collect(toList()); }

 distinct()

去除重复元素,这个方法是经过类的 equals 方法来判断两个元素是否相等的api

如例子中的 Person 类,须要先定义好 equals 方法,否则相似[Person{name='jack', age=20}, Person{name='jack', age=20}] 这样的状况是不会处理的数据结构

 参考:https://blog.csdn.net/haiyoung/article/details/80934467

 

 

limit(long n)

 limit: 对一个Stream进行截断操做,获取其前N个元素,若是原Stream中包含的元素个数小于N,那就获取其全部的元素;app

返回前 n 个元素 list = list.stream() .limit(2) .collect(toList()); 打印输出 [Person{name='jack', age=20}, Person{name='mike', age=25}]

 

skip方法 :
skip: 返回一个丢弃原Stream的前N个元素后剩下元素组成的新Stream,若是原Stream中包含的元素个数小于N,那么返回空Stream;ide

 

Map

  • map生成的是个一对一映射,for的做用
  • 比较经常使用

 

/** * 取出全部的用户名字 */
    public static void getUserNameList(){ List<PersonModel> data = Data.getData(); //old
        List<String> list=new ArrayList<>(); for (PersonModel persion:data) { list.add(persion.getName()); } System.out.println(list); //new 1
        List<String> collect = data.stream().map(person -> person.getName()).collect(toList()); System.out.println(collect); //new 2
        List<String> collect1 = data.stream().map(PersonModel::getName).collect(toList()); System.out.println(collect1); //new 3
        List<String> collect2 = data.stream().map(person -> { System.out.println(person.getName()); return person.getName(); }).collect(toList()); }

 

filter 与 map 同时使用: 函数式编程

List<String> collect = users.stream().filter(item -> { if (!StringUtils.isEmpty(item.getUserName())) { return true; } return false; }).map(item -> item.getUserName()).collect(Collectors.toList());

 

 

FlatMap

  • 顾名思义,跟map差很少,更深层次的操做函数

  • 但仍是有区别的测试

  • map和flat返回值不一样spa

  • Map 每一个输入元素,都按照规则转换成为另一个元素。
    还有一些场景,是一对多映射关系的,这时须要 flatMap。

  • Map一对一

  • Flatmap一对多

  • map和flatMap的方法声明是不同的

    • <r> Stream<r> map(Function mapper);
    • <r> Stream<r> flatMap(Function> mapper);
  • map和flatMap的区别:我我的认为,flatMap的能够处理更深层次的数据,入参为多个list,结果能够返回为一个list,而map是一对一的,入参是多个list,结果返回必须是多个list。通俗的说,若是入参都是对象,那么flatMap能够操做对象里面的对象,而map只能操做第一层。

 
public static void flatMapString() { List<PersonModel> data = Data.getData(); //返回类型不同
        List<String> collect = data.stream() .flatMap(person -> Arrays.stream(person.getName().split(" "))).collect(toList()); List<Stream<String>> collect1 = data.stream() .map(person -> Arrays.stream(person.getName().split(" "))).collect(toList()); //用map实现
        List<String> collect2 = data.stream() .map(person -> person.getName().split(" ")) .flatMap(Arrays::stream).collect(toList()); //另外一种方式
        List<String> collect3 = data.stream() .map(person -> person.getName().split(" ")) .flatMap(str -> Arrays.asList(str).stream()).collect(toList()); }

 map转list:

Map<String, List<ProgrammeResult>> projectGroups = programmeResults.stream().collect(Collectors.groupingBy(ProgrammeResult::getProjectId)); List<ProgrammeResult> fhSuccessResult = projectGroups.entrySet().stream().flatMap(item -> item.getValue().stream()).collect(Collectors.toList());

 

 

Collect

  • collect在流中生成列表,map,等经常使用的数据结构
  • toList()
  • toSet()
  • toMap()

/** * toList */
    public static void toListTest(){ List<PersonModel> data = Data.getData(); List<String> collect = data.stream() .map(PersonModel::getName) .collect(Collectors.toList()); } /** * toSet */
    public static void toSetTest(){ List<PersonModel> data = Data.getData(); Set<String> collect = data.stream() .map(PersonModel::getName) .collect(Collectors.toSet()); } /** * toMap */
    public static void toMapTest(){ List<PersonModel> data = Data.getData(); Map<String, Integer> collect = data.stream() .collect( Collectors.toMap(PersonModel::getName, PersonModel::getAge) ); data.stream() .collect(Collectors.toMap(per->per.getName(), value->{ return value+"1"; })); } /** * 指定类型 */
    public static void toTreeSetTest(){ List<PersonModel> data = Data.getData(); TreeSet<PersonModel> collect = data.stream() .collect(Collectors.toCollection(TreeSet::new)); System.out.println(collect); } /** * 分组 */
    public static void toGroupTest(){ List<PersonModel> data = Data.getData(); Map<Boolean, List<PersonModel>> collect = data.stream() .collect(Collectors.groupingBy(per -> "男".equals(per.getSex()))); System.out.println(collect); } /** * 分隔 */
    public static void toJoiningTest(){ List<PersonModel> data = Data.getData(); String collect = data.stream() .map(personModel -> personModel.getName()) .collect(Collectors.joining(",", "{", "}")); System.out.println(collect); }

 

groupingBy 分组

groupingBy 用于将数据分组,最终返回一个 Map 类型

Map<Integer, List<Person>> map = list.stream().collect(groupingBy(Person::getAge));

 

例子中咱们按照年龄 age 分组,每个 Person 对象中年龄相同的归为一组

另外能够看出,Person::getAge 决定 Map 的键(Integer 类型),list 类型决定 Map 的值(List)

 

2.收集对象实体自己
- 在开发过程当中咱们也须要有时候对本身的list中的实体按照其中的一个字段进行分组(好比 id ->List),这时候要设置map的value值是实体自己。

 

public Map<Long, Account> getIdAccountMap(List<Account> accounts) { return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account)); }

 

account -> account是一个返回自己的lambda表达式,其实还可使用Function接口中的一个默认方法 Function.identity(),这个方法返回自身对象,更加简洁

重复key的状况。
在list转为map时,做为key的值有可能重复,这时候流的处理会抛出个异常:Java.lang.IllegalStateException:Duplicate key。这时候就要在toMap方法中指定当key冲突时key的选择。(这里是选择第二个key覆盖第一个key)

public Map<String, Account> getNameAccountMap(List<Account> accounts) { return accounts.stream().collect(Collectors.toMap(Account::getUsername, Function.identity(), (key1, key2) -> key2)); }

 

分组后统计每一个组的数量:

Map<Integer, Long> items = list.stream().collect(Collectors.groupingBy(User::getUserName,Collectors.counting())); 

  

 

多级分组

groupingBy 能够接受一个第二参数实现多级分组:

 

Map<Integer, Map<T, List<Person>>> map = list.stream().collect(groupingBy(Person::getAge, groupBy(...)));

 

partitioningBy 分区

分区与分组的区别在于,分区是按照 true 和 false 来分的,所以partitioningBy 接受的参数的 lambda 也是 T -> boolean

根据年龄是否小于等于20来分区 Map<Boolean, List<Person>> map = list.stream() .collect(partitioningBy(p -> p.getAge() <= 20)); 打印输出 { false=[Person{name='mike', age=25}, Person{name='tom', age=30}], true=[Person{name='jack', age=20}] }

 

【统计】

 List<User> users = User.getUsers(); int sum = users.stream().mapToInt(User::getUserAge).sum();//求和 System.out.println("sum==" + sum); int max = users.stream().mapToInt(User::getUserAge).max().getAsInt();//最大 System.out.println("max==" + max); int min = users.stream().mapToInt(User::getUserAge).min().getAsInt();//最小 System.out.println("min==" + min); Double average = users.stream().mapToInt(User::getUserAge).average().getAsDouble();//平均值 System.out.println("average==" + average); long count = users.stream().mapToInt(User::getUserAge).count(); // 获得元素个数 System.out.println("count===" + count);

 

【参数匹配】

 // allMatch 检测是否所有知足指定的参数行为 boolean b = users.stream().allMatch(User->User.getUserAge()>5); System.out.println("allMatch,检测是否所有都知足指定的参数行为:"+b); // anyMatch 检测是否存在一个或者多个知足指定的参数行为 boolean any = users.stream().anyMatch(User->User.getUserAge()>5); System.out.println("anyMatch,检测是否存在一个或多个知足指定的参数行为:"+any); // nonMatch 检测是否不存在知足指定行为的元素 boolean non = users.stream().noneMatch(User->User.getUserAge()>5); System.out.println("检测是否不存在知足指定行为的元素:"+non);
 
 

 







 

参考博客:

https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/

https://www.jianshu.com/p/9fe8632d0bc2

https://cloud.tencent.com/developer/article/1187833

https://www.concretepage.com/java/jdk-8/java-8-distinct-example

相关文章
相关标签/搜索