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 repetation from the ArrayList
. How can it be done?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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:
NOTE: This HashSet constructor identifies duplicates by invoking the elements' equals() methods.
I hope I understand your question correctly: assuming that the values are of type
String
, the most efficient way is probably to convert to aHashSet
and iterate over it:Here's straightforward way without resorting to custom comparators or stuff like that:
Note: Using
TreeSet
instead ofHashSet
would give directly sorted arraylist and aboveCollections.sort
could be skipped, butTreeSet
is otherwise less efficent, so it's often better, and rarely worse, to useHashSet
even when sorting is needed.