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 the ArrayList
. How can it be done?
-
5I will update the tags. This is a Java question and I believe it is unrelated to jsf and primefaces... – Mathias Schwarz Nov 17 '12 at 09:02
9 Answers
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.

- 22,322
- 9
- 59
- 108
-
that implies that 2 records are considered equals (in the sense of HashSet) if their names are equals. – njzk2 Mar 12 '14 at 19:29
-
@njzk2 - I figured that would be pretty obvious, but I guess I can edit to point it out explicitly... – jahroy Mar 12 '14 at 23:20
-
I get an error while trying to implement the above code: `java: no suitable constructor found for HashSet`. Any idea why this occurs? – lordlabakdas Feb 06 '17 at 15:09
-
If you want to retain an original order from the list see `LinkedHashSet` https://stackoverflow.com/a/8712770/32453 – rogerdpack Nov 19 '20 at 16:14
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

- 1
- 1

- 18,813
- 9
- 90
- 92
-
1The heading shouts - "Use this answer!! Not the accepted one". And I must say, this is the way to go. – paradocslover Apr 19 '21 at 03:14
-
This 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 a `List` in the first place – Guilherme Taffarel Bergamin Aug 20 '21 at 21:55
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
}

- 7,099
- 23
- 28
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());

- 1,124
- 1
- 14
- 17
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.

- 60,639
- 21
- 115
- 176
ArrayList values = ... // your values
Set uniqueValues = new HashSet(values); //now unique

- 9,562
- 2
- 32
- 29
-
I 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? – SDas Nov 20 '12 at 05:53
public static List getUniqueValues(List input) {
return new ArrayList<>(new LinkedHashSet<>(incoming));
}
dont forget to implement your equals method first

- 62,887
- 36
- 269
- 388

- 21
- 2
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.

- 6,651
- 3
- 36
- 58
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.

- 54,432
- 29
- 203
- 199

- 73
- 1
- 6