Retrieve data from HashMap<String, ArrayList<HashMap<String, String>>> - java

I have data that is currently being stored in a HashMap<String, ArrayList<HashMap<String, String>>>
I am looking to get a certain value of the ArrayList<HashMap<String, String>>> where the Key is of value "FieldName1". Alternatively, if I try to get the ArrayList Index would it return me both the Key,value pair?
Ultimately what I am trying to do is compare this specific value where the key is "fieldName1" to the other "fieldnName1"'s within the arrayList. If that is the case, should I take the index position of the ArrayList instead?
Thanks!
edit***
The reason HashMap<String, ArrayList<HashMap<String, String>>> is used is because what I am trying to do is process an excel file. I read each row in my excel file and based off the first cell in the row (ex A1) it maps to a certain DB Table. I then have a layoutFile that stores the DB Table name and its corresponding field names. Each new row in that excel file is a different field name.

Alternatively, if I try to get the ArrayList Index would it return me
both the Key,value pair?
This will return the hahsmap object and no the key value pair.
Ultimately what I am trying to do is compare this specific value where
the key is "fieldName1" to the other "fieldnName1"'s within the
arrayList.
Dont know what you meant by this but in a hashmap you cannot have duplicate keys. So you cannot have two "FieldName1" inside the hashmap.
To get the value of "FieldName1" you to traverse the initial hashmap to get the arraylist which contains the hashmap that has the required field. So first get the required arraylist from the hashmap, from the arraylist get the required hashmap and from the hashmap you can get the appropriate field.

I would say you can use the index of the ArrayList, which will return the HashMap object, and then compare the key and value of the one HashMap with the other one's key and value.

Related

Iterating over a multivaluemap from arraylist values java?

I have some keys in an arraylist. Now, I want to search for values from a multivaluemap by passing those keys and saving the value in a list. What would be the best way to approach it. Thanks.
MultiValueMap<String,String>subcountyid = new MultiValueMap<String,String>();
subcountyid = d.getSubcountyid();
ArrayList<String>subcountynames = (ArrayList<String>) subcounties.get(county_id);
So, basically all my keys are in the subcountynames arraylist and I want to search through my subcountyid map for the value of each keys. How should i approach this situation.

Merge multiple Map and differentiate their value with different keys in java

EDIT: I have three maps with following format:
map1.put("aaa",1);
map1.put("bbb",1);
map1.put("ccc",1);
map2.put("aaa",2);
map2.put("bbb",3);
map3.put("ccc",6);
map3.put("ddd",6);
Now I want the result as list with following format:
[{"id":"aaa","map1count":"1","map2count":"2","map3count":"0"},
{"id":"bbb","map1count":"1","map2count":"0","map3count":"0"},
{"id":"ccc","map1count":"1","map2count":"0","map3count":"6"},
{"id":"ddd","map1count":"0","map2count":"0","map3count":"6"}]
How can I do this in java, help me to figure out.
you can create a class Count, with three members ex. mapCount1, mapCount2, mapCount3. Now create result map
HashMap<String, Count>() result = new HashMap<>();
Now you need to traverse these 3 maps, check map.containsKey(key), here key might be "aaa", "bbb", "ccc" etc. Now traverse first map, since map doesnt not contain any key, add key with value to result map. Now do this for 2nd, 3rd map, If key is there and then get value of count object and update count for mapCount2/3. At last for each key you can print count specific to each map.

Alternative for hashmap [duplicate]

This question already has an answer here:
Java: Get hashmap value [closed]
(1 answer)
Closed 6 years ago.
I have a hashmap which has several values and it override the same keys, is there any solution for this? this is my Hashmap
HashMap<String, String> meMap=new HashMap<String, String>();
meMap.put("Jack","John");
meMap.put("Jack","Jacob");
it will override the first one.
You can use get function: meMap.get("Color3");. You can access to different methods of maps in Java here:
https://docs.oracle.com/javase/8/docs/api/java/util/Map.html
A Map is a Key - Value store, meaning that you access values in the Map by specifying its Key. So you would do:
meMap.get("Color3");
You have to use get() for single element:
Toast.makeText(context, map.get("Color3"), Toast.LENGTH_SHORT).show();
You can not have multiple values for one key in HashMap.
If you need to store multiple values for same key then you should use map insted of HashMap.
Map has combination of Key and value. If you want a value, you have to call it's key.
System.out.println(meMap.get("Color3"));
Resource Link:
Class HashMap
From your comment,it seems you want to store multiple values for a single key.In that case you can store your values in a List and then add that list to the map,as shown below:
HashMap<String, List<String>> meMap=new HashMap<String, List<String>>();
List<String> lst=new ArrayList<String>();//List to store colors
lst.add("Red");
lst.add("Blue");
lst.add("Green");
lst.add("White");
//add the key and list to map
meMap.put("Color",lst);
//to get the value at 3rd position,use get(2),as index starts from 0.
System.out.println(meMap.get("Color").get(2));

Data updataion for same key in Hashmap

I am writing program which reads data from excel file which contains following two columns:
1- Unit code 2- quantity
Unit Code contains all units which maybe repeated also. I want to add all quantities present for same unit code without loosing any of them. I want to store this whole data in hashmap.
Please help me out.
Thanks in Advance.
You can't put duplicate keys in HashMap. But you can try something like this
Map<Key,List<Values>> map=new HashMap<>();
Now there can be list of values for selected key.
Eg:
Map<String,List<String>> map=new HashMap<>();
List<String> list=new ArrayList<>();
list.add("a");
list.add("b");
list.add("b");
map.put("a",list);
System.out.println(map);
Output:
{a=[a, b, b]}
You can store your data in a HashMap. The 'unit' can be taken as keys and the sum of 'quantities' as values. You can insert a key-value pair in your HashMap as soon as you find a 'unit' first time. Corresponding quantity will be stored as value. Now again when inserting the next record from excel, check if the new 'unit' already exists in the HashMap. If yes then the new 'quantity' should be summed to the corresponding value. If no, then a new entry will be put in the map.
The code is as follows:
Map<String,Integer> map=new HashMap<>();
//Open the file for reading using some excel API
//Read the unit and quantity line by line and assign them in `unit` and `quantity` variables
String unit=""; // Read actual unit value from file
int quantity=0; // Read actual quantity value from file
if(map.containsKey(unit)){
map.put(unit, map.get(unit)+quantity);
}
The map does not allow duplicate keys. So when you again put the same key in the map, it will overwrite the existing entry in the map. Thus the resultant map will contain the units and the sum of corresponding quantities as entries.

Creating a HashMap as an index for title keywords to improve search efficiency

I have a custom class Disks which stores various information of CDs such as their Title, Length, Artist etc. These Disks objects are stored in an ArrayList which can only have elements of Disks added. I am using a method to search for these objects based on matching their title. It takes a user input and then goes through each element of the list and compares the user keyword and the Title of the CD. If it is a complete match, its information is then returned to the user.
I want to change this search mechanization slightly by incorporating a HashMap. I am looking to tokenize each Disks Title and then create a mapping entry for the keyword.
Here is an example: The word "Cars" appears in the titles of the ArrayList elements at position 0,5,7. I want to be able to create a mapping entry for "Cars" which will be a list [0,5,7]. If another element is added to the ArrayList at position 10 with "Cars" in the title, how would I amend the old mapping entry so the new list would be [0,5,7,10]?
In the end I want the user to search for title keywords “Loud Cars”. I will first find "loud" in the index to get a list of [0,7,5] (for example), and then find "cars" to get a list of [0,5,7,10]. Then, I will find where these lists intersect and return the ArrayList elements that correspond to these locations.
My current HashMap declartion looks like this: public HashMap<String, ArrayList<Integer>> map = new HashMap<>(); however even when the Key is different, the values stored in the ArrayList are the same because there is only one of them.
My Disks ArrayList is: public ArrayList<Disks> items; Would there be a way to incorporate this ArrayList into the Value of the HashMap?
Add a new value to the index entry for "Cars"
map.get("Cars").add(10);
Safe way to do this (key = "Cars", index = 10):
ArrayList<Integer> entry = map.get(key);
if (entry == null) {
entry = new ArrayList<Integer>();
map.put(key, entry);
}
entry.add(index);
Instead of using
HashMap<String, ArrayList<Integer>>
I'd recommend
HashMap<String, HashSet<Integer>>
Which is automatically avoids duplicates.
When you search for multiple words, use retainAll to build the intersection of multiple sets (but copy the first set because retainAll is destructive):
Set<Integer> resultSet = new HashSet<Integer>();
resultSet.addAll(map.get("Cars"));
resultSet.retainAll(map.get("Loud"));
You would need to create a new ArrayList of Integer for every string mapping to a value. The first time an entry is used, you create a new list (You must check that the string maps to null), and add the value of the index that the new Disk entry will be stored at in your ArrayList of Disls to you ArrayList of Integers. Any time the string maps to a non-empty list, then you just add the index (where it is in the Disk ArrayList) to the ArrayList of Integer.
Honestly, I think the best way for you to scale your solution is by using bloom filters or something sophisticated like this. This would require you to create complex hash codes, manage false positives, among other things.
Having that said, based on your design, I think what you can simply have a hash map pointing to the Disks objects that are also stored on the array list.
public HashMap<String, ArrayList<Disks>> map
For the keyword "cars", you have a list of Disks objects. For the keyword "loud" you have another list of Disks objects. Just take both lists and find the intersection, using the retainAll() method.
Make sure to override hashCode() and equals() in Disks so all collections will work fine.

Categories