【问题标题】:How do I remove repeated elements (objects of myClass) from ArrayList如何从 ArrayList 中删除重复的元素(myClass 的对象)
【发布时间】:2015-10-13 10:03:38
【问题描述】:

我有一个数组列表

ArrayList<WorkPosition> info = new ArrayList<>();

在某些时候,我添加了一些对象。最后这个arrayList有多次相同的对象..该对象有一些int值和2个arraylists

97 [9, 10, 15] [1202, 1204, 2082]
97 [9, 10, 15] [1202, 1204, 2082]
97 [9, 10, 15] [1202, 1204, 2082]
42 [14, 16, 29] [404, 1081, 1201D]
42 [14, 16, 29] [404, 1081, 1201D]
42 [14, 16, 29] [404, 1081, 1201D]
57 [18, 30, 32] [2081, 435, 1032]
57 [18, 30, 32] [2081, 435, 1032]
57 [18, 30, 32] [2081, 435, 1032]
34 [19, 27, 28] [4501, 831, 201]
34 [19, 27, 28] [4501, 831, 201]
34 [19, 27, 28] [4501, 831, 201]

有什么方法可以删除多余的外观吗? 我读过这个How do I remove repeated elements from ArrayList?,但在我的情况下,我没有Strings,但对象类型为WorkPosition..

public class WorkPosition {

    private int id;
    ArrayList<Integer> machines = new ArrayList<>();
    ArrayList<String> bottles= new ArrayList<>();


    public WorkPosition(int id, ArrayList<Integer> machines, ArrayList<String> bottles) {

        this.id = id;
        this.machines = machines;
        this.kaloupia = kaloupia;
    }

    //getters and setters...
}

【问题讨论】:

  • 您可以改用Set
  • 你应该给出WorkPosition源代码,关键是覆盖那个类的equals & hashcode
  • 我添加了WorkPosition

标签: java arraylist


【解决方案1】:

我阅读了这篇如何从ArrayList 中删除重复的元素?但就我而言,我没有Strings,但对象类型为WorkPosition

由于答案中的解决方案不关心您是否使用 Strings 或其他一些表现良好的对象,所以您需要做的就是使您的 WorkPosition 对象表现良好解决方案的观点。

为此,您必须重写两个特定方法 - hashCode()equals(Object),并确保它们的行为符合规范。完成此操作后,消除重复项的方法将适用于您的对象,就像它适用于 Strings 一样。

class WorkPosition {
    @Override
    public boolean equals(Object obj) {
        // The implementation has to check that obj is WorkPosition,
        // and then compare the content of its attributes and arrays
        // to the corresponding elements of this object
        ...
    }
    @Override
    public int hashCode() {
        // The implementation needs to produce an int based on
        // the values set in object's fields and arrays.
        // The actual number does not matter too much, as long as
        // the same number is produced for objects that are equal.
        ...
    }
}

【讨论】:

    【解决方案2】:

    您可以将您的List 转换为Set

    ArrayList<WorkPosition> info = new ArrayList<>();
    Set<WorkPosition> distincts = new HashSet<>(info);
    

    这要求WorkPosition对象覆盖equals(obj)方法和hashCode(),像这样:

    public class WorkPosition {
    
       // your code
    
        @Override
        public boolean equals(Object obj) {
            // logic here
        }
    
        @Override
        public int hashCode() {
            // logic here
        }
    }
    

    【讨论】:

      【解决方案3】:

      只需将 ArrayList 转换为 Set 实现即可。 Set 集合不包含重复元素。 示例:

       Set<WorkPosition> set = new HashSet<WorkPosition>(list);
      

      所有放置在 List、Set 或 Map(作为键或值)中的对象都应具有适当的 equals 定义。请参阅Collection.containsMap.containsKeyMap.containsValue

      阅读更多关于the practice of implementing equals

      【讨论】:

        猜你喜欢
        • 2022-01-17
        • 2015-08-25
        • 1970-01-01
        • 1970-01-01
        • 2014-09-19
        • 2011-11-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多