If you are looking to remove the duplicates from your String and not sure of the positions of the duplicates occurrences then you can try using both a list (to maintain order) and a set (to remove duplicates).
You can then rebuilt your String using the list without duplicates.
Here is the code snippet:
public static void main (String[] args)
{
String str = "hello I am a example example";
String[] tokens = str.split(" ");
Set<String> set = new HashSet<>();
List<String> list = new ArrayList<>();
for(String token : tokens) {
if(!set.contains(token)) {
set.add(token);
list.add(token);
}
}
/* Print String */
for(int i = 0; i < list.size(); i++)
System.out.print(list.get(i) + " ");
}
Output:
hello I am a example
"example lala example"and"example example lala"?