【问题标题】:List of unique elements, given a List of Lists给定列表列表的唯一元素列表
【发布时间】:2016-05-26 16:53:11
【问题描述】:

给定一个排序整数数组列表的数组列表transactions,我正在编写代码以返回其唯一元素。例如,给定

transactions = [
  [1, 1, 2, 3, 5, 8, 13, 21],
  [2, 3, 6, 10],
  [11, 21]
]

我的代码应该返回唯一的元素,保持排序顺序:

[1, 2, 3, 5, 6, 8, 10, 11, 13, 21]

为此,我只需将每个列表中的每个元素添加到 LinkedHashSet,根据它的定义,它会保持排序并删除重复项。

Set<Integer> uniqEl = new LinkedHashSet<>();

for (List<Integer> l : transactions) {
    for (Integer n : l) {
        uniqEl.add(n);
    }
}

虽然我的代码利用 Java 库完成了工作,但我想要一个更高效的实现。有什么更好的算法可以从列表列表中生成唯一元素的排序列表吗?

【问题讨论】:

  • 效率更高?所以你已经实际测量了,这才是你的应用程序的真正瓶颈? (旁注,为什么不使用TreeSet?)
  • uniqEl.addAll(l) 可能稍微更有效率。
  • 您的结果将是1, 2, 3, 5, 8, 13, 21, 6, 10, 11,这不是您想要的排序顺序。像@Tunaki 建议的那样使用TreeMap 会给你想要的结果。

标签: java sorting arraylist set duplicates


【解决方案1】:

如果“更高效”是指更精简,那么这种功能性方法可能会为您解决问题:

List<Integer> list =
transactions.stream()
            .flatMap(List::stream)
            .distinct()
            .sorted()
            .collect(Collectors.toList());

【讨论】:

  • 而不是l -&gt; l.stream()List::stream 方法引用会不会更精简?
【解决方案2】:

您将无法获得比使用TreeSet 并将所有列表添加到此集合中更有效的方法。 TreeSet 将按自然顺序升序对元素进行排序,并忽略重复项。

public static void main(String[] args) {
    List<List<Integer>> transactions = Arrays.asList(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21), Arrays.asList(2, 3, 6, 10), Arrays.asList(11, 21));

    SortedSet<Integer> set = new TreeSet<>();
    for (List<Integer> l : transactions) {
        set.addAll(l);
    }
}

当然,您可以使用 Java 8 Streams 来实现:

SortedSet<Integer> set = transactions.stream()
                                     .flatMap(List::stream)
                                     .collect(Collectors.toCollection(TreeSet::new));

使用此解决方案,您可以并行运行它,但您必须衡量它是否提高了性能。

【讨论】:

    【解决方案3】:

    对于Eclipse Collections,以下应该可以工作:

    MutableList<MutableList<Integer>> transactions =
        Lists.mutable.with(
            Lists.mutable.with(1, 1, 2, 3, 5, 8, 13, 21), 
            Lists.mutable.with(2, 3, 6, 10), 
            Lists.mutable.with(11, 21));
    SortedSet<Integer> flattenedDistinct =
        transactions.asLazy().flatCollect(l -> l).toSortedSet();
    
    Assert.assertEquals(
        SortedSets.mutable.of(1, 2, 3, 5, 6, 8, 10, 11, 13, 21), flattenedDistinct);
    

    您也可以通过使用带有目标集合的 Eager 版本的 flatCollect 来实现相同的结果,如下所示:

    SortedSet<Integer> flattenedDistinct =
        transactions.flatCollect(l -> l, SortedSets.mutable.empty());
    

    为避免整数装箱,您可以使用 Eclipse Collections 中提供的原始集合。

    MutableList<MutableIntList> transactions = Lists.mutable.with(
        IntLists.mutable.with(1, 1, 2, 3, 5, 8, 13, 21),
        IntLists.mutable.with(2, 3, 6, 10), 
        IntLists.mutable.with(11, 21));
    MutableIntList flattenedDistinct =
        transactions.injectInto(IntSets.mutable.empty(), MutableIntSet::withAll).toSortedList();
    
    Assert.assertEquals(
        IntLists.mutable.with(1, 2, 3, 5, 6, 8, 10, 11, 13, 21), flattenedDistinct);
    

    注意:我是 Eclipse Collections 的贡献者。

    【讨论】:

      猜你喜欢
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 2023-03-31
      • 1970-01-01
      • 2022-12-03
      相关资源
      最近更新 更多