I have an ArrayList<String>
, and I want to remove repeated strings from it. How can I do this?
相关问题
- 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
If you want to remove duplicates from ArrayList means find the below logic,
If you don't want duplicates, use a Set instead of a
List
. To convert aList
to aSet
you can use the following code:If really necessary you can use the same construction to convert a
Set
back into aList
.Probably a bit overkill, but I enjoy this kind of isolated problem. :)
This code uses a temporary Set (for the uniqueness check) but removes elements directly inside the original list. Since element removal inside an ArrayList can induce a huge amount of array copying, the remove(int)-method is avoided.
While we're at it, here's a version for LinkedList (a lot nicer!):
Use the marker interface to present a unified solution for List:
EDIT: I guess the generics-stuff doesn't really add any value here.. Oh well. :)
This is used for your Custom Objects list
If you are using model type List< T>/ArrayList< T> . Hope,it's help you.
Here is my code without using any other data structure like set or hashmap
this can solve the problem: