简体   繁体   English

根据自定义对象属性从arraylist中删除元素

[英]removing element from arraylist depending on a custom object property

I have an ArrayList with custom objects. 我有一个带有自定义对象的ArrayList。 What i want is to remove the duplicates from the array depending on the name property of the custom object. 我想要的是从数组中删除重复项,具体取决于自定义对象的name属性。 I have tried to accomplish this with Set person = new TreeSet(); 我试图使用Set person = new TreeSet(); but it's not working. 但它不起作用。 I guess because the set is comparing addresses or something else than the name property. 我猜因为该集合比较地址或其他名称属性。 So i'm now trying to use an iterator which is not removing the duplicates either. 所以我现在正在尝试使用一个迭代器,它不会删除重复项。 This is what i got; 这就是我得到的;

ArrayList<Person> people = new ArrayList<Person>();
Iterator<Person> iterator = people.iterator();
while (iterator.hasNext()) {
   Person person = iterator.next();

   if (person.getName().equals(iterator.next().getName())) {
       iterator.remove();
   }
}
for (Person person : people) {
    System.out.println(person.getName());
}

The ArrayList is not being modified though i see duplicates in it. 虽然我看到了重复的ArrayList,但它没有被修改。 I need some help. 我需要一些帮助。 Thanks! 谢谢!

I had the same situation, and I came up with this solution to use SortedSet . 我遇到了同样的情况,我想出了使用SortedSet解决方案。 In this case, those objects which cause set's comparator to return 0, will only be inserted once in the Set. 在这种情况下,那些导致set的比较器返回0的对象只会在Set中插入一次。

Here is an example: 这是一个例子:

SortedSet<Person> persons = new TreeSet<Person>(new Comparator<Person>() {
    @Override
    public int compare(Person arg0, Person arg1) {
        return arg0.getName().compareTo(arg1.getName());
    }
});

And now if you insert a Person into your persons , the duplicates (based on their name property) will not be inserted. 现在,如果您将Person插入您的persons ,则不会插入重复项(基于其name属性)。

So you can iterator over your list<Person> and insert every item of it into your persons set, and be sure that you will not have any duplicates. 因此,您可以对list<Person>进行迭代,并将其中的每个项目插入到您的persons集中,并确保您不会有任何重复项。 So the rest would be like: 所以剩下的就像:

Iterator<Person> iterator = people.iterator();
while(iterator.hasNext()) {
    persons.add(iterator.next());
}
people.clear();
people.addAll(persons); //Now, your people does not contain duplicate names

Your code is currently broken because you only compare objects with the next object in the list. 您的代码当前已损坏,因为您只将对象与列表中的下一个对象进行比较。 To correct your current approach, you would need to have another sub-loop which compares each object with all others in the list. 要纠正当前的方法,您需要有另一个子循环,将每个对象与列表中的所有其他对象进行比较。 That might get messy with nested iterators. 使用嵌套迭代器可能会变得混乱。

One alternative is to define a new list and populate it with items once you've confirmed they are not duplicates. 另一种方法是定义一个新列表,并在确认它们不是重复项后用项目填充它。 This avoids nesting iterators. 这避免了嵌套迭代器。

Finally, another option would be to define an equals method that compares based on this property and throw the objects in a Set . 最后,另一个选项是定义一个equals方法,该方法基于此属性进行比较并将对象抛出到Set Don't forget hashCode too. 不要忘记hashCode

Its not removing because you are only comparing each element with the next element. 它不会被删除,因为您只是将每个元素与下一个元素进行比较。 You can store the names in a HashSet which can hold only 1 of each String then remove the item if its name is already in the set. 您可以将名称存储在HashSet中,该HashSet只能容纳每个String中的1个,如果其名称已在该集合中,则删除该项目。

HashSet<String> seen = new HashSet<String>();
while (iterator.hasNext()) {
     Person p = iterator.next();
     if (seen.contains(p.getName())) {
           iterator.remove();
     } else { 
           seen.add(p.getName());
     }
}

Everytime you write .next(), the iteration is taken 1 step forward. 每次编写.next()时,迭代都会向前迈出一步。 So, say you have 10 person in your list. 所以,假设你的名单中有10个人。 You pick the 1st person, and check the next person using iterator.next(). 你挑选第一个人,并使用iterator.next()检查下一个人。 Though you fetch the 2nd person, the iterator is now at person 2. So in the next run, the 3rd person is fetched and compared with the 4th person. 虽然你取第二个人,但是迭代器现在是第二个人。所以在下一次运行中,第三个人被取出并与第四个人进行比较。

What you ought to do is, pick 1 person and compare his name with the names of all the 10 person in the list and then remove all the instances of duplicate objects from the list. 你应该做的是,选择1个人并将他的名字与列表中所有10个人的名字进行比较,然后从列表中删除所有重复对象的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM