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

shmosel
  • 49,289
  • 6
  • 73
  • 138
kiri
  • 1,977
  • 5
  • 27
  • 55
  • 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) – Rogue Aug 13 '19 at 01:36
  • That's fine We need to consider only userid – kiri Aug 13 '19 at 01:49
  • It's quicker not to put the duplicates in there in the first place. Consider a `HashSet` or `TreeSet`. – user207421 Aug 13 '19 at 02:05

2 Answers2

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.

shmosel
  • 49,289
  • 6
  • 73
  • 138
  • HashMap is the simplest & most efficient (O(N)) solution. +1 – mertcanb Aug 13 '19 at 01:59
  • 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. – Captain Aug 13 '19 at 03:20
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);
Captain
  • 714
  • 4
  • 18