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.
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.
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With