Get unique values from arraylist in java

前端 未结 9 928
心在旅途
心在旅途 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 06:56

    You should use a Set. A Set is a Collection that contains no duplicates.

    If you have a List that contains duplicates, you can get the unique entries like this:

    List gasList = // create list with duplicates...
    Set uniqueGas = new HashSet(gasList);
    System.out.println("Unique gas count: " + uniqueGas.size());
    

    NOTE: This HashSet constructor identifies duplicates by invoking the elements' equals() methods.

提交回复
热议问题