Currently I have 6 HashMaps that contain the name of cities and values of different categories but I need to sum up the values of each city for every category, that is:
HashMap<String, Integer> HeatHMap = new HashMap<>();
HashMap<String, Integer> DaylightHMap = new HashMap<>();
HashMap<String, Integer> PrecitipationHMap = new HashMap<>();
HashMap<String, Integer> DaylightHMap = new HashMap<>();
HashMap<String, Integer> WindHMap = new HashMap<>();
HashMap<String, Integer> MoistureHMap = new HashMap<>();
Where HeatHMap contains:
Cities Values
Tucson 23
Hermosillo 47
Boulder 25
and DaylightHMap contains:
Cities Values
Tucson 43
Hermosillo 37
Boulder 75
Right now, I need to add up the values of each city, i.e., Hermosillo, for each category and save the values into another HashMap, so the result would be something like:
Cities Values
Tucson 78 = (23+43+...+n)
Hermosillo 160 = (47+37+...+n)
....
I was thinking in adding every HashMap into an ArrayList and then get access to each city but I realized having a HashMap into a list would not be a good approach to tackle this problem. So far, I have:
public void verifyTheWinner(
HashMap <String, Integer> Table1, HashMap <String, Integer> Table2,
HashMap <String, Integer> Table3, HashMap <String, Integer> Table4,
HashMap <String, Integer> Table5, HashMap <String, Integer> Table6)
{
List<HashMap> categories = new ArrayList<HashMap>();
categories.add(Weather);
categories.add(SeaWeather);
categories.add(Rainfall);
categories.add(Sunshine);
categories.add(Prices);
categories.add(AvgStd);
HashMap<String, Integer> citiesAndValuesTotal= new HashMap<>();
for (int i=0; i<categories.size(); i++){
......
}}
My questions are:
How can I perform arithmetic operations such as addition of values for each city and then saving them into a new HashMap?
Is there another Collection that I can use to accomplish this goal or is HashMap the best approach to solve this problem?
Thanks for your support, please ask me for more details if you need them. Every answer is welcome.
You would be better off with a better data structure, such as a class that incorporated all the wanted details for any given city:
public class CityWeather {
private String name;
private int heat;
private int daylight;
// ...
private int moisture;
// ...
}
Then you only need one map, say
HashMap<String, CityWeather> weatherMap = new HashMap<>();
That one map can meet your needs currently served by all the other ones, including the citiesAndValuesTotal map you want to create. For the last, all you need to do is add a method to CityWeather, something like this:
int meaninglessWeatherSum() {
return heat + daylight + ... + moisture;
}
Then you don't need to do anything special to perform the computation -- it's right there whenever you want it.
I would make a custom object (maybe called City) to handle something like this.
public class City {
public String name;
public Integer heat;
public Integer dayLight;
public Integer precipitation;
public Integer wind;
public Integer moisture;
public Integer getTotal() {
return heat + dayLight + precipitation + wind + moisture;
}
}
You could have a single map, from the city name to the City object.
Also I think you mispelled "precipitation", have two "day light" maps and your parameter names don't match what you use in your verifyTheWinner method.
I appreciate very much your solutions John Bollinger and Adam Rosni, but I was able to solve this problem by using the solution from this post
how to merge more than one hashmaps also sum the values of same key in java that suited better my needs.
My solution was the following:
//Solution from Melike Ttkn from the mentioned link
public HashMap<String, Integer> mergeAndAdd(ArrayList<HashMap<String, Integer>> maplist) {
HashMap<String, Integer> result = new HashMap<>();
for (HashMap<String, Integer> map : maplist) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer current = result.get(key);
result.put(key, current == null ? entry.getValue() : entry.getValue() + current);
}
}
return result;
}
Then I added my method to make a listArray of HashMaps like this:
public ArrayList<HashMap<String,Integer>> makeAlist(HashMap<String,Integer> Values1, HashMap<String, Integer> Values2, HashMap<String, Integer> Values3,
HashMap<String, Integer> Values4, HashMap <String, Integer> Values5, HashMap<String, Integer> Values6){
ArrayList<HashMap<String,Integer>> mergedCategories = new ArrayList<>();
mergedCategories.add(Values1);
mergedCategories.add(Values2);
mergedCategories.add(Values3);
mergedCategories.add(Values4);
mergedCategories.add(Values5);
mergedCategories.add(Values6);
return mergedCategories;
}
And lastly, I just called this method:
System.out.println(mergeAndAdd(makeAlist(Values1, Values2, Values3, Values4, Values5, Values6)));
Related
I came across a problem of sorting a HashMap<String, Integer> based on values. But, I came across many articles over the internet which first created a linkedList/arraylist of Map.Entry<String, Integer> and then sorted it on the basis of value.
Below is the code snippet showing sorting of a hashmap on the basis of key.
// Java program to sort hashmap by values
import java.util.*;
import java.lang.*;
public class Main {
// function to sort hashmap by values
public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<String, Integer> > list =
new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
// Driver Code
public static void main(String[] args)
{
HashMap<String, Integer> hm = new HashMap<String, Integer>();
// enter data into hashmap
hm.put("Math", 98);
hm.put("Data Structure", 85);
hm.put("Database", 91);
hm.put("Java", 95);
hm.put("Operating System", 79);
hm.put("Networking", 80);
Map<String, Integer> hm1 = sortByValue(hm);
// print the sorted hashmap
for (Map.Entry<String, Integer> en : hm1.entrySet()) {
System.out.println("Key = " + en.getKey() +
", Value = " + en.getValue());
}
}
}
My question is, why is there a need to convert hashmap to list of entrySet and then sort it?
According to my understanding, we should be able to directly sort it based on the values just like any POJO class on a certain parameter. There shouldn't be any need to convert it into some collection and then sort it.
Instead of placing the values in a map and sorting, create a record or class to hold the data. After populating with instances of the class, sort the list prior to placing in the map. It is necessary to use a LinkedHashMap to preserve the sorted order. Otherwise, the normal behavior of a HashMap will most likely disturb the sort.
record Rec(String getName, int getVal) {}
List<Rec> list = List.of(
new Rec("Math", 98), new Rec("Data Structure", 85),
new Rec("Database", 91), new Rec("Java", 95),
new Rec("Operating System", 79), new Rec("Networking", 80));
Map<String, Integer> result = list.stream()
.sorted(Comparator.comparing(Rec::getVal))
.collect(Collectors.toMap(Rec::getName, Rec::getVal,
(a, b) -> a, LinkedHashMap::new));
result.entrySet().forEach(System.out::println);
prints
Operating System=79
Networking=80
Data Structure=85
Database=91
Java=95
Math=98
The issue, imo, is that not just any map lends itself to be sorted on values. Sorting a regular HashMap after the fact on values would be fruitless since inserting the keys would still perturb the order. Sorting before wouldn't help. A TreeMap won't work since it is designed to sort on keys and any supplied Comparator sorts using a KeyExtractor. And I suspect that any Map that might allow this would still convert to another data structure to accomplish the sort and return some LinkedHashMap or equivalent.
I have multiple maps (like 100 or more). I want to put them to list like this:
ArrayList<HashMap> mapsList = new ArryaList(Arrays.asList(map1, map2, map3 ..., map100);
is there a way to put all this in here in one loop to avoid writing the same name one hundred times changing only number in it?
#EDIT
I have this map like this:
HashMap<String, Integer> map1 = new HashMap<>();
map1.put("x", 47);
...
nextmap here...
Use a method to generate a map, insert the value and add it in the List.
public Map<String, Integer> newMap(Values... values){
Map<String, integer> map = new HashMap<>();
for(Values v : values){
map.put(v.getKey(), v.getValue());
}
list.add(map);
return map;
}
Where Values hold both key and value
class Values{
String key;
Integer value;
public Values(String key, Integer value){
...
}
//getters;
}
That way, you don't need to play with map1, map2, map3, .. but simply call that method.
newMap(new Values("x", 47), new Values("y", 12));
newMap(new Values("x", 4));
...
My last attempt was poorly explained, so recreated with hopefully a clearer explanation.
I have a Collection that has the following data structure.
LinkedHashMap<String, String> currentHashMap = new LinkedHashMap<String, String>();
currentHashMap.put("id","12345");
currentHashMap.put("firstName","John");
currentHashMap.put("lastName","Doe");
haystack.add(currentHashMap);
*repeat for roughly 250,000 more entries
I need to search the HashMaps within each list entry, check if the key exists, and if it has the corresponding value. If it does, I want to return all the values in that particular hashmap
This is the code that I'm currently using.
private TreeSet<String> searchWithinCollection(List<LinkedHashMap<String, String>> haystack, String needle, String needleKey) {
TreeSet<String> returnValueSet = new TreeSet<>();
for (LinkedHashMap<String, String> mappedData : haystack) {
System.out.println(mappedData.values());
for (Entry<String, String> specificData : mappedData.entrySet()) {
if (needleKey.equals(specificData.getKey()) && needle.equals(specificData.getValue())) {
//where I want to collect the values within the current hashmap, as it's satisfied the search criteria
//the current code would only return the current key/balue pair, even if all values were collected outside the for loop. Instead, I'd want to collect "12345", "John", "Doe" if needleKey = id and needle = 12345.
}
}
}
return returnValueSet;
}
What I'd want is if a key in the current collection equaled "id", and it's value equaled "12345", then I could returned all values within that particular collection (and to use the example above, would be "12345", "John", "Doe").
The best I can do is return the first key/value, which isn't very helpful obviously. Attempting to capture all values in mappedData outside the second for loop brought no result (either with creating a new instance of a Collection, clone, or what have you.). I also found nothing that could manually advance the pointer in the for each loop to manually capture all values in the Collection.
EDIT: If it helps, where there is a System.out.println(mappedData.values()); I can get the values I'm expecting, but only the id in the example is return if values() is accessed within the if statement.
You should not iterate over all the entries in a hashmap (currently, the loop for (Entry<String, String> specificData : mappedData.entrySet()) in your code. This defeats the purpose of maintaining a hashmap.
Instead, the inner loop should become
String potentialNeedle = mappedData.get(needleKey);
if (needle.equals(potentialNeedle))
returnValueSet.addAll(mappedData.values());
You're using the map as if it was a list of entries, which completely defeats the purpose of a map: quickly access to a value for a given key:
for (Map<String, String> mappedData : haystack) {
String value = mappedData.get(needleKey);
if (needle.equals(value)) {
returnValueSet.addAll(mappedData.values());
}
}
Maybe you should take redesign in account to improve performance. it could be smart to have a map for each field you want to search. instead of your haystack create your data-structure like that:
public class PersonDataManager {
private final Map<Integer, Map<String, String>> baseData;
private final Map<String, List<Integer>> firstNameLookupMap;
private final Map<String, List<Integer>> lastNameLookupMap;
public PersonDataManager(){
this.baseData = new HashMap<>();
this.firstNameLookupMap = new HashMap<>();
this.lastNameLookupMap = new HashMap<>();
}
public void addPerson(Integer id, String firstName, String lastName){
//try to find existing person to update:
Map<String, String> personMap = baseData.get(id);
if(personMap == null){
personMap = new HashMap<>();
baseData.put(id, personMap);
}
personMap.put("firstName", firstName);
personMap.put("lastName", lastName);
//add to lookup-maps
addLookupName(firstNameLookupMap, id, firstName);
addLookupName(lastNameLookupMap, id, lastName);
}
private static void addLookupName(Map<String, List<Integer>> nameMap, Integer id, String name){
//get existing list of the name:
List<Integer> idList = nameMap.get(name);
if(idList == null){
idList = new ArrayList<>();
}
if(!idList.contains(id)){
idList.add(id);
}
}
private List<Map<String, String>> searchByName(Map<String, List<Integer>> nameMap, String name){
List<Integer> matchingIds = nameMap.get(name);
List<Map<String, String>> result = new ArrayList<>();
if(matchingIds != null){
for(Integer id : matchingIds){
result.add(searchById(id));
}
}
return result;
}
public Map<String, String> searchById(Integer id){
return baseData.get(id);
}
public List<Map<String, String>> searchByFirstName(String name){
return searchByName(firstNameLookupMap, name);
}
public List<Map<String, String>> searchByLastName(String name){
return searchByName(lastNameLookupMap, name);
}
}
This way you can easily get a hashmap of a person using its id. if you need to search by firstname or lastname you can utilize the additional maps to get the matching person-ids. hope this helps
EDIT: Just implemented a class which does exactly what i think you might need. have fun ;)
I have created a map called result.
In the sortByKeys method as my keys are String with Numeric values, I have converted them to Integer key type Map then sorted them.
The sorting is working fine when I am looping and printing individually, but not when I am setting them in another Map.
public class TestDate {
public static void main (String args[]){
Map<String, String> result = new HashMap<String, String>();
result.put("error", "10");
result.put("1","hii");
result.put("Update","herii");
result.put("insert","insert");
result.put("10","hiiuu");
result.put("7","hii");
result.put("21","hii");
result.put("15","hii");
Map<String, String> sorted = sortByKeys(result);
//System.out.println(sorted);
}
private static Map<String, String> sortByKeys(Map<String, String> map) {
Map <Integer,String> unSorted = new HashMap<Integer, String>();
Map <String,String> sorted = new HashMap<String, String>();
for (Map.Entry<String, String> entry : map.entrySet())
{
try{
int foo = Integer.parseInt(entry.getKey());
unSorted.put(foo, entry.getValue());
}catch (Exception e){
}
}
Map<Integer, String> newMap = new TreeMap<Integer, String>(unSorted);
Set set = newMap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry me = (Map.Entry)iterator.next();
System.out.println(me.getKey());
System.out.println(me.getValue());
sorted.put(me.getKey().toString(), me.getValue().toString());
}
System.out.println(sorted);
return null;
}
}
Here is the o/p :
1
hii
7
hii
10
hiiuu
15
hii
21
hii
{21=hii, 10=hiiuu, 1=hii, 7=hii, 15=hii}
If you don't need the last inch of performance, you can solve this rather directly, without an extra step to sort the map, by using SortedMap:
Map<String,String> result = new TreeMap<>(Comparator.comparingInt(Integer::parseInt));
If you are among the unfortunate bunch who are still being denied access to Java 8, you'll have to implement the Comparator in long-hand:
new TreeMap<>(new Comparator<String,String> { public int compare(String a, String b) {
return Integer.compare(Integer.parseInt(a), Integer.parseInt(b));
}});
The above approach works only under the assumption that all keys are parseable integers. If that is not the case, then you won't be able to use the SortedMap directly, but transform your original map into it, filtering out the unparseable keys.
It's because the Map you're putting them into is a HashMap, which isn't sorted. There's no guarantee of the ordering of results you'll get out of the HashMap, even if you put them in in the right order.
(And calling it sorted doesn't change anything :) )
You print 2 different maps and not the same: you iterate over and print the entries of newMap map, and at the end you print sorted map.
You see the sorted entries printed because you iterate over your sorted newMap.
Then you print the sorted map which is unsorted (despite by its name). You print a different map instance.
Print this:
System.out.println(newMap); // This is the instance of the sorted "TreeMap"
I have map of maps
Map<String, Map<String,Integer>> outerMap = new HashMap<String, Map<String, Integer>>();
and I want to put some values to inner map. Is that correct way? Or it can be done better?
class SampleMap {
Map<String, Map<String, Integer>> outerMap = new HashMap<String, Map<String, Integer>>();
public void add(String outerKey, String innerKey, Integer value) {
Map<String, Integer> tempMap = new HashMap<String, Integer>();
if (outerMap.size() > 0)
tempMap = outerMap.get(outerKey);
tempMap.put(innerKey, value);
outerMap.put(key, tempMap);
}
}
You can improve the code by avoiding the creation of a new inner map eagerly, until the point when you know that you must create it.
In addition, if you know that the inner map instance came from the outer map, you don't have to spend time putting it back where it came from.
public void add(String outerKey, String innerKey, Integer value) {
Map<String, Integer> tempMap
if (outerMap.containsKey(outerKey)) {
tempMap = outerMap.get(outerKey);
} else {
tempMap = new HashMap<String, Integer>();
outerMap.put(outerKey, tempMap);
}
tempMap.put(innerKey, value);
}
Technically there is nothing wrong in your code (except a minor improvement suggested by dasblinkenlight), but is map of maps what you really need?
If you want to read/write values by two keys, probably it's better to create map from pair of two keys (MultiKey or Pair implementation can be used) or another data structure (see this comment for details https://stackoverflow.com/a/3093993/554281)