I have a list of objects which contain several properties. I am looking to break the list into several lists composed of objects which posses the same sub-property.
Here is a hypothetical example, say I have a list of Cars. Each Car has properties: id, manufacturerId and color.
I would like to create lists of Cars for those with matching manufacturerId properties.
I have tried using list.where within list.forEach to create new sub-lists, however I get duplicate lists because when the property I am comparing against (A) matches with another property (B), I get another sub-list when:
B = A.
You can use groupBy from package:collection
var carsByManufacturer = groupBy(cars, (car) => car.manufacturerId);
This will create a Map where the keys are the manufacturerID, and the values are lists of cars with that manufacturer.
Then do,
for (final manufacturerEntry in carsByManufacturer) {
final key = manufacturerEntry.key;
final value = manufacturerEntry.value;
final listOfCarsFromSameManufactuer = List.from(entry.value);
...
}
You will now have lists called: listOfCarsFromSameManufactuer.
Related
I am trying to create a list whose name should include one of the values from another list. To simplify, I have created a following simple example.
Create first list
#{list.1} create list 1 2 3
log ${list.1}[1]
Get the second value from the list
${list1_2ndValue} = get from list ${list.1} 1
log ${list1_2ndvalue}
Use this value in a new list as part of its name. New list name should be #{list.2}
#{list.${list1_2ndValue}} create list a b c
It fails with No keyword with name '#{list.${list1_2ndValue}}' found. error
Tried different methods but no luck. How can I use another list value in the name of new list. Any help is appreciated.
I am looking for an example how to store one key and multiple values example qualifications as below:
[name:'Test1',job:'QA',qualifications:['Selenium','Java']]
Map<String,String>=new HashMap<String,String>//not allowing multiple values.
You need to know, that you are using a map containing entries of different key type. In your case, the most common key type for all values is Object, becuase name and job keys are of type String, while qualifications is of type List<String>. If you want to create such a map step by step (by specifying each key-value entry), you need to make sure that qualifications is a list. You can do it by assigning a list of predefined entries, or by assigning an empty list and then adding elements to it with << operator. Consider the following example:
def expected = [name:'Test1',job:'QA',qualifications:['Selenium','Java']]
def emp = [:]
emp.name = "Test1"
emp.job = "QA"
emp.qualifications = []
emp.qualifications << "Selenium"
emp.qualifications << "Java"
assert emp == expected
In this example, we create an empty list and later we add values to. Alternatively, we could assign a list containing both values already.
def expected = [name:'Test1',job:'QA',qualifications:['Selenium','Java']]
def emp = [:]
emp.name = "Test1"
emp.job = "QA"
emp.qualifications = ["Selenium", "Java"]
assert emp == expected
Your example from comment did something different. Instead of adding an element to a list, it was overriding emp.qualification entry with a value of type String. This is why it stored only single the last assigned value.
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.
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();
}
I have two lists:
List<String> ids
List<String> names
The task is to create one Map<String,String> out of these two lists, preferably using Java 8.
Unfortunatly, I did not find how to make it when we have lists with String type.
Assuming both the lists have the same size & ids are unique & ids are the keys of the map and names are corresponding values, you can use the following code to create a map:
Map<String,String> idsNames = IntStream.range(0,ids.size())
.mapToObj(i -> new AbstractMap.SimpleEntry<>(ids.get(i),names.get(i)))
.collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey,
AbstractMap.SimpleEntry::getValue));
In my model, turtles have a list A with X number if items. I want to create a new list, which has the X number of items of every list A.
How can I achieve that?
I think what you want is a single list of all elements from all the turtles.
If you want a 2D list, then you could do the following,
let result [ A of turtles]
If you want a consolidated list (i.e. 1D list), then you could do the following,
let result reduce sentence [ A of turtles]