如何合并几个列表?

时间:2017-10-06 17:28:23

标签: java collections merge

如何组合三个列表:

  List<String> one = Arrays.asList("one","four","seven");
    List<String> two = Arrays.asList("two","five","eight");
    List<String> three = Arrays.asList("three","six");

    List<List<String>> merged = ...;

要获得结果:

List {one, two, three, four, five , six , seven, eight}

在实际情况中,我需要从参与者的列表(组)中获得一些匹配队列。在第一场比赛中,所有来自不同组的参赛者都参加比赛在第二场比赛中发挥所有第二参与者等等。我无法解决这个问题。

5 个答案:

答案 0 :(得分:3)

您可以使用Java 8流轻松地将列表合并在一起,并flatMap()将其合并到List Stringimport java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Merge { public static void main(String[] args) { List<String> one = Arrays.asList("one","four","seven"); List<String> two = Arrays.asList("two","five","eight"); List<String> three = Arrays.asList("three","six"); List<List<String>> merged = Arrays.asList(one, two, three); System.out.println(merged); // Output -> [[one, four, seven], [two, five, eight], [three, six]] List<String> flatMapped = merged.stream().flatMap(List::stream).collect(Collectors.toList()); System.out.println(flatMapped); // Output -> [one, four, seven, two, five, eight, three, six] flatMapped.sort((o1, o2) -> { // TODO - implement sorting logic to return "one", "two", "three", etc. }); } } 。按“一”,“二”,“三”等排序将需要自定义排序器,因为只需按String.compareTo()或任何其他自然排序进行排序将对字符进行词汇比较,而不是英文的含义。

StringJoiner

答案 1 :(得分:3)

两点: - 您不能按数字对字符串中的数字进行排序。因此,您必须进行一些映射,例如使用Map,然后按键排序或者为sort方法编写Comparator - Arrays.asList(...)生成一个不可修改的列表,你不能添加一些东西

所以,这对我有用:

List<String> one = new ArrayList<>(Arrays.asList("one", "four", "seven"));
List<String> two = new ArrayList<>(Arrays.asList("two", "five", "eight"));
List<String> three = new ArrayList<>(Arrays.asList("three", "six", "ten"));

one.addAll(two);
one.addAll(three);

one.sort((o1, o2) -> {
    // Declare here, which value is less, equal or greater than the other

    return 0;
});

答案 2 :(得分:2)

默认情况下,当您对String列表进行排序时,它们将按字典顺序排序(az,0-9等),因此您不能指望{{1}按照您期望的顺序对Collections.sort列表进行排序。

因此,您需要实现可以执行所需操作的自定义String,并使用方法签名Comparator

答案 3 :(得分:1)

如果您只想在不改变顺序的情况下组合列表,那么您可以执行以下操作:

public static void main(String[] args) {
    List<String> one = Arrays.asList("one","four","seven");
    List<String> two = Arrays.asList("two","five","eight");
    List<String> three = Arrays.asList("three","six");

    System.out.println(zipLists(one, two, three)); 
    //[one, two, three, four, five, six, seven, eight]
}

public static List<String> zipLists(List<String>... lists) {
    int maxSize = 0, totalSize = 0;
    List<Iterator<String>> iterators = new ArrayList<>(lists.length);
    for(List<String> list: lists) {
        int size = list.size();
        maxSize = Math.max(maxSize, size);
        totalSize += size;
        iterators.add(list.iterator());
    }
    List<String> mergedList = new ArrayList<>(totalSize);
    for(int i = 0; i < maxSize; i++) {
        for(Iterator<String> iterator: iterators) {
            if(iterator.hasNext()) {
                mergedList.add(iterator.next());
            }
        }
    }
    return mergedList;
}

答案 4 :(得分:0)

假设您要做的就是一次一个地从源列表中获取元素并编织&#39;他们与其他名单... 递归解决方案:

public static List<String> MergeLists (List<List<String>> a2 ) {
    List<String> h = new ArrayList<String>();
    List<List<String>> a = new ArrayList<List<String>>(); 
    boolean call = false;
    for (List<String> l : a2) {
        if (! l.isEmpty()) {
            h.add(l.get(0));
            if (l.size() > 0) {
                a.add((List<String>) l.subList(1, l.size()));
                call = true;
            }
        }
    }
    if (call) { h.addAll(MergeLists(a)); return h;}
    else return h;
}