Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing non unique Objects from a List (identifiable by a property)

I am capturing events from a stream, each event is a Device Object. The way the stream work is that it is on a timer, so it picks up the same device multiple times and adds to the stream.

I am putting all theres is a List<Device> and putting that into another stream.

I have create a StreamTransformer in the attempt to remove duplicate from the list and then add the unique list back into the stream.

This transform code below, I have tried to add to set and back to list, but this hasn't worked I assume due to the fact they are objects not strings.

  //Transform Stream List by removing duplicate objects
  final deviceList = StreamTransformer<List<Device>, List<Device>>.fromHandlers(
      handleData: (list, sink) {
      List<Device> distinctList = list.toSet().toList();
      sink.add(distinctList);
  });

I have attempted to use .where and other libraries but to no avail and am hoping for some guidance.

Device Object contains unique id and name that could be used to filter out duplicates

Question: How can I remove duplicate objects from a List in Dart?

Thanks in advance.

like image 236
Yonkee Avatar asked Jan 19 '19 23:01

Yonkee


People also ask

How do I remove non unique items from a list in Python?

You can remove duplicates from a Python using the dict. fromkeys(), which generates a dictionary that removes any duplicate values. You can also convert a list to a set. You must convert the dictionary or set back into a list to see a list whose duplicates have been removed.

How do you remove duplicates from a list in Java 8?

Remove duplicates in arraylist – Java 8Use steam's distinct() method which returns a stream consisting of the distinct elements comparing by object's equals() method. Collect all district elements as List using Collectors. toList() . Java program to remove duplicates from arraylist in java without using Set.


1 Answers

First of all you would need to define by which criteria the objects are supposed to be unique. Is there an identifying property for example? If that is the case the following options should work.

The most efficient way is probably to make the approach with a set working. For that you would need to turn your objects into data objects, meaning have them identify for equality by property values. For that you would override the equality operator and hashCode getter. However this changes how your objects behave on every equality operation, so you would have to judge if that is suitable. See this article.

Another option is to just filter manually using a Map:

class MyObj {
  String val;

  MyObj(this.val);
}

TestListFiltering(){
  List<MyObj> l = [
    MyObj("a"),
    MyObj("a"),  
    MyObj("b"),
  ];
  // filter list l for duplicate values of MyObj.val property
  Map<String, MyObj> mp = {};
  for (var item in l) {
    mp[item.val] = item;
  }
  var filteredList = mp.values.toList();
}
like image 194
Oswin Noetzelmann Avatar answered Oct 07 '22 00:10

Oswin Noetzelmann