List<Map<String, Object>> to Map<String, String[]> - list

I have a List of Map List<Map<String, Object>>. I want to move to Map<String, String[]>
Can someone please let me know how to convert?
List<Map<String, Object>> currentList = new ArrayList<Map<String, Object>>();
Map<String, Object> currMap = new HashMap<String, Object>();
currMap.put("A", "ABC");
currMap.put("B", "PQR");
currMap.put("C", "XYZ");
currentList.add(currMap);
currMap.put("A", "123");
currMap.put("B", "456");
currMap.put("C", "789");
currentList.add(currMap);
currMap.put("A", "OOO");
currMap.put("B", "ZZZ");
currentList.add(currMap);
To-be :
"A", ["ABC", "123", "OOO"],
"B", ["PQR", "456", "ZZZ"],
"C", ["XYZ", "789", ""]

One way using streams would be:
currentList.stream()
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.groupingBy(Entry::getKey, Collectors.mapping(Entry::getValue, Collectors.toList())));
This would result in:
{A=[ABC, 123, OOO], B=[PQR, 456, ZZZ], C=[XYZ, 789]}
As a sidenote, the way you are generating currentList in the example does not result in a list of maps as you are probably expecting. As it is, you will end up with 3 references to the same map in the list.

Related

How to perform a search in array of object flutter?

I am having a Map list with key and values, for example :
map<String, dynamic> my_List = [{"name": "mike", "age": "20"}, {"name":"william","age": "23"}].
I already tried containsValue but I don't want to use it.
The result i need to get is, when i search for m i need to get [{"name": "mike", "age": "20"}, {"name":"william","age": "23"}] , and when i search 3 i need the result as [{"name":"william","age": "23"}].
You could create a Person or User class as julemand101 has suggested but if You have to work with Map try this:
List<Map<String, dynamic>> search(String input){
return my_List.where((e) => e["name"].contains(input) || e["age"].contains(input)).toList();
}

How to perform search for a Map in flutter?

I am having a Map list with key and values, for example :
map<String, dynamic> my_List = [{"name": "mike", "age": "20"}, {"name": "william", "age": "23"}].
I tried containsValue, but I don't want to use it.
The result I want to get is when search for "i" then I need to get the result like {"mike" and "william"} and when I search for "2" I need only the result {20 and 23}.
Try something like:
String search = "k";
var matchNames = my_List.where(p => p["name"].contains(search)).map(p => p["name"]);
String result = "";
foreach(var name in matchNames){
result += name;
if(name != matchNames.last){
result += " and ";
}
}

AppSync Subscription Filters

We need a way to filter a subscription in the following manner:
type Subscription {
onPlanningViewUpdate(prop1: ["a", "b", "c"]): ReturnObject
}
ReturnObject = {prop1: "a", ...} // would pass through
ReturnObject = {prop1: "b", ...} // would pass through
ReturnObject = {prop1: "c", ...} // would pass through
ReturnObject = {prop1: "x", ...} // would NOT pass through
We have tried using the request and response mapping templates of a resolver with a NONE type data source, but the mappings seem to only get called once when the subscription is first opened. It looks like subscriptions only handle an exact match. We need a way to determine if a prop is contained in the array ["a", "b", "c"]. If prop1 == "a" or prop1 == "b" or prop1 == "c" pass through.
Here is the actual filter we want to use:
type Subscription {
onPlanningViewUpdate(site_ids: ["a", "b", "c"], planDate: "aString", lob_ids: ["x", "y", "z"]):
ReturnObject
}
ReturnObject = {site_ids: "a", planDate: "aString", lob_ids: "y"} // would pass through
ReturnObject = {site_ids: "a", planDate: "wrongString", lob_ids: "y"} // would NOT pass through
Is there a way to do this ?
Thanks,
Warren Bell

Using Linq to order lists within a list

I have a list of items (actually an IEnumerable). Each item has a list of values. For example:
Item1.Value[0] = "Health & Safety"
Item1.Value[1] = "Economic"
Item1.Value[2] = "Environment"
Item2.Value[0] = "Reputation"
Item2.Value[1] = "Environment"
Item2.Value[2] = "Regulatory"
...
How can I order the list of values using linq? I know I can order the list of items using something like:
Items.Orderby(x => x.something)
...but how do I get to the list of values in each item?
You can try this
Items.ForEach(i=> x.something = x.something.OrderBy(o=> o.field));
EDIT: Based off OP's comment, Value is a Dictionary<string, object>. You cannot sort a Dictionary as they are unordered by design.
Consider using a SortedDictionary<TKey, TValue>, and implement an IComparer<TKey> to fit your sorting needs:
Example:
Dictionary<string, object> values = new Dictionary<string, object>
{
{ "b", 1 }, { "a", 2 }, { "c", 3 }
};
// { { "a", 2 }, { "b", 1 }, { "c", 3 } }
SortedDictionary<string, object> keyAscending =
new SortedDictionary<string, object>(values);
public class ReverseStringComparer : IComparer<string>
{
int IComparer<string>.Compare(string x, string y)
{
return y.CompareTo(x);
}
}
// { { "c", 3 }, { "b", 1 }, { "a", 2 } }
SortedDictionary<string, object> keyDescending =
new SortedDictionary<string, object>(values, new ReverseStringComparer());

How to set an Array<Object> inside a Dictionary<String, AnyObject> - Swift3

I'm new in swift. I'm trying to add an Array to a specific key in my Dictionary.
I have the following code:
var myArray : Array<Links> = []
var myDict : Dictionary<String, AnyObject> = [:]
myDict["links"] = myArray as AnyObject? // I need help in this row, It does not work.
This is the Json structure I have in myDict and I'm trying to set:
id : "blabla"
links: [
0: {key1: "a", key2: "b", name: "c", link: "d"}
1: {key1: "e", key2: "f", name: "j", link: "h"}
]
Please, consider I already have all the rest working properly. My only problem is how to add my array in the dictionary as commented in the code above.
My JSON structure:
I hope I could make myself clear enough.
Thank you.
First of all don't cast types up and don't annotate types unless the compiler complains.
Second of all a JSON dictionary is [String:Any] in Swift 3.
Further the recommended syntax to create an empty collection object is
var myDict = Dictionary<String, Any>()
Assuming your array – actually a dictionary – is
let myArray = [
0: ["key1": "a", "key2": "b", "name": "c", "link": "d"],
1: ["key1": "e", "key2": "f", "name": "j", "link": "h"]
]
just assign it:
myDict["links"] = myArray
Even if there is a struct
struct Link {
var key1, key2, name, link : String
}
and the array dictionary is
let linkDictionary = [
0: Link(key1:"a", key2: "b", name: "c", link: "d"),
1: Link(key1:"e", key2: "f", name: "g", link: "h")]
you can assign it if the value type is Any
myDict["links"] = linkDictionary
Assuming, for a second, that links really was an array, it would be:
var dictionary: [String: Any] = [
"id": "blabla",
"links": [
["key1": "a", "key2": "b", "name": "c", "link": "d"],
["key1": "e", "key2": "f", "name": "j", "link": "h"]
]
]
// retrieve links, or initialize it if not found
var links = dictionary["links"] as? [[String: String]] ?? [[String: String]]()
// add your new link to local dictionary
links.append(["key1": "k", "key2": "l", "name": "m", "link": "n"])
// update original structure
dictionary["links"] = links
Personally, though, when I see a repeating dictionary structure like your links, this screams for a real model for these objects. For example:
struct Foo {
let id: String
var links: [Link]?
}
struct Link {
let key1: String
let key2: String
let name: String
let link: String
}
var foo = Foo(id: "blabla", links: [
Link(key1: "a", key2: "b", name: "c", link: "d"),
Link(key1: "e", key2: "f", name: "j", link: "h")
])
foo.links?.append(Link(key1: "k", key2: "l", name: "m", link: "n"))
Now, in this latter example, I assumed that links was really an array, not a dictionary, but that's not really my point here. My key observation is that code is more readable and robust if you use proper custom types rather than just arrays and dictionaries.
And if you need to send and receive these model objects to some web service, you then map this model object to and from JSON. But use custom types for your actual model.
If you want to make the above types easily converted to and from JSON, you can use one of the object mapping libraries out there, so you can do something yourself, e.g.:
protocol Jsonable {
var jsonObject: Any { get }
init?(jsonObject: Any)
}
extension Foo: Jsonable {
var jsonObject: Any {
return [
"id": id,
"links": links?.map { $0.jsonObject } ?? [Any]()
]
}
init?(jsonObject: Any) {
guard let dictionary = jsonObject as? [String: Any],
let id = dictionary["id"] as? String else { return nil }
var links: [Link]?
if let linksDictionary = dictionary["links"] as? [Any] {
links = linksDictionary.map { Link(jsonObject: $0)! }
}
self.init(id: id, links: links)
}
}
extension Link: Jsonable {
var jsonObject: Any { return [ "key1": key1, "key2": key2, "name": name, "link": link ] }
init?(jsonObject: Any) {
guard let dictionary = jsonObject as? [String: Any],
let key1 = dictionary["key1"] as? String,
let key2 = dictionary["key2"] as? String,
let name = dictionary["name"] as? String,
let link = dictionary["link"] as? String else {
return nil
}
self.init(key1: key1, key2: key2, name: name, link: link)
}
}
Then you can do stuff like:
let object = try JSONSerialization.jsonObject(with: data)
var foo = Foo(jsonObject: object)!
Or:
foo.links?.append(Link(key1: "j", key2: "k", name: "l", link: "m"))
let data = try! JSONSerialization.data(withJSONObject: foo.jsonObject)
This was the solution:
var arrLinks : Array<Dictionary<String, Any>> = []
for link in myArray {
var dict : Dictionary<String, Any> = [:]
dict["key1"] = link.name
dict["key2"] = link.ghostBefore
dict["key3"] = link.ghostAfter
arrLinks.append(dict)
}
myDict["links"] = arrLinks as AnyObject