【问题标题】:How to merge a few lists?如何合并几个列表?
【发布时间】:2018-03-18 13:43:35
【问题描述】:

如何组合三个列表:

  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}

在实际情况下,我需要从参与者列表(组)中获取一些匹配队列。在第一场比赛中,所有第一批参与者都来自不同的组。在第二场比赛中,所有第二名参与者都参加比赛,依此类推。而且我将无法解决它。

【问题讨论】:

  • 您是否尝试过在列表中使用 addAll(...) 方法?
  • @Lukas Werner 只是 adAll() 几次,Collection.sort 不正确。
  • 为什么合并的列表不是 String 而是 List>... 检查你的定义
  • 你能不能更精确一点。您究竟想如何合并/排序?
  • 您的预期结果中缺少“四个”?

标签: java collections merge


【解决方案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;
}

【讨论】:

  • 是的,这正是我所需要的。谢谢
【解决方案2】:

假设您要做的就是一次一个地从源列表中获取元素,然后将它们“编织”到其他列表中...... 递归解决方案:

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;
}

【讨论】:

    【解决方案3】:

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

    因此,您需要实现一个自定义的Comparator,它可以做任何想做的事情,并使用方法签名Collections.sort(list, yourCustomComparator)

    【讨论】:

      【解决方案4】:

      两点: - 您不能按数字对数字单词进行排序,以字符串形式编写。因此你必须做一些映射,例如使用 Map,然后按键排序,或者为排序方法编写 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;
      });
      

      【讨论】:

        【解决方案5】:

        您可以使用 Java 8 流轻松地将您的列表合并在一起,并将 flatMap() 合并为单个 ListString。按“一”、“二”、“三”等排序将需要自定义排序器,因为只需按 String.compareTo() 或任何其他自然排序进行排序就会对字符进行词法比较,而不是它们在英语中的含义。

        import 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.
                });
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2010-10-16
          • 1970-01-01
          • 1970-01-01
          • 2016-07-07
          • 2013-08-22
          • 2016-01-17
          • 2018-11-16
          • 1970-01-01
          • 2015-07-19
          相关资源
          最近更新 更多