javastring-utils

removing duplicates from string array - using HashSet


Im trying to convert a string array with duplicates to string array without duplicates by using hashSet like below.

String[] d = new HashSet<String>(Arrays.asList(duplicateList)).toArray(new String[0]);


duplicateList = AD,AD,AD,AD,AD,AD,AD,AD,AD,AD,CP,RR,RR,RR,RR,RR,RR,,,,,,,,,,,

When I print d its still the same. Am i missing something?

Note: I do not want to loop through and use contains or equals.


Solution

  • Try something like this. That should be it.

    d.addAll(Arrays.asList(duplicateList.split(",")));