Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an ArrayList which is getting populated with string array. At the end of the process I have to remove the duplicates string arrays added to the list. I tried doing the conventional solution of using LinkedHashSet and populating into a new Array List, but in vein.

List<String[]> OrgList = new ArrayList<String[]>();
//String arrays added to OrgList 
.....
.....
List<String[]> NewList = new ArrayList<String[]>(new LinkedHashSet<String[]>(OrgList));

Is there any other way to remove duplicates in this scenario?

Thanks in Advance.

share|improve this question
    
Why are you using String array then why not set directly? –  SMA Jan 6 at 12:45
    
@almasshaikh look here why set wont work here : stackoverflow.com/questions/10154305/… –  sᴜʀᴇsʜ ᴀᴛᴛᴀ Jan 6 at 12:47
    
Unless i got it wrong and If i understand OP's question then adding set instead of array wont help him @sᴜʀᴇsʜᴀᴛᴛᴀ? –  SMA Jan 6 at 12:50

4 Answers 4

up vote 0 down vote accepted

LinkedHashSet did not work, because it's String arrays, that you are adding, for which equals is taken from Object.

  1. If order does not matter, you can use TreeSet with custom Comparator, although you will loose order.

  2. If you can switch from arrays of Strings to List, your LinkedHashSet would work

share|improve this answer

You can use this code

private String getArrayKey(String[] list) {
        StringBuffer temp = new StringBuffer();
        for (String s : list) {
            temp.append(s).append("-");
        }
        return temp.toString();
    }

Then put your List in a Map in this way.

Map<String , String []> map = new HashMap<String, String[]>();
        for (String[] strings : OrgList ) {
            map.put(getArrayKey(strings), strings);            
        }

and finally retrive your list

List<String[]> NewList = new ArrayList<String[]>();
NewList.addAll(map.values());
share|improve this answer
    
Thanks for the reply –  Prashanth T P Jan 6 at 14:05

Same TreeSet can be used only the comparison need to be changed as Arrays.equal doesn't support String[].

TreeSet<String[]> set = new TreeSet<String[]>(new Comparator<String[]>() {
            @Override
            public int compare(String[] o1, String[] o2) {
                return Arrays.asList(o1).containsAll(Arrays.asList(o2))?0:1;
            }});
        set.addAll(origList);
share|improve this answer

This should work

    TreeSet<String[]> set = new TreeSet<String[]>(new Comparator<String[]>() {
        @Override
        public int compare(String[] o1, String[] o2) {
            return Arrays.equals(o1, o2) ? 0 : 1;
        }});
    set.addAll(list);
share|improve this answer
    
Thanks for the reply. –  Prashanth T P Jan 6 at 14:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.