1

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.

6
  • How are you printing the output array? It would help to show a complete working code. Commented Nov 12, 2014 at 19:06
  • im just using sysout d.toString(); Commented Nov 12, 2014 at 19:10
  • print it as : System.out.println(Arrays.toString(d)); Commented Nov 12, 2014 at 19:11
  • 2
    Works for me. Quick question - is your duplicateList an array of those 2-letter strings? Or is it a large string that contains a comma-separated list? Commented Nov 12, 2014 at 19:13
  • 1
    @JNPW No surpize it won't work. You have to get an array first, using split as in the asnwer below, for instance Commented Nov 12, 2014 at 19:15

1 Answer 1

2

Try something like this. That should be it.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.