Hello I'd like to add Strings to an ArrayList and then sort it to remove duplicates. The order should remain the same way I added those Strings though. What I want: [randomtext, testtext, anothertext] What I get: [anothertext, randomtext, testtext]
Is this possible or is there an easier way?
ArrayList<String> abc = new ArrayList();
abc.add("randomtext");
abc.add("testtext");
abc.add("anothertext");
abc.add("randomtext");
abc.add("testtext");
abc.add("anothertext");
abc.add("randomtext");
abc.add("testtext");
abc.add("anothertext");
Collections.sort(abc);
for (int i = 1; i < abc.size() ; i++)
{
if(abc.get(i) == abc.get(i-1))
{
abc.remove(i);
i -= 1;
}
}
System.out.print(abc);