如何根据Object的单个属性匹配删除List中的重复对象?

时间:2017-07-10 20:52:55

标签: java arraylist collections removeall

我有两个新的Java列表:

public class SomeDto() {
  public Long id;
  public String name;
  public Long qty;
}

List<SomeDto> someDtoList1 = new ArrayList<SomeDto>();
List<SomeDto> someDtoList2 = new ArrayList<SomeDto>();

我想做这样的事情:

someDtoList1.removeAll(someDtoList2);

但仅基于id属性。这可以在java 7中以某种方式在一次调用中完成吗?或者我需要编写自己的逻辑吗?

3 个答案:

答案 0 :(得分:2)

坚果壳中的步骤:

  • 首先在课堂上创建getter。
  • 添加默认构造函数,以便您可以创建新对象并将其添加到列表中。
  • 然后在列表中添加一些对象。
  • 迭代该列表。使用getter搜索您想要的属性。
  • 如果匹配,请添加到其他列表。
  • 使用删除所有方法

        public static class SomeDto {
    
            public Long id;
            public String name;
            public Long qty;
    
            public SomeDto(Long id, String name, Long qty) {
                this.qty = qty;
                this.name = name;
                this.id = id;
            }
    
            public Long getId() {
                return id;
            }
    
            public String getName() {
                return name;
            }
    
            public Long getQty() {
                return qty;
            }
        }
    
    public static void main(String args[]) {
    
        List<SomeDto> someDtoList1 = new ArrayList<SomeDto>();
        List<SomeDto> someDtoList2 = new ArrayList<SomeDto>();
    
        someDtoList1.add(new SomeDto((long) 1, "Scarlett Johannson", (long) 100));
        someDtoList1.add(new SomeDto((long) 2, "Emma Stone", (long) 101));
        someDtoList1.add(new SomeDto((long) 3, "Ariana Grande", (long) 1));
    
       for(SomeDto it: someDtoList1){// Just displaying the newly created list
    
        System.out.println("Id: " + it.getId() + " Name: " + it.getName() + " QTY: " + it.getQty());
    }
    
        for(SomeDto it: someDtoList1){// Iterating over the list
    
            if (it.getId() == (long) 3) {// Searching for criteria
                someDtoList2.add(it);// when a match is found add to another list
            }
         }
    
    someDtoList1.removeAll(someDtoList2);// removing the objects
    
    System.out.println("After removal of 3rd ID: ");
    
       for(SomeDto it: someDtoList1){// Displaying the final list
    
         System.out.println("Id: " + it.getId() + " Name: " + it.getName() + " QTY: " + it.getQty());
       }
    }
    

答案 1 :(得分:1)

首先,您需要在equals属性上实现SomeDto.id方法。然后迭代someDtoList1并调用方法someDtoList2.contains(curretDto)。如果返回true,则可以从currentDto中删除someDtoList1。您可以使用java.util.Iterator

class SomeDto {
    public Long id;
    public String name;
    public Long qty;

    @Override
    public boolean equals(Object obj) {
        return id == obj.id;
    }

    public static void main(String[] args) {

        List<SomeDto> someDtoList1 = new ArrayList<SomeDto>();
        List<SomeDto> someDtoList2 = new ArrayList<SomeDto>();

        for (Iterator iterator = someDtoList1.iterator(); iterator.hasNext();) {
            SomeDto someDto = (SomeDto) iterator.next();
            if (someDtoList2.contains(someDto)) {
                iterator.remove();
            }
        }
    }
}

答案 2 :(得分:0)

为此,您只能覆盖equals类中的SomeDto方法,才能比较id属性。

但总的来说这非常危险:   代码中的任何位置如果它们的id相等,则认为两个SomeDto个对象是相等的。

如果您确定可以接受,可以这样做:

    @Override
    public boolean equals(Object other)
    {
        if (this == other) return true;
        if (!(other instanceof SomeDto)) return false;
        SomeDto otherDto = (SomeDto)other;
        return (null == this.id?otherDto.id == null: this.id.equals(otherDto.id);           
    }

PS。在大多数情况下,这是不可接受的,特别是与DTO。