-1
 List<Action> action = noDuplicate Object();
 List<Action> actions = duplicate Object();

I want to add both the list and have only distinct object? Any suggestion please? please don't use overriding equals and hashcode() method since i could not place because it is generating by JAXB.

Abhishek
  • 3
  • 4

2 Answers2

1

You can do the following.

  1. Create new wrapper class

    class ActionWrapper { private Action action; //equals and hash code here based on action fields }

  2. Use Set instead of List

  3. Finally convert set to list something like this actionWrappers.stream().map((s) -> s.getAction()).collect(Collectors.toList())

Maxim Tulupov
  • 1,621
  • 13
  • 8
  • Thanks for your suggestion... can we have any ans without using wrapper class. please ? – Abhishek Apr 04 '17 at 10:47
  • Unfortunately it is not clear how do you intend to determine duplicate objects. Let's assume you have the following fields in Action class String name, Date dateCreated. And action equals another only if name1 == name2 and date1 == date2. Let's create a map Map where key = name + date. So you just put into map your actions something like this map.put(action.name + action.dateCreated, action), then get unique values new ArrayList(map.values()) – Maxim Tulupov Apr 04 '17 at 11:12
  • Thanks Maxim for suggestion.. – Abhishek Apr 04 '17 at 11:25
0

You can iterate backwards over the list and then check for each item if there you find a duplicate in the remaining list: if you find a duplicate remove the current item and proceed.

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class ListTest {

    @Test
    public void listDedup() throws Exception {
        List<String> duplicates = new ArrayList<>();
        duplicates.add("a");
        duplicates.add("a");
        duplicates.add("A");
        duplicates.add("b");
        Assert.assertEquals(4, duplicates.size());

        /* iterate backwards, so that we can safely remove items 
         * from the list that we are iterating over 
         */
        for (int i = duplicates.size() - 1; i >= 0; i--) {
            String item = duplicates.get(i);
            // now check the remaining items in the list for duplicates
            for (int j = i - 1; j >= 0; j--) {
                String possibleDuplicate = duplicates.get(j);
                // check for equality: use any comparison operation you like
                if (item.equalsIgnoreCase(possibleDuplicate)) {
                    // this is a duplicate - remove it
                    duplicates.remove(i);
                    // break the inner loop because we already have a duplicate
                    break;  
                }
            }
        }
        // only "a" and "b" reamin in the list
        Assert.assertEquals(2, duplicates.size());
        Assert.assertEquals("a", duplicates.get(0));
        Assert.assertEquals("b", duplicates.get(1));
    }
}
TmTron
  • 17,012
  • 10
  • 94
  • 142