Get unique values from arraylist in java

前端 未结 9 897
心在旅途
心在旅途 2020-11-28 06:27

I have an ArrayList with a number of records and one column contains gas names as CO2 CH4 SO2 etc.Now i want to retrieve different gas names(unique) only withou

相关标签:
9条回答
  • 2020-11-28 07:12

    When I was doing the same query, I had hard time adjusting the solutions to my case, though all the previous answers have good insights.

    Here is a solution when one has to acquire a list of unique objects, NOT strings. Let's say, one has a list of Record object. Record class has only properties of type String, NO property of type int. Here implementing hashCode() becomes difficult as hashCode() needs to return an int.

    The following is a sample Record Class.

    public class Record{
    
        String employeeName;
        String employeeGroup;
    
        Record(String name, String group){  
            employeeName= name;
            employeeGroup = group;    
        }
        public String getEmployeeName(){
            return employeeName;
        }
        public String getEmployeeGroup(){
            return employeeGroup;
        }
    
      @Override
        public boolean equals(Object o){
             if(o instanceof Record){
                if (((Record) o).employeeGroup.equals(employeeGroup) &&
                      ((Record) o).employeeName.equals(employeeName)){
                    return true;
                }
             }
             return false;
        }
    
        @Override
        public int hashCode() { //this should return a unique code
            int hash = 3; //this could be anything, but I would chose a prime(e.g. 5, 7, 11 )
            //again, the multiplier could be anything like 59,79,89, any prime
            hash = 89 * hash + Objects.hashCode(this.employeeGroup); 
            return hash;
        }
    

    As suggested earlier by others, the class needs to override both the equals() and the hashCode() method to be able to use HashSet.

    Now, let's say, the list of Records is allRecord(List<Record> allRecord).

    Set<Record> distinctRecords = new HashSet<>();
    
    for(Record rc: allRecord){
        distinctRecords.add(rc);
    }
    

    This will only add the distinct Records to the Hashset, distinctRecords.

    Hope this helps.

    0 讨论(0)
  • 2020-11-28 07:12

    If you have an array of a some kind of object (bean) you can do this:

    List<aBean> gasList = createDuplicateGasBeans();
    Set<aBean> uniqueGas = new HashSet<aBean>(gasList);
    

    like said Mathias Schwarz above, but you have to provide your aBean with the methods hashCode() and equals(Object obj) that can be done easily in Eclipse by dedicated menu 'Generate hashCode() and equals()' (while in the bean Class). Set will evaluate the overridden methods to discriminate equals objects.

    0 讨论(0)
  • 2020-11-28 07:13
    ArrayList values = ... // your values
    Set uniqueValues = new HashSet(values); //now unique
    
    0 讨论(0)
提交回复
热议问题