Nested HashMap:
HashMap<String,HashMap<String,String>> outerMap=new HashMap<String, HashMap<String, String>>();
HashMap<String,String> innerhashMap=new HashMap<String, String>();
innerhashMap.put("aaa","AAA");
outerMap.put("111",innerhashMap);
innerhashMap.put("aaa","AAA");
outerMap.put("222",innerhashMap);
I want outer map keys list,inner map keys list and innermap values list
for ( String outerkey : outerMap.keySet() ) {
HashMap<String,String> innerHashMap = (HashMap<String,String>) outerMap.get(outerKey)
for ( String innerKey : innerHashMap.keySet() ) {
String innerValue = (String) innerHashMap.get(innerKey);
//... Process them
}
}
HashMap<String,HashMap<String,String>> outerMap = new HashMap<>();
HashMap<String,String> innerhashMap = new HashMap<>();
innerhashMap.put("aaa","AAA");
outerMap.put("111",innerhashMap);
innerhashMap.put("aaa","AAA");
outerMap.put("222",innerhashMap);
outerMap.forEach((k, v) -> {
System.out.println("OUTER KEY: " +k);
v.forEach((kk, vv) -> {
System.out.println("INNER KEY: " +kk+ " INNER VALUE: " +vv);
});
});
Related
I've been able to get a Jsonarray as a response from using RestAssured. But don't know how to put it in a Hashmap with a String that shows the name of the metric and an Integer showing the corresponding value.
Response from RestAssured:
{
"results": [
{
"maximum": 3.858
},
{
"minimum": 5.20
},
{
"number": 249
}
]
}
What I want is a Map that contains max, min & number and their corresponding values.
Ex: {"max": 3.858, "min": 5.20, "number": 249 }
So far I have tried the below code, but it doesn't seem to work. Any help would be appreciated.
public static HashMap<String, String> getMinMaxCount(String URL, String query) {
JsonObject res = getNewRelicAPIResponse(URL, query);
HashMap<String, String> map = null;
//System.out.println("Response is : " + res);
JsonArray metricsArray = res.get("results").getAsJsonArray();
int arraySize = metricsArray.size();
String[] strArr = new String[arraySize];
for (int i = 0; i < arraySize; i++) {
strArr[i] = String.valueOf(metricsArray.get(i));
//Create a Hashmap & append the Max, Min & Count
map = new HashMap<>();
// String[] tokens = strArr[i].split(":");
// String[] tokens2 = tokens[1].split("}");
map.put("max",strArr[i]);
}
return map;
}
That's what I did:
String res = ... //get response from API
JsonPath jsonPath = new JsonPath(res);
List<Map<String, Object>> list = jsonPath.getObject("results", new TypeRef<>(){});
System.out.println(list);
//[{maximum=3.858}, {minimum=5.2}, {number=249}]
Map<String, String> resultMap = new HashMap<>();
for (Map<String, Object> mapInList : list) {
for (Map.Entry<String, Object> entryOfMapInList : mapInList.entrySet()) {
resultMap.put(entryOfMapInList.getKey(), entryOfMapInList.getValue().toString());
}
}
System.out.println(resultMap);
//{number=249, maximum=3.858, minimum=5.2}
The for loop in the code is need to be replaced by java8 streams. How can I resolve this?
public class Conversion {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Person class (name, age , gender)
//Employee class (name, gender)
Map<String, List<Person>> mapPerson = new HashMap<String, List<Person>>();
Map<String, List<Employee>> employeeMap = new HashMap<>();
List<Person> personList1 = new ArrayList<Person>();
personList1.add(new Person("Adam", 22, "Male"));
personList1.add(new Person("Alon", 21, "Female"));
List<Person> personList2 = new ArrayList<Person>();
personList2.add(new Person("Chad", 23, "Male"));
personList2.add(new Person("Daina", 21, "Female"));
List<Person> personList3 = new ArrayList<Person>();
personList3.add(new Person("Mark", 22, "Female"));
personList3.add(new Person("Helen", 25, "Male"));
mapPerson.put("A", personList1);
mapPerson.put("B", personList2);
mapPerson.put("C", personList3);
for (Map.Entry<String, List<Person>> entry : mapPerson.entrySet()) {
String key = entry.getKey();
List<Person> values = entry.getValue();
System.out.println("Key = " + key);
System.out.println("Values = " + values + "n");
}
System.out.println("******************************************");
employeeMap = getEmployeeMap(mapPerson);
for(Map.Entry<String, List<Employee>> listMap : employeeMap.entrySet())
{
System.out.println("Key : " + listMap.getKey());
System.out.println("Values : " + listMap.getValue());
System.out.println("===============");
}
}
This is the for loop in the function for iteration of MAP. Instead of this I have to use streams.
public static Map<String, List<Employee>> getEmployeeMap(Map<String, List<Person>> personMap) {
Map<String, List<Employee>> employeeMap = new HashMap<>();
for(Map.Entry<String, List<Person>> listMap : personMap.entrySet()) {
String key = listMap.getKey();
List<Person> personList = listMap.getValue();
List<Employee> employeeList = personList.stream().map(p-> new Employee(p.getName(), p.getGender())).collect(Collectors.toList());
employeeMap.put(key, employeeList);
}
return employeeMap;
}
}
You can use Collectors.toMap and modify the value inside it.
Map<String, List<Employee>> employeeMap =
mapPerson.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey,
e -> e.getValue().stream().map(p-> new Employee(p.getName(), p.getGender())).collect(Collectors.toList()));
This question already has answers here:
Why does my ArrayList contain N copies of the last item added to the list?
(5 answers)
Closed 5 years ago.
I am trying to create a HashMap in a HashMap so it will be easier for me to access elements of it in the future as shown below.
The problem is it only repeating the last elements of the while loop and not the rest of it.
HashMap<String,String> result = new HashMap<>();
HashMap<Integer, HashMap<String,String>> fr = new HashMap<>();
int i = 0;
try {
ResultSet rq = qexec.execSelect();
// ResultSetFormatter.out(System.out, rq, query);
// get result from SPARQL query
while (rq.hasNext()) {
QuerySolution soln = rq.next();
id = soln.getLiteral("?id").getLexicalForm();
//...
result.put("id",id);
//...
if (soln.getLiteral("?wateruse") != null) {
wateruse = soln.getLiteral("?wateruse").getLexicalForm();
//...
result.put("wateruse",wateruse);
} else {
System.out.println("NO");
}
fr.put(i, result);
i++;
}
} finally {
qexec.close();
}
This is how the result should be:
John001
High usage
John002
John003
Smith001
Moderate
Smith002
Smith003
...
Kevin001
Low usage
But fr only repeats Kevin001 and Low usage without the rest.
I've tried to put fr.put(i,result) outside the loop but that still does not give the correct result.
EDIT
I tried to print all elements from fr that shows the repeating elements.
finally {
qexec.close();
}
for (int index : fr.keySet()) {
for(Map.Entry<String, String> entry :result.entrySet()) {
System.out.println(index + " = " + entry.getKey() + " : " + entry.getValue());
}
}
UPDATE - SOLUTION
Declare HashMap inside the loop as mentioned in comments below.
To print nested HashMap, no need to use result.
I did as shown below and it prints both outermap and innermap as well.
for (int k=0; k < fr.size(); k++) {
HashMap<String,String> innermap = fr.get(k);
for(Map.Entry<String, String> e : innermap.entrySet()) {
System.out.println(k + " = " + e.getKey() + " : " + e.getValue());
}
}
You're adding the same result map to your parent map each time through the loop. Create a new instance of result each time through the loop:
Map<String, String> result = new HashMap<>();
Map<Integer, Map<String, String>> fr = new HashMap<>();
int i = 0;
try {
ResultSet rq = qexec.execSelect();
while (rq.hasNext()) {
// Create your new HashMap inside the loop:
result = new HashMap<>();
QuerySolution soln = rq.next();
id = soln.getLiteral("?id").getLexicalForm();
//...
result.put("id",id);
//...
if (soln.getLiteral("?wateruse") != null) {
wateruse = soln.getLiteral("?wateruse").getLexicalForm();
//...
result.put("wateruse",wateruse);
}
else {
System.out.println("NO");
}
fr.put(i,result);
i++;
}
}
To print the results from fr an its nested map, you can do something like this:
for (Map<String, String> map : fr.values()) {
for(Map.Entry<String, String> e : map.entrySet()) {
System.out.println(index + " = " + e.getKey()
+ " : " + e.getValue());
}
}
Try this a small change here, place the "result" map creation in while loop
Map<Integer, Map<String, String>> fr = new HashMap<>();
int i = 0;
try {
ResultSet rq = qexec.execSelect();
while (rq.hasNext()) {
Map<String, String> result = new HashMap<>();
QuerySolution soln = rq.next();
id = soln.getLiteral("?id").getLexicalForm();
//...
result.put("id",id);
//...
if (soln.getLiteral("?wateruse") != null) {
wateruse = soln.getLiteral("?wateruse").getLexicalForm();
//...
result.put("wateruse",wateruse);
}
else {
System.out.println("NO");
}
fr.put(i,result);
i++;
}
}
This for loop to print elemenets:
for (int i=0;i< fr.size();i++){
Map<String,String> element= fr.get(i);
// use the element here.
}
I am iterating 500,000 items using for loop , after 300, 000 item i am getting Out of memory , I have also tried to split loop in 100,000 still, but that also didn't work. When I increased memory in runConfig-xms10g, its working fine. Can anyone tell me how to split and free memory or any other way to iterate large number of recods.
protected Map<Long, Map<String, String>> getExistingItems()
{
Map<Long, Map<String, String>> items = new HashMap<Long, Map<String, String>>();
for (Item item : itemMaster.getItems()) {
if (item.getExpirationDate() == null && !items.containsValue(item.getItemId())) {
Map<String, String> item_hash = new HashMap<String, String>();
if (item.getEffectiveDate() != null)
item_hash.put("effectiveDate", sdf.format(item.getEffectiveDate().getTime()));
for (ItemMasterAttributeValue attrVal : item.getItemMasterAttributeValues()) {
item_hash.put(attrVal.getId().getItemMasterAttribute().getCode(), attrVal.getValue());
}
items.put(item.getItemId(), item_hash);
}
}
after splitting below is code:
protected Map<Long, Map<String, String>> getExistingItems()
{
Map<Long, Map<String, String>> items = new HashMap<Long, Map<String, String>>();
java.util.Date date = new java.util.Date();
System.out.println("before getItems : " + new Timestamp(date.getTime()));
Collection<Item> allItems = itemMaster.getItems();
System.out.println("after getItems : " + new Timestamp(date.getTime()));
int split = (allItems.size() / 100000);
Map<Integer, Collection<Item>> splitMap = new HashMap<Integer, Collection<Item>>();
Collection<Item> tempCollection = new ArrayList<Item>();
int splitKey = 1;
int key = 1;
for(Item item : allItems)
{
tempCollection.add(item);
if(splitKey / 100000 >= key && splitKey % 100000 == 0)
{
splitMap.put(key, tempCollection);
tempCollection = new ArrayList<Item>();
key++;
}
splitKey++;
}
splitMap.put(key, tempCollection);
System.out.println("map size " + splitMap.size());
for(int i = 1; i <= split + 1; i++)
{
System.out.println("i is :" + i + " " + Calendar.getInstance().getTime());
for(Item item : splitMap.get(i))
{
if(!items.containsKey(item.getItemId()))
{
Map<String, String> item_hash = new HashMap<String, String>();
if (item.getEffectiveDate() != null)
item_hash.put("effectiveDate", sdf.format(item.getEffectiveDate().getTime()));
for (ItemMasterAttributeValue attrVal : item.getItemMasterAttributeValues()) {
item_hash.put(attrVal.getId().getItemMasterAttribute().getCode(), attrVal.getValue());
}
items.put(item.getItemId(), item_hash);
}
}
System.gc();
}
I have a for each loop running as:
for (System system : val.keySet())
{
do something
}
Is there an alternate method to write this?
Here are some ways how you can loop through a map:
1- Using for-Each loop:
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
2- Iterating over keys or values
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//iterating over keys only
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
//iterating over values only
for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
3- Using iterator:
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, Integer> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}