I’d like to eliminate duplicates based on the properties "name" and "price" of each element and filter the list accordingly.
List<Connector> connectorList = [
Model(name : "Type A", price : 2.30, value : "Value 1"),
Model(name : "Type B", price : 2.30, value : "Value 2"),
Model(name : "Type C", price : 2.30, value : "Value 3"),
Model(name : "Type A", price : 2.30, value : "Value 4"),
Model(name : "Type B", price : 3.30, value : "Value 5"),
Model(name : "Type B", price : 2.30, value : "Value 6"),
]
Output:
List<Connector> filteredConnectorList = [
Model(name : "Type A", price : 2.30, value : "Value 1"),
Model(name : "Type B", price : 2.30, value : "Value 2"),
Model(name : "Type B", price : 3.30, value : "Value 5"),
Model(name : "Type C", price : 2.30, value : "Value 3"),
]
2
Answers
First of all use
equatable
package to override the==
operator:Then you can easily use
Set
data structure to achieve your result:With the help of the
collection
package you could do this