Stream流中的常用方法concat

201 阅读1分钟

concat方法的作用是将两个流的数据合并为一个流。 实例代码:

public static void main(String[] args) {
    UserVo userVo = new UserVo();
    userVo.setMobile("151");
    userVo.setFullName("中国移动");
    List<UserVo> list1 = Arrays.asList(userVo);
    Map<String, String> map1 = list1.stream().collect(Collectors.toMap(UserVo::getMobile, UserVo::getFullName));
    System.out.println("map1:"+map1);
    UserVo userVo1 = new UserVo();
    userVo1.setMobile("131");
    userVo1.setFullName("中国联通");
    List<UserVo> list2 = Arrays.asList(userVo1);
    Map<String, String> map2 = list2.stream().collect(Collectors.toMap(UserVo::getMobile, UserVo::getFullName));
    System.out.println("map2:"+map2);
    Map<String, String> map = Stream.concat(map1.entrySet().stream(), map2.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    System.out.println("map:"+map);
}

image.png