1

All,

I have a object

public class Device {

     public String getUserId() {
        return userId;
    }
    public String getDeviceId() {
        return deviceId;
    }

}

I get all the List of Values

     List<Device> uList = getList();

In the List i have a duplicate values Based on userId Now i want to get Unique list which will remove the duplicates of userId

How can i acheive it, I am very much new to Java

3
  • What if the userid is the same, and the device id is different? There's a couple answers here regardless, one more correct than other. Additionally you might benefit from a different design depending on how you're using this list (show the code for that) Commented Aug 13, 2019 at 1:36
  • That's fine We need to consider only userid Commented Aug 13, 2019 at 1:49
  • It's quicker not to put the duplicates in there in the first place. Consider a HashSet or TreeSet. Commented Aug 13, 2019 at 2:05

2 Answers 2

5

The simplest way is to create a Map where the key is the userId:

Map<String, Device> map = new HashMap<>();
devices.forEach(d -> map.put(d.getUserId(), d));
List<Device> uniques = new ArrayList<>(map.values());

Or, using streams:

Map<String, Device> map = devices.stream()
        .collect(Collectors.toMap(Device::getUserId, d -> d, (a, b) -> a));
List<Device> uniques = new ArrayList<>(map.values());

Alternatively, you can dump them in a TreeSet with a comparator that checks userId:

Set<Device> set = new TreeSet<>(Comparator.comparing(Device::getUserId));
set.addAll(devices);
List<Device> uniques = new ArrayList<>(set);

All of this assumes you're not concerned about discrepancies in deviceId. Otherwise, take a look at Map.merge() or the corresponding Collectors.toMap() overload.

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

2 Comments

HashMap is the simplest & most efficient (O(N)) solution. +1
It's safe to assume that insertion order matters if the original question used a List. TreeSet works on comparison order. LinkedHashSet will handle uniqueness and insertion order.
0

Define equality in your object by overriding equals and hashCode (why hashCode? read this) and then use a Set (or a LinkedHashSet if order matters) rather than a List.

    public class Device {

        // fields, constructors, getters, setters
        ...

        @Override
        public boolean equals(final Object obj) {

            if (obj == this)
                return true;

            if (!(obj instanceof Device))
                return false;

            return Objects.equals(userId, ((Device) obj).userId);
        }

        @Override
        public int hashCode() {

            return Objects.hash(userId);
        }
    }

If you absolutely must have a List at the end of this... well it's easy enough to manipulate.

final Set<Device> uniqueOrderedDevices = Sets.newLinkedHashSet(devicesList);
final List<Device> list = Lists.newArrayList(uniqueOrderedDevices);

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.