Get unique values from ArrayList in Java
Solution 1
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.
Solution 2
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
Solution 3
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
}
Solution 4
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.
Solution 5
ArrayList values = ... // your values
Set uniqueValues = new HashSet(values); //now unique
SDas
Updated on May 04, 2021Comments
-
SDas about 3 years
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 without repeatation from theArrayList
. How can it be done? -
SDas over 11 yearsI am now getting the unique elements from the list but it is being sorted by itself.But i need the data not to be in sorted order.How can it be done?
-
njzk2 about 10 yearsthat implies that 2 records are considered equals (in the sense of HashSet) if their names are equals.
-
jahroy about 10 years@njzk2 - I figured that would be pretty obvious, but I guess I can edit to point it out explicitly...
-
lordlabakdas about 7 yearsI get an error while trying to implement the above code:
java: no suitable constructor found for HashSet
. Any idea why this occurs? -
rogerdpack over 3 yearsIf you want to retain an original order from the list see
LinkedHashSet
stackoverflow.com/a/8712770/32453 -
paradocslover about 3 yearsThe heading shouts - "Use this answer!! Not the accepted one". And I must say, this is the way to go.
-
Guilherme Taffarel Bergamin over 2 yearsThis answer is probably great for most uses, but in case you need the list to always be of distinct values, say at time of populating it for example, then I would rather be using a
Set
like the accepted answer. I might not even have aList
in the first place