当前位置:网站首页>[List merge] Combine multiple lists into one list

[List merge] Combine multiple lists into one list

2022-08-10 12:32:00 Tan Yue sword refers to the big factory

多个list合并为一个list

/** * 多个list合并 * * @author : kwan * @version : 1.0.0 * @date : 2022/8/9 10:33 */
public class Java8_48_stream_merge_01 {
    
  public static void main(String[] args) {
    
    List<Person> personList = new ArrayList<>();
    personList.add(new Person("Sherry", 9000, 24, "female", "New York"));
    personList.add(new Person("Tom", 8900, 22, "male", "Washington"));
    personList.add(new Person("Jack", 9000, 25, "male", "Washington"));
    personList.add(new Person("Lily", 8800, 26, "male", "New York"));
    personList.add(new Person("Alisa", 9000, 26, "female", "New York"));
    List<Person> personList2 = new ArrayList<>();
    personList2.add(new Person("Sherry", 9000, 24, "female", "New York"));
    personList2.add(new Person("Tom", 8900, 22, "male", "Washington"));
    personList2.add(new Person("Jack", 9000, 25, "male", "Washington"));
    personList2.add(new Person("Lily", 8800, 26, "male", "New York"));
    personList2.add(new Person("Alisa", 9000, 26, "female", "New York"));
    List<List<Person>> list = new ArrayList<>();
    list.add(personList);
    list.add(personList2);
    List<Person> persons = merge(list);
    System.out.println(JSONArray.toJSONString(persons));
  }
  private static List<Person> merge(List<List<Person>> list) {
    
    List<Person> persons = new ArrayList<>();
    for (List<Person> people : list) {
    
      for (Person person : people) {
    
        persons.add(person);
      }
    }
    return persons;
  }
}
/** * 多个list合并 * * @author : kwan * @version : 1.0.0 * @date : 2022/8/9 10:33 */
public class Java8_48_stream_merge_02 {
    
    public static void main(String[] args) {
    
        List<Integer> list = ImmutableList.of(1, 3, 5);
        list = list.stream().flatMap(l -> {
    
            List<Integer> list1 = new ArrayList<>();
            list1.add(l + 1);
            list1.add(l + 2);
            return list1.stream();
        }).collect(Collectors.toList());
        System.out.println(list);// [2, 3, 4, 5, 6, 7]
    }
}
/** * 多个list合并 * * @author : kwan * @version : 1.0.0 * @date : 2022/8/9 10:33 */
public class Java8_48_stream_merge_03 {
    
    public static void main(String[] args) {
    
        List<List<Map<String, Object>>> lists = ImmutableList.of(
                ImmutableList.of(
                        ImmutableMap.of("a", 1, "b", 2), ImmutableMap.of("a", 2, "b", 3)
                ),
                ImmutableList.of(
                        ImmutableMap.of("a", 3, "b", 4), ImmutableMap.of("a", 4, "b", 5)
                ),
                ImmutableList.of(
                        ImmutableMap.of("a", 5, "b", 6), ImmutableMap.of("a", 6, "b", 7)
                )
        );
        // 将多个list合并为一个list
        List<Map<String, Object>> list = lists.stream().flatMap(Collection::stream).collect(Collectors.toList());
        System.out.println(JSONArray.toJSONString(list));
    }
}
原网站

版权声明
本文为[Tan Yue sword refers to the big factory]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101140120307.html