Stream是Java 8的新特性,基于lambda表达式,是对集合对象功能的增强,它专注于对集合对象进行各种高效、方便聚合操作或者大批量的数据操作,提高了编程效率和代码可读性。Collectors通常在Stream处理后,返回转换成集合类时使用,本文主要介绍Java Stream中Collectors.toList()、Collectors.toSet()、Collectors.toCollection()和Collectors.toMap()的使用,以及相关的示例代码。

1、Collectors.toList()的使用

Collectors.toList()是将Stream转换成List,List为ArrayList,需要转换成其它,则需要使用Collectors.toCollection(),例如,

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
   public static class Person {
     int age;
     Person (int age) {
         this.age = age;
     }
     public int getAge() {
         return age;
     }
     public void setAge(int age) {
         this.age = age;
     }

  }
public static void main(String[] args) {

    List<Person> pList = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55));
    List<Person> result = pList.stream()
                                .map(o -> {
                            o.setAge(18);
                            return o;
                        }).collect(Collectors.toList());

    System.out.println("stream() result :"+result.getClass());
    System.exit(0); //success
  }
}

2、Collectors.toSet()的使用

Collectors.toSet()是将Stream转换成Set,Set为HashSet,需要转换成其它,则也需要使用Collectors.toCollection(),例如,

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
   public static class Person {
     int age;
     Person (int age) {
         this.age = age;
     }
     public int getAge() {
         return age;
     }
     public void setAge(int age) {
         this.age = age;
     }

  }
public static void main(String[] args) {

    List<Person> pList = Arrays.asList(new Person(11),new Person(22), new Person(33), new Person(44), new Person(55));
    Set<Person> setResult = pList.stream()
                                .map(o -> {
                            o.setAge(18);
                            return o;
                        }).collect(Collectors.toSet());

    System.out.println("stream() result :"+setResult.getClass());
    System.exit(0); //success
  }
}

3、Collectors.toCollection()的使用

toList()toSet()使用时,需要转换成指定的集合类型,则可以使用Collectors.toCollection(),如下,

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
   public static class Person {
     int age;
     String name;
     Person (int age,String name) {
         this.age = age;
         this.name = name;
     }
     public int getAge() {
         return age;
     }
     public void setAge(int age) {
         this.age = age;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }

  }
public static void main(String[] args) {

    List<Person> pList = Arrays.asList(new Person(11,"mike"),new Person(22,"lynn"), new Person(33,"john"), new Person(44,"mickey"), new Person(55,"fiona"));
    List<Person> listResult = pList.stream().collect(Collectors.toCollection(LinkedList::new));
    System.out.println("stream() result :"+listResult.getClass());
    System.exit(0); //success
  }
}

4、Collectors.toMap()的使用

Collectors.toMap()是将Stream转换成Map,Map为HashMap,需要转换成其它,则也需要使用Collectors.toCollection()

例如,

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
   public static class Person {
     int age;
     String name;
     Person (int age,String name) {
         this.age = age;
         this.name = name;
     }
     public int getAge() {
         return age;
     }
     public void setAge(int age) {
         this.age = age;
     }
     public String getName() {
         return name;
     }
     public void setName(String name) {
         this.name = name;
     }

  }
public static void main(String[] args) {

    List<Person> pList = Arrays.asList(new Person(11,"mike"),new Person(22,"lynn"), new Person(33,"john"), new Person(44,"mickey"), new Person(55,"fiona"), new Person(31,"fiona"));
    //(oldValue, newValue) -> newValue是重复key时,使用的值,可以指定
    Map<String, Integer> map = pList.stream().collect(Collectors.toMap(Person::getName, Person::getAge,(oldValue, newValue) -> newValue));
    System.out.println("stream() result :"+map);
    System.out.println("stream() result :"+map.getClass());
    //指定转换成集合类型LinkedHashMap
    Map<String, Integer> map1 = pList.stream().collect(Collectors.toMap(Person::getName, Person::getAge,(oldValue, newValue) -> newValue,LinkedHashMap::new));
    System.out.println("stream() result :"+map1);
    System.out.println("stream() result :"+map1.getClass());
    System.exit(0); //success
  }
}

推荐文档

相关文档

大家感兴趣的内容

随机列表