删除 ArrayList> 中的重复行

发布于 2024-09-09 03:25:32 字数 525 浏览 2 评论 0 原文

public void removeDuplicates (ArrayList<ArrayList<String>> strings) {

    Set<ArrayList<String>> s = new LinkedHashSet<ArrayList<String>>(strings);
    strings =  new ArrayList<ArrayList<String>>(s);
}

我想删除 ArrayList> 中的重复行,我想使用 LinkedHashSet 并比较 ArrayList 之间的情况,如果是同样不插入。我知道我需要 Comparator 但我不知道如何实现它来比较 ArrayList 内的 ArrayList

谢谢

public void removeDuplicates (ArrayList<ArrayList<String>> strings) {

    Set<ArrayList<String>> s = new LinkedHashSet<ArrayList<String>>(strings);
    strings =  new ArrayList<ArrayList<String>>(s);
}

i want to remove duplicated lines in ArrayList<ArrayList<String>>, I want to use LinkedHashSet and compare ArrayList between themselves and if is the same not insert it. i know i need Comparator but i do not know how to implement it for comparing ArrayList inside ArrayList.

Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

久而酒知 2024-09-16 03:25:32

您可以尝试使用 Set<列表> - 如果您实现自己的 List 或扩展 ArrayList 操作系统,那么“equals()”将按您期望的方式运行。

因此,当您添加 arrayList 时,它将自动与其余的进行比较。

不确定先添加 arrayList 然后用元素填充它们时会发生什么。

你的情况是什么?


您说您想使用 LinkedHashSet - 好的,这与我最初的建议相同。
(你不需要比较器)
您可以扩展 ArrayList 并使其“equals()”方法符合您的愿望:

  • 相同数量的元素
  • 相同的元素
  • 等等......

You could try using a Set< List > - and if you implement your own List or extend ArrayList os that the "equals()" acts the way you expect.

So when you add an arrayList it will automatically be compared with the rest.

Not sure what will happen when you add the arrayLists first and then populate them with elements.

What is your case?


You said you want to use LinkedHashSet - ok, that is the same as my initial suggestion.
(you don't need comparator)
You can extend ArrayList and make its "equals()" method conform your wish:

  • same number of elements
  • same elements
  • etc...
濫情▎り 2024-09-16 03:25:32

您没有提到订单是否重要。

如果内部列表中的项目顺序不重要,则这应该有效:-

List<List<String>> list = new ArrayList<List<String>>();
list.add(Arrays.asList(new String[] {
        "a",
        "b",
        "c"
}));
list.add(Arrays.asList(new String[] {
        "b",
        "a",
        "c"
}));
list.add(Arrays.asList(new String[] {
        "a",
        "b",
        "c",
        "d"
}));

// use set to remove duplicates
Set<Set<String>> set = new HashSet<Set<String>>();
for (List<String> innerList : list) {
    set.add(new HashSet<String>(innerList));
}

// convert back to list
List<List<String>> noDupList = new ArrayList<List<String>>();
for (Set<String> innerSet : set) {
    noDupList.add(new ArrayList<String>(innerSet));
}

// print out for debugging
for (List<String> l : noDupList) {
    System.out.println(l);
}

You didn't mention if the order is important.

If the item order in the inner list is not important, this should work:-

List<List<String>> list = new ArrayList<List<String>>();
list.add(Arrays.asList(new String[] {
        "a",
        "b",
        "c"
}));
list.add(Arrays.asList(new String[] {
        "b",
        "a",
        "c"
}));
list.add(Arrays.asList(new String[] {
        "a",
        "b",
        "c",
        "d"
}));

// use set to remove duplicates
Set<Set<String>> set = new HashSet<Set<String>>();
for (List<String> innerList : list) {
    set.add(new HashSet<String>(innerList));
}

// convert back to list
List<List<String>> noDupList = new ArrayList<List<String>>();
for (Set<String> innerSet : set) {
    noDupList.add(new ArrayList<String>(innerSet));
}

// print out for debugging
for (List<String> l : noDupList) {
    System.out.println(l);
}
〆一缕阳光ご 2024-09-16 03:25:32

您将必须进行强力比较,并将每个数组列表与其他每个数组列表进行比较。

并将其元素与索引 1-n 进行比较

从索引 0 开始,一旦找到一个遵循伪代码的假设 ArrayList myDatastruct

for (int i =0; i < myDatastructure.count() ; i++){

    for (int j = i+1 ; i< mydatastructure.count() ; j++){
      compare(myDatastructure.get(i),myDataStructure.get(j));

    }
}

:对于比较方法,您将需要编写一个 for 循环,逐一迭代比较每个索引处的项目在两个 arrayList 中,如果它们的长度不同,您将希望通过不费心比较它们来短路这一点。

您可能希望在单独的数组列表中标记要删除的索引,并在单独的循环中删除它们,否则您会搞砸索引。

实施应该相当简单,因此留作练习。

You are going to have to do a brute force compare and compare each and every arraylist against every other array list.

start with index 0 and compare it's elements against index 1-n once you find an

Assumeing ArrayList myDatastructure following psuedo code:

for (int i =0; i < myDatastructure.count() ; i++){

    for (int j = i+1 ; i< mydatastructure.count() ; j++){
      compare(myDatastructure.get(i),myDataStructure.get(j));

    }
}

for the compare method you will want to write a for loop that iterates through the elements one by one comparing the item at each index in both arrayLists, you will want to short circuit this by not bothering to compare them if they are not the same length.

You will probably want to mark the indexes that you want to remove in an Separate arraylist and remove them in a separate loop otherwise you will screw up your indices.

Implementation should be fairly simple, so that's left as an exercise.

十雾 2024-09-16 03:25:32

假设该列表包含

 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

您期望该集合包含什么?两个元素?一种元素?

你能说出你想如何比较 ArrayList 吗?

如果这是一对一的比较 - “如果它们以相同顺序包含相同元素,那么它们就相等”,那么

sets = new HashSet<ArrayList<String>>(lists);

应该足够了。

如果您想要更复杂的比较规则,那么您需要重写 equals 方法,如 Leni Kirilov 上面所说。但列表越大,性能开销就会越高。

Say the list contains

 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
 [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

What do you expect the set to contain? Two elements? One element?

Can you tell how you want to compare the ArrayLists?

If that is the one-to-one compare - "they are equal if they contain the same elements in the same order" then

sets = new HashSet<ArrayList<String>>(lists);

Should be sufficient.

If you want to have more complex comparison rules then you need to override the equals method as Leni Kirilov said above. But the larger the list is, you will have a high performance overhead.

勿挽旧人 2024-09-16 03:25:32

同意上面的@Leni,只需重写 ArrayLists 的 equals 方法,并且 LinkedHashSet 实现应该自动过滤重复项。

来自 Java 文档:不包含重复元素的集合。更正式地讲,集合不包含使 e1.equals(e2) 的一对元素 e1 和 e2,并且最多包含一个空元素。正如其名称所暗示的,该接口对数学集合抽象进行建模

Agree with @Leni above, just override ArrayLists's equals method and the LinkedHashSet implementation should automatically filter duplicates.

From Java doc: A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文