Get unique values from arraylist in java

前端 未结 9 896
心在旅途
心在旅途 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<String> gasList = // create list with duplicates...
    Set<String> uniqueGas = new HashSet<String>(gasList);
    System.out.println("Unique gas count: " + uniqueGas.size());
    

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

    0 讨论(0)
  • 2020-11-28 06:57
        public static List getUniqueValues(List input) {
          return new ArrayList<>(new LinkedHashSet<>(incoming));
        }
    

    dont forget to implement your equals method first

    0 讨论(0)
  • 2020-11-28 06:58

    I hope I understand your question correctly: assuming that the values are of type String, the most efficient way is probably to convert to a HashSet and iterate over it:

    ArrayList<String> values = ... //Your values
    HashSet<String> uniqueValues = new HashSet<>(values);
    for (String value : uniqueValues) {
       ... //Do something
    }
    
    0 讨论(0)
  • 2020-11-28 07:02

    Here's straightforward way without resorting to custom comparators or stuff like that:

    Set<String> gasNames = new HashSet<String>();
    List<YourRecord> records = ...;
    
    for(YourRecord record : records) {
      gasNames.add(record.getGasName());
    }
    
    // now gasNames is a set of unique gas names, which you could operate on:
    List<String> sortedGasses = new ArrayList<String>(gasNames);
    Collections.sort(sortedGasses);
    

    Note: Using TreeSet instead of HashSet would give directly sorted arraylist and above Collections.sort could be skipped, but TreeSet is otherwise less efficent, so it's often better, and rarely worse, to use HashSet even when sorting is needed.

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

    You can use Java 8 Stream API.

    Method distinct is an intermediate operation that filters the stream and allows only distinct values (by default using the Object::equals method) to pass to the next operation.
    I wrote an example below for your case,

    // Create the list with duplicates.
    List<String> listAll = Arrays.asList("CO2", "CH4", "SO2", "CO2", "CH4", "SO2", "CO2", "CH4", "SO2");
    
    // Create a list with the distinct elements using stream.
    List<String> listDistinct = listAll.stream().distinct().collect(Collectors.toList());
    
    // Display them to terminal using stream::collect with a build in Collector.
    String collectAll = listAll.stream().collect(Collectors.joining(", "));
    System.out.println(collectAll); //=> CO2, CH4, SO2, CO2, CH4 etc..
    String collectDistinct = listDistinct.stream().collect(Collectors.joining(", "));
    System.out.println(collectDistinct); //=> CO2, CH4, SO2
    
    0 讨论(0)
  • 2020-11-28 07:06

    you can use this for making a list Unique

    ArrayList<String> listWithDuplicateValues = new ArrayList<>();
    list.add("first");
    list.add("first");
    list.add("second");
    
    ArrayList uniqueList = (ArrayList) listWithDuplicateValues.stream().distinct().collect(Collectors.toList());
    
    0 讨论(0)
提交回复
热议问题