Put value LinkedHashMap multidimensional - java

I have the following structure:
import java.util.LinkedHashMap;
...
LinkedHashMap <String, Object>level0 = new LinkedHashMap<String, Object>();
LinkedHashMap <String, Object>level1 = new LinkedHashMap<String, Object>();
LinkedHashMap <String, Object>level2 = new LinkedHashMap<String, Object>();
LinkedHashMap <String, Object>level3 = new LinkedHashMap<String, Object>();
level1.put("level2", level2);
level2.put("level2", level3);
level0.put("level1", level1);
System.out.println(level0);
Output this:
{
level1={
level2={}
}
}
I need to set a value through a "path" (or something), would be something like this:
MapThisObject example = new MapThisObject(level0);
example.putValue("level1.level2", "string", "test");
example.putValue("level1.level2", "int", 1);
example.putValue("level1.level2", "object", new LinkedHashMap());
System.out.println(example.result());
/*output:
{
level1={
level2={
string="test",
int=1,
Object={}
}
}
}
*/
In other words, there is the possibility to put or set values ​​for "multidimensional objects" through a "path" (like Xpath)?

A simple example
public static void set(Map<String, Object> map, String path, Object value) {
String[] parts = path.split("\\.");
for(int i = 0; i < parts.length-1 ; i++) {
String key = parts[i];
Map<String, Object> map2 = (Map<String, Object>) map.get(key);
if (map2 == null) {
map.put(key, map2 = new LinkedHashMap<String, Object>());
}
map = map2;
}
map.put(parts[parts.length - 1], value);
}
set(example, "level1.level2.string", "test");
set(example, "level1.level2.int", 1);

From what you've described, it sounds like all you need is a map containing maps, nested to however many axes you're trying to select from.
The alternative would be to build your own tree structure, of course. Or to express it as an XML DOM tree, which would let you use standard XPath.

Related

Java 8 : how to extract a HashMap from a ArrayList of HashMap in Java 8 using streams?

I have the following sample result when I query a database :
dataList = [{ name : name1, rollno: rollno1 }, { name : name2, rollno: rollno2 } ]
I want to convert this list of hashmaps into a single hashmap using Java 8 streams.
I tried using Collectors.toMap() but i am not sure how to refer to rollNo as key and the hashmap as the value inside the toMap method.
Exected output :
{ rollno1 : { name : name1, rollno: rollno1 } , rollno2 : { name : name2, rollno: rollno2 } }
I tried doing it using a for each loop on the list and then adding the rollno as key to a hashmap and the hashmap as value itself of that rollno.
HashMap<String,HashMap<String,String>> newMap = new HashMap();
for(HashMap<String,String> record : dataList){
String key = record.get("rollno").toString();
newMap.put(key,record);
}
Is there a way to refactor this code using functional streams in Java 8?
Will using streams collect method give any performance advantage over the foreach for doing this?
Will appreciate any leads. Thanks
Here's complete example how to do it
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<HashMap<String, String>> input = new ArrayList<>();
HashMap<String, String> subinput1 = new HashMap<>();
subinput1.put("name", "name1");
subinput1.put("rollno", "rollno1");
input.add(subinput1);
HashMap<String, String> subinput2 = new HashMap<>();
subinput2.put("name", "name2");
subinput2.put("rollno", "rollno2");
input.add(subinput2);
HashMap<String, HashMap<String, String>> result = (HashMap<String, HashMap<String, String>>) input.stream()
.collect(Collectors.toMap(v -> (String) v.get("rollno"), e -> e));
System.out.println(result);
}
}
It iterates over a collection of HashMaps, takes the key in which it should be stored in the result HashMap, then it creates a Map of Maps where the key is the "rollno" from the input map, and value is the input map itself.
As I don't know the type of Object you are using, so I am performing this on String. But it is valid for any type of object.
HashMap result = (HashMap<String, HashMap<String, String>>) listOfHashMaps.stream()
.collect(Collectors.toMap(e-> e.get("roll"),e->e));
As type casting will help you to achieve this.
You can use Collectors.toMap with Function.identity() as below,
list.stream()
.collect(toMap(e->e.get("rollno"), Function.identity()));
public static void main(String[] args) {
List<HashMap<String, String>> input = new ArrayList<>();
HashMap<String, String> subinput1 = new HashMap<>();
subinput1.put("name", "name1");
subinput1.put("rollno", "rollno1");
input.add(subinput1);
HashMap<String, String> subinput2 = new HashMap<>();
subinput2.put("name", "name2");
subinput2.put("rollno", "rollno2");
input.add(subinput2);
//Test key conflict
HashMap<String, String> subinput3 = new HashMap<>();
subinput2.put("name", "name3");
subinput2.put("rollno", "rollno2");
input.add(subinput2);
System.out.println("input:"+ JSONObject.toJSONString(input));
HashMap<String, HashMap<String, String>> result = (HashMap<String, HashMap<String, String>>)
input.stream()
.collect(Collectors.toMap(
v -> (String) v.get("rollno"),
Function.identity(),(oldValue, newValue) -> newValue
));
//fastjson hashmap-toString use =
System.out.println(JSONObject.toJSONString(result));
}
Probably something like this:
Map<RollNo, List<Roll>> rollsPerType = rolls.stream()
.collect(groupingBy(Roll::getRollNo));
where Roll is the main object and RollNo is the property inside (since you didn't provide the actual definitions).

Setting values of a maps nested within a map

In one of my projects, I tried to set values in a nested map passed in to the parameter and return an updated map. The question is: assuming that I didn't know the map structure, how can I set values in a given path in a nested map?
I tried to do just that. I attempted to recursively call the set method but to no avail, instead of returning {age=1, human={lives=3, deaths=2}}, the method either returned {deaths=2} or null. However, please note that this is one of my many innumerable tries.
Here's one of my methods (other methods were deleted):
#SuppressWarnings("unchecked")
private static Map<Object, Object> setNested(YamlParser parser, List<String> paths, String key, Object value, Map<Object, Object> previousMap, int loops) {
Object found = parser.getObject(paths);
if (!(found instanceof Map))
return previousMap; // path is not nested
Map<Object, Object> theMap = (Map<Object, Object>) found;
theMap.put(key, value);
// .... ?
System.out.println(theMap);
return setNested(parser, paths, key, theMap, theMap, loops + 1);
}
A version with adding all the missing intermediate maps is even simpler:
private static Map<String, Object> setNested(Map<String, Object> map, List<String> keys, Object value) {
String key = keys.get(0);
List<String> nextKeys = keys.subList(1, keys.size());
Object newValue;
if (nextKeys.size() == 0) {
newValue = value;
} else if (!map.containsKey(key)) {
newValue = setNested(new LinkedHashMap<>(), nextKeys, value);
} else {
newValue = setNested((Map<String, Object>) map.get(key), nextKeys, value);
}
Map<String, Object> copyMap = new LinkedHashMap<>(map);
copyMap.put(key, newValue);
return copyMap;
}
I do not see what the YamlParser is good for in this example and I do not know what exactly you want to do. I think, it is about making a new map where the intermediate maps and the final (leaf) map have been copied and the new leaf map has a new value.
If this is not exactly what you need, you are free to modify it. Maybe it gives you a hint how to implement your own method:
public class Test {
private static Map<String, Object> setNested(Map<String, Object> map, List<String> keys, Object value) {
String key = keys.get(0);
List<String> nextKeys = keys.subList(1, keys.size());
if (nextKeys.size() == 0) {
Map<String, Object> copyMap = new LinkedHashMap<>((Map) map);
copyMap.put(key, value);
return copyMap;
} else if (!map.containsKey(key)) {
return map;
} else {
Map<String, Object> copyMap = new LinkedHashMap<>((Map) map);
Map<String, Object> nextMap = (Map<String, Object>) map.get(key);
copyMap.put(key, setNested(nextMap, nextKeys, value));
return copyMap;
}
}
public static void main(String[] args) {
Map<String, Object> map1 = new LinkedHashMap<>();
Map<String, Object> map2 = new LinkedHashMap<>();
map2.put("lives", 3);
map2.put("deaths", 2);
map1.put("age", 1);
map1.put("human", map2);
System.out.println(map1);
map1 = setNested(map1, Arrays.asList("human", "deaths"), 7);
System.out.println(map1);
}
}
Note: This method can insert new keys at the lowest level maps, but not at the intermediate maps.

android arraylist get hashmap position

I have an ArrayList HashMap like the one below.
ArrayList<HashMap<String, String>> mArrType = new ArrayList<>();
with the following values added into it
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("type", "TRIMMER");
map.put("request", "5");
map.put("actual", "0");
mArrType.add(map);
map = new HashMap<String, String>();
map.put("type", "HAND ROUTER");
map.put("request", "6");
map.put("actual", "0");
mArrType.add(map);
map = new HashMap<String, String>();
map.put("type", "AIR COMPRESSOR");
map.put("request", "6");
map.put("actual", "0");
mArrType.add(map);
Question is how can i get the position of a hashmap from arraylist. eg : hashmap with 'type' trimmer has a position 0 in arraylist, I want to retrieve the position value "0"
I'll write a small util method
private static int getTrimmerTypeMapPosition(ArrayList<HashMap<String, String>> mArrType) {
for (int i = 0; i < mArrType.size(); i++) {
HashMap<String, String> mp = mArrType.get(i);
if (mp.get("type").equals("TRIMMER")) {
return i;
}
}
return -1;
}
To make this method very generic, have "type" and "TRIMMER" as method params, so that you can just pass any key and value pairs to check with.
That's not efficiently possible with your data structure. You can either store the own position in each HashMap or loop through all entries and search for the one with the type you are looking for.
You can, of course, define another HashMap<String, Integer> which maps all your type strings to the corresponding ArrayList index.
Others answer is also correct, but you can do this thing using Java8 also.
E.g.:
int index = IntStream.range(0, mArrType.size()).
filter(i -> mArrType.get(i).get("type").equals("TRIMMER"))
.findFirst().getAsInt();

What's the best way to sum two Map<String,String>?

I have the following maps.
Map<String,String> map1= new HashMap<String, String>(){{
put("no1","123"); put("no2","5434"); put("no5","234");}};
Map<String,String> map1 = new HashMap<String, String>(){{
put("no1","523"); put("no2","234"); put("no3","234");}};
sum(map1, map2);
I want to join them to one, summing up similar keyed values together. What;s the best way I could do it using java 7 or guava libraries ?
expected output
Map<String, String> output = { { "no1" ,"646"}, { "no2", "5668"}, {"no5","234"}, {"no3","234" } }
private static Map<String, String> sum(Map<String, String> map1, Map<String, String> map2) {
Map<String, String> result = new HashMap<String, String>();
result.putAll(map1);
for (String key : map2.keySet()) {
String value = result.get(key);
if (value != null) {
Integer newValue = Integer.valueOf(value) + Integer.valueOf(map2.get(key));
result.put(key, newValue.toString());
} else {
result.put(key, map2.get(key));
}
}
return result;
}
try this
Map<String, List<String>> map3 = new HashMap<String, List<String>>();
for (Entry<String, String> e : map1.entrySet()) {
List<String> list = new ArrayList<String>();
list.add(e.getValue());
String v2 = map2.remove(e.getKey());
if (v2 != null) {
list.add(v2);
}
map3.put(e.getKey(), list);
}
for (Entry<String, String> e : map2.entrySet()) {
map3.put(e.getKey(), new ArrayList<String>(Arrays.asList(e.getValue())));
}
Java 8 introduces Map.merge(K, V, BiFunction), which makes this easy if not particularly concise:
Map<String, String> result = new HashMap<>(map1);
//or just merge into map1 if mutating it is okay
map2.forEach((k, v) -> result.merge(k, v, (a, b) ->
Integer.toString(Integer.parseInt(a) + Integer.parseInt(b))));
If you're doing this repeatedly, you're going to be parsing and creating a lot of strings. If you're generating the maps one at a time, you're best off maintaining a list of strings and only parsing and summing once.
Map<String, List<String>> deferredSum = new HashMap<>();
//for each map
mapN.forEach((k, v) ->
deferredSum.computeIfAbsent(k, x -> new ArrayList<String>()).add(v));
//when you're done
Map<String, String> result = new HashMap<>();
deferredSum.forEach((k, v) -> result.put(k,
Long.toString(v.stream().mapToInt(Integer::parseInt).sum())));
If this summing is a common operation, consider whether using Integer as your value type makes more sense; you can use Integer::sum as the merge function in that case, and maintaining lists of deferred sums would no longer be necessary.
Try this
Map<String,String> map1= new HashMap<String, String>(){{
put("no1","123"); put("no2","5434"); put("no5","234");}};
Map<String,String> map2 = new HashMap<String, String>(){{
put("no1","523"); put("no2","234"); put("no3","234");}};
Map<String,String> newMap=map1;
for(String a:map2.keySet()){
if(newMap.keySet().contains(a)){
newMap.put(a,""+(Integer.parseInt(newMap.get(a))+Integer.parseInt(map2.get(a))));
}
else{
newMap.put(a,map2.get(a));
}
}
for(String k : newMap.keySet()){
System.out.println("key : "+ k + " value : " + newMap.get(k));
}

Get values of Map from a Map

In my code i have a
Map<String,Map<String,customObject>>
I am not sure how to iterate over this map and get the values from it.
What i am trying to do here is get the enclosing Map by passing in the key to the external Map.
When i get the enclosing map i need to iterate over it and get key and value from it.
Can you please let me know how i can do this as i am kind of stuck here.
Any example or code of a similar type can be of a great help to understand it better.
Thanks
Vikeng21
You can use the entry set of both Maps. something like this:
Map<String,Map<String,String>> map1 = ...
Set<Entry<String,Map<String,customObject>>> entrySet1 = map1.entrySet();
for (Entry<String, Map<String, customObject>> entry1 : entrySet1) {
Map<String,String> map2 = entry1.getValue();
Set<Entry<String, customObject>> entrySet2 = map2.entrySet();
for (Entry<String, customObject> entry2 : entrySet2) {
System.out.println(entry1.getKey() +" -> "+entry2.getKey()+" -> "+entry2.getValue());
}
}
To iterate over hashmap entries...
for (Map.Entry<String, Map<String, Object>> ent : hashmap.entrySet())
{
//ent.getKey(); is the key [String]
//ent.getValue(); is the value [Map<String, Object>]
}
Now work out from there, it's basically the same.
I am not sure how to iterate over this map and get the values from it
You would iterate over the map's values like with any maps - see below an example that uses such a structure.
Map<String, CustomObject> innerMap = new HashMap<String, CustomObject> ();
innerMap.put("abc", new CustomObject());
Map<String, Map<String, CustomObject>> externalMap = new HashMap<String, Map<String, CustomObject>> ();
externalMap.put("map1", innerMap);
//iterate over all the maps contained in externalMap
for (Map<String, CustomObject> inner : externalMap.values()) {
System.out.println(inner);
}
If you also need to access the keys, you can iterate over the entry set:
for (Entry<String, Map<String, CustomObject>> e : externalMap.entrySet()) {
System.out.println(e.getKey()); //map1
System.out.println(e.getValue()); //innerMap
}
I think this example will give your answer....
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class MapInMap {
public static void main(String[] args) {
Map<String, MyObj> innerMap01 = new HashMap<String, MyObj>();
Map<String, MyObj> innerMap02 = new HashMap<String, MyObj>();
innerMap01.put("OneOne", new MyObj());
innerMap02.put("TwoOne", new MyObj());
Map<String, Map<String, MyObj>> maps = new HashMap<String, Map<String, MyObj>>();
maps.put("One", innerMap01);
maps.put("Two", innerMap02);
for (Entry<String, Map<String, MyObj>> map : maps.entrySet()) {
for (Entry<String, MyObj> innerObject : map.getValue().entrySet()) {
// your logic
}
}
}
}
class MyObj {
int i;
}

Categories

Resources