Remove duplicate element from the set in java

Viewed 39407

I have set of string array and i want to remove duplicate elements from this...

    String[] arr1 = {"a1","b1"};
    String[] arr2 = {"a2","b2"};
    Set<String[]> mySet = new HashSet<String[]>();
    mySet.add(arr1);
    mySet.add(arr2);
    mySet.add(new String[] {"a1","b1"});
    System.out.print(mySet.size());

Currently mySet looks like this:

[{"a1","b1"},{"a2","b2"},{"a1","b1"}]

But I want like this:

[{"a1","b1"},{"a2","b2"}]

I know some ways...

  1. Every time I need to run inner loop and check whether its duplicate or not.
  2. Can I override the set's behavior? (hashcode or equals)? ( i do not know how....)
  3. Do I need to change data structure for this? (linkedhashset or list or any other suitable data structure for this?)
8 Answers
Related