How do I join two lists in Java?

后端 未结 30 1746
旧巷少年郎
旧巷少年郎 2020-11-22 14:36

Conditions: do not modifiy the original lists; JDK only, no external libraries. Bonus points for a one-liner or a JDK 1.3 version.

Is there a simpler way than:

相关标签:
30条回答
  • 2020-11-22 15:11

    This is simple and just one line, but will add the contents of listTwo to listOne. Do you really need to put the contents in a third list?

    Collections.addAll(listOne, listTwo.toArray());
    
    0 讨论(0)
  • 2020-11-22 15:11

    You could do it with a static import and a helper class

    nb the generification of this class could probably be improved

    public class Lists {
    
       private Lists() { } // can't be instantiated
    
       public static List<T> join(List<T>... lists) {
          List<T> result = new ArrayList<T>();
          for(List<T> list : lists) {
             result.addAll(list);
          }
          return results;
       }
    
    }
    

    Then you can do things like

    import static Lists.join;
    List<T> result = join(list1, list2, list3, list4);
    
    0 讨论(0)
  • 2020-11-22 15:11

    Use a Helper class.

    I suggest:

    public static <E> Collection<E> addAll(Collection<E> dest, Collection<? extends E>... src) {
        for(Collection<? extends E> c : src) {
            dest.addAll(c);
        }
    
        return dest;
    }
    
    public static void main(String[] args) {
        System.out.println(addAll(new ArrayList<Object>(), Arrays.asList(1,2,3), Arrays.asList("a", "b", "c")));
    
        // does not compile
        // System.out.println(addAll(new ArrayList<Integer>(), Arrays.asList(1,2,3), Arrays.asList("a", "b", "c")));
    
        System.out.println(addAll(new ArrayList<Integer>(), Arrays.asList(1,2,3), Arrays.asList(4, 5, 6)));
    }
    
    0 讨论(0)
  • 2020-11-22 15:12

    No way near one-liner, but I think this is the simplest:

    List<String> newList = new ArrayList<String>(l1);
    newList.addAll(l2);
    
    for(String w:newList)
            System.out.printf("%s ", w);
    
    0 讨论(0)
  • 2020-11-22 15:13

    You could use the Apache commons-collections library:

    List<String> newList = ListUtils.union(list1, list2);
    
    0 讨论(0)
  • 2020-11-22 15:13

    Here's an approach using streams and java 8 if your lists have different types and you want to combine them to a list of another type.

    public static void main(String[] args) {
        List<String> list2 = new ArrayList<>();
        List<Pair<Integer, String>> list1 = new ArrayList<>();
    
        list2.add("asd");
        list2.add("asdaf");
        list1.add(new Pair<>(1, "werwe"));
        list1.add(new Pair<>(2, "tyutyu"));
    
        Stream stream = Stream.concat(list1.stream(), list2.stream());
    
        List<Pair<Integer, String>> res = (List<Pair<Integer, String>>) stream
                .map(item -> {
                    if (item instanceof String) {
                        return new Pair<>(0, item);
                    }
                    else {
                        return new Pair<>(((Pair<Integer, String>)item).getKey(), ((Pair<Integer, String>)item).getValue());
                    }
                })
                .collect(Collectors.toList());
    }
    
    0 讨论(0)
提交回复
热议问题