Get unique values ​​from arraylist in java

I have an ArrayList with several entries, and one column contains gas names like CO2 CH4 SO2, etc. Now I want to get different names of gases (unique) only without repetition with ArrayList . How can I do that?

+68
java list set
Nov 17 '12 at 8:56
source share
6 answers

You must use Set .

A Set is a Collection that does not contain duplicates.

If you have a List that contains duplicates, you can get 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 equals () methods of elements.

+108
Nov 17 '12 at 9:11
source share

You can use the Java 8 Stream API .

The distinct method is an intermediate operation that filters the stream and allows you to use only distict values ​​(by default using the Object :: equals method) to advance 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 
+56
Nov 16 '15 at 12:36
source share

I hope I correctly understood your question: considering 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 } 
+11
Nov 17 '12 at 9:01
source share

Here it is straightforward, without resorting to conventional comparators or the like:

 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 a TreeSet instead of a HashSet will give a directly sorted arraylist and above Collections.sort may be skipped, but a TreeSet is otherwise less efficient, so it is often better and rarely worse to use a HashSet even when sorting.

+6
Nov 17 '12 at 9:16
source share
 ArrayList values = ... // your values Set uniqueValues = new HashSet(values); //now unique 
+6
Nov 17 '12 at 11:37
source share

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

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

as said above, Mathias Schwarz, but you must provide your aBean hashCode() and equals(Object obj) methods, which can be easily executed in Eclipse using the highlighted menu " Generate hashCode() and equals() " (while in the class bean), Set will evaluate overridden methods to allocate equal objects.

+1
Nov 22 '13 at 13:26
source share



All Articles