How to put/get values into/from Nested HashMap - java

I want to create a nested HashMap that will take two keys of type float and give out value of type Integer.
public static HashMap<Float, HashMap<Float, Integer>> hashX = new HashMap<Float,HashMap<Float, Integer>>();
Is there a simple method of putting/getting the values like an ordinary HashMap i.e.
hashX.put(key, value);
hashX.get(key);
or is it a more complicated method that must be used?
I have searched around the web for a solution but am finding it tough to find a solution that applies to me. Any help would be appreciated!

Map<Float, Map<Float, Integer>> map = new HashMap<>();
map.put(.0F, new HashMap(){{put(.0F,0);}});
map.put(.1F, new HashMap(){{put(.1F,1);}});
map.get(.0F).get(.0F);

You have to get() the nested map out of the outer map and call can call put() and get() on it
float x = 1.0F;
HashMap<Float, Integer> innerMap = hashX.get(x);
if (innerMap == null) {
hashX.put(x, innerMap = new HashMap<>()); // Java version >= 1.7
}
innerMap.put(2.0F, 5);

You can create a wrapper class with a method like this:
public class MyWrapper {
private Map<Float, Map<Float, Integer>> hashX;
// ...
public void doublePut(Float one, Float two, Integer value) {
if (hashX.get(one) == null) {
hashX.put(one, new HashMap<Float, Integer>());
}
hashX.get(one).put(two, value);
}
}
Please note that you should use interfaces instead of concrete implementations when you declare your fields. For example it would make easier to refactor HashMap into ConcurrentHashMap if the need arises.

You can do it like this:
HashMap<Float, Integer> h1 = new HashMap<Float, Integer>();
h1.put(1.0f,new Integer(1));
HashMap<Float, Integer> h2 = new HashMap<Float, Integer>();
h2.put(3.0f,new Integer(3));
hashX.put(1.0f, h1);
hashX.put(1.0f, h1);

Try this.
static <K0, K1, V> void put(Map<K0, Map<K1, V>> map, K0 k0, K1 k1, V value) {
map.computeIfAbsent(k0, x -> new HashMap<>()).put(k1, value);
}
static <K0, K1, V> V get(Map<K0, Map<K1, V>> map, K0 k0, K1 k1) {
return Optional.ofNullable(map.get(k0)).map(s -> s.get(k1)).orElse(null);
}
and
Map<Float, Map<Float, Float>> map = new HashMap<>();
put(map, 1F, 2F, 3F);
put(map, 4F, 5F, 6F);
System.out.println(map);
output:
{4.0={5.0=6.0}, 1.0={2.0=3.0}}
Or simply
Map<Float, Map<Float, Float>> map = Map.of(
1F, Map.of(2F, 3F),
4F, Map.of(5F, 6F));

I want to create a nested HashMap that will take two keys of type float and give out value of type Integer.
You don't need a nested Map for that. If you want to lookup using a composite key, it is better to declare your map to be as such. There isn't a good Pair class in JFK, but you ca use Map.Entry, which is somewhat inconvenient to use but works:
Map<Map<Float, Float>, Integer> map = new ....
See https://stackoverflow.com/a/3110563/18573 for creating Map.Entry instances

package com.Collection;
import java.util.*;
public class India {
public static void main(String[] args) {
Set s = new TreeSet();
s.add("Barshi");
s.add("Pandharpur");
s.add("Kurduwadi");
s.add("Vairag");
Map<String,Map<String,Map<String,TreeSet>>> map =
Map.of("India", Map.of("Maharashtra", Map.of("Solapur", new TreeSet(s))));
System.out.println(map);
System.out.println(map.get("India").get("Maharashtra").get("Solapur").contains("Barshi"));
}
}

Related

Finding the Key from Value in Map

So I am having a bit of trouble understanding how to do this. I am building a word counter in Java using Map<String, Integer> where a word is the String and the amount of times that word was said in a .txt document is Integer. This is all working great...but I am trying to develop a part where it will display at the bottom what the top 5 results are ---> what the top 5 map.values() are.
The problem I have run into is after I find the value, I can't get the String with it.
Any suggestions would be very helpful to me.
You need to use the Map.Entry<String, Integer> to get the pair of the key and value.
The values() method returns only values, whereas the keySet() method returns only the keys.
Firstly, you should sort your map, based on values, to get the top five results.
The straightforward approach uses a Comparator. See the answer here for more.
Then you simply get the first five entries of the map.getEntrySet(). It would be easier to use an Iterator for this.
UPDATE:
Set<Entry<String, Integer>> set = wordCount.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
int topResults = 5;
Iterator<Entry<String, Integer>> iter = list.iterator(); //refer the sorted collection
while (iter.hasNext() && topResults > 0 ) {
Map.Entry<String, Integer> entry = iter.next();
System.out.println(entry.getKey() + "->" + entry.getValue());
topResults --;
}
a) Iterate over the map, this way you have both keys and values accessible:
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
// ...
}
b) build a second map for reverse lookup, e.g. Map - note that this is likely not a good idea here as you may have duplicate keys (same number for different words)
c) consider using a bidimap - this is a map which you can query both by key and by value.
I suggest you to override Comparator and build your Map constructor based on it.
Code is showed below:
class ValueComparator implements Comparator {
Map map;
public ValueComparator(Map map) {
this.map = map;
}
public int compare(Object keyA, Object keyB) {
Comparable valueA = (Comparable) map.get(keyA);
Comparable valueB = (Comparable) map.get(keyB);
return valueB.compareTo(valueA);
}
}
public class YourClass{
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 10);
map.put("b", 30);
map.put("c", 50);
map.put("d", 40);
map.put("e", 20);
System.out.println(map);
Map sortedMap = sortByValue(map);
System.out.println(sortedMap);
}
public static Map sortByValue(Map unsortedMap) {
Map sortedMap = new TreeMap(new ValueComparator(unsortedMap));
sortedMap.putAll(unsortedMap);
return sortedMap;
}
}

adding the values of two maps when same key

Looking for a standard library function way in Java for adding the values in two maps based on their keys.
Map A: {a=1, b=2}
Map B: {a=2, c=3}
Resulting map:
Map C: {a=3, b=2, c=3}
I know this can be coded in a few lines. I also know functional programming is great for this. I am just wandering if there is a standard function or syntax people use out there.
Something like (but probably more generic than):
public HashMap<String,Double> addValues(HashMap<String,Double> a, HashMap<String,Double> b) {
HashMap<String,Double> ret = new HashMap<String,Double>(a);
for (String s : b.keySet()) {
if (ret.containsKey(s)) {
ret.put(s, b.get(s) + ret.get(s));
} else {
ret.put(s, b.get(s));
}
}
return ret;
}
An alternative which does essentially the same thing, using Java 8 new getOrDefault method:
Set<String> keys = new HashSet<> (a.keySet());
keys.addAll(b.keySet());
Map<String, Integer> c = new HashMap<>();
for (String k : keys) {
c.put(k, a.getOrDefault(k, 0) + b.getOrDefault(k, 0));
}
But if using Java 8, you may as well stream the keys and make it a one liner:
Map<String, Object> c = Stream.concat(a.keySet().stream(), b.keySet().stream())
.distinct()
.collect(toMap(k -> k, k -> a.getOrDefault(k, 0) + b.getOrDefault(k, 0)));
I think what you are doing is just fine. I can think of one other way though, using a MultiMap. You can add all your elements to a multimap and then run a summation function over all the values for each key at the end.
Here's a version that allows for any number of Maps to be combined:
public static Map<String, Integer> addKeys(Map<String, Integer>... maps) {
Set<String> keys = new HashSet<String>();
for (Map<String, Integer> map : maps)
keys.addAll(map.keySet());
Map<String, Integer> result = new HashMap<String, Integer>();
for (String key : keys) {
Integer value = 0;
for (Map<String, Integer> map : maps)
if (map.containsKey(key))
value += map.get(key);
result.put(key, value);
}
return result;
}
Usage:
public static void main(String[] args){
Map<String, Integer> a = new HashMap<String, Integer>();
a.put("a", 1);
a.put("b", 2);
Map<String, Integer> b = new HashMap<String, Integer>();
b.put("a", 2);
b.put("c", 3);
Map<String, Integer> c = addKeys(a, b);
System.out.println(c);
}
Ouptut:
{b=2, c=3, a=3}
Unfortunately, it's not possible as far as I can see to create a generic method:
public static <K, V extends Number> Map<K, V> addKeys(Class<V> cls, Map<K, V>... maps);
Because the Number class doesn't support the + operator. Which seems a bit daft to me...

How to receive difference of maps in java?

I have two maps:
Map<String, Object> map1;
Map<String, Object> map2;
I need to receive difference between these maps. Does exist may be apache utils how to receive this difference?
For now seems need take entry set of each map and found diff1 = set1 - set2 and diff2 = set2- set1.
After create summary map =diff1 + diff2
It looks very awkwardly. Does exist another way?
Thanks.
How about google guava?:
Maps.difference(map1,map2)
Here is a simple snippet you can use instead of massive Guava library:
public static <K, V> Map<K, V> mapDifference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
Map<K, V> difference = new HashMap<>();
difference.putAll(left);
difference.putAll(right);
difference.entrySet().removeAll(right.entrySet());
return difference;
}
Check out the whole working example
If I understood well you are trying to calculate symmetric difference beetween the two maps entry sets.
Map<String, Object> map1;
Map<String, Object> map2;
Set<Entry<String, Object>> diff12 = new HashSet<Entry<String, Object>>(map1.entrySet());
Set<Entry<String, Object>> diff21 = new HashSet<Entry<String, Object>>(map2.entrySet());
Set<Entry<String, Object>> result;
diff12.removeAll(map2.entrySet());
diff21.removeAll(map1.entrySet());
diff12.addAll(diff21);
Considering the awkward behavior you mentioned, let's take a closer look at the above code behavior. For example if we take the numerical example from the above given link:
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("a", 1);
map1.put("b", 2);
map1.put("c", 3);
map1.put("d", 4);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("a", 1);
map2.put("d", 4);
map2.put("e", 5);
After you calculate the difference as shown, the output:
System.out.println(Arrays.deepToString(diff12.toArray()));
gives:
[e=5, c=3, b=2]
which is the correct result. But, if we do it like this:
public class CustomInteger {
public int val;
public CustomInteger(int val) {
this.val = val;
}
#Override
public String toString() {
return String.valueOf(val);
}
}
map1.put("a", new CustomInteger(1));
map1.put("b", new CustomInteger(2));
map1.put("c", new CustomInteger(3));
map1.put("d", new CustomInteger(4));
map2.put("a", new CustomInteger(1));
map2.put("d", new CustomInteger(4));
map2.put("e", new CustomInteger(5));
the same algorithm gives the following output:
[e=5, a=1, d=4, d=4, b=2, a=1, c=3]
which is not correct (and might be described as awkward :) )
In the first example the map is filled with int values wich are automatically boxed to Integer values.
The class Integer has its own implementation of equals and hashCode methods.
The class CustomInteger does not implement these methods so it inherits them from the omnipresent Object class.
The API doc for the removeAll method from the Set interface says the following:
Removes from this set all of its elements that are contained in the specified collection
(optional operation). If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the two sets.
In order to determine which elements are contained in both collections, the removeAll method uses the equals method of the collection element.
And that's the catch: Integer's equals method returns true if the two numeric values are the same, while Object's equals method will return true only if it is the same object, e.g.
:
Integer a = 1; //autoboxing
Integer b = new Integer(1);
Integer c = 2;
a.equals(b); // true
a.equals(c); // false
CustomInteger d = new CustomInteger(1);
CustomInteger e = new CustomInteger(1);
CustomInteger f = new CustomInteger(2);
d.equals(e); //false
d.equals(f) // false
d.val == e.val //true
d.val == f.val //false
If it's still a bit fuzzy I strongly suggest reading the following tutorials:
Learning the Java language
Collections
Set<Entry<String, Object>> diff = new HashSet<Entry<String, Object>>((map1.entrySet()));
diff.addAll(map2.entrySet());//Union
Set<Entry<String, Object>> tmp = new HashSet<Entry<String, Object>>((map1.entrySet()));
tmp.retainAll(map2.entrySet());//Intersection
diff.removeAll(tmp);//Diff
Building on Vlad's example to work with maps of different sizes
public static <K, V> Map<K, V> mapDiff(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) {
Map<K, V> difference = new HashMap<>();
difference.putAll(left);
difference.putAll(right);
difference.entrySet().removeAll(left.size() <= right.size() ? left.entrySet() : right.entrySet());
return difference;
}
Try using guava's MapDifference.
Simple way to do it. if you want complex way, you can change filter to compare value.
Map<String, Object> map1 = new HashMap<String, Object>() {{
put("A", "1");
put("B", "2");
put("C", "3");
}};
Map<String, Object> map2 = new HashMap<String, Object>() {{
put("A", "1");
put("B", "2");
put("D", "3");
}};
Map<String, Object> newList = map1.keySet().stream().filter(str -> !map2.containsKey(str)).collect(Collectors.toMap(v -> v, v -> map1.get(v)));
Map<String, Object> oldList = map2.keySet().stream().filter(str -> !map1.containsKey(str)).collect(Collectors.toMap(v -> v, v -> map2.get(v)));
System.out.println(newList);
System.out.println(oldList);

Initializing Hashtables in Java?

In C# you can initialize Hashtables (and many other types of objects) using code like this -
Hashtable table = new Hashtable {{1, 1}, {2, 2}};
Is there anything like this in Java or do you have to just declare the Hashtable first and then manually put items in it one by one?
This is answered elsewhere but you can use an anonymous subclass:
new HashMap<Integer, Integer>() {{ put(1, 1); put(2, 2); }};
Lot's of boiler plate, but still a one-liner :). This will, unfortunately, also complain about the missing serialVersionUID constant which you can either add or ignore the warning on.
This is called an instance initializer block, more information here.
In Google Guava, if you want an immutable map you can use:
Map<K,V> m = ImmutableMap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
up to 5 Key/Value pairs.
Beyond that, you can use their ImmutableMap.Builder class:
ImmutableMap<String, Integer> WORD_TO_INT = ImmutableMap.builder()
.put("one", 1)
.put("two", 2)
.put("three", 3)
.build();
Still not nearly as nice as in C#, but the fluent API is a bit helpful.
If you need to initialize a HashMap (HashTable is obsolete) you can use an static initialization block.
Example:
private static Map<String, String> map;
static {
map = new HashMap<String, String>();
map.put("name1", "value1");
map.put("name2", "value2");
....
}
Hope this helped, have Fun!
Another answer (besides the obvious "no -- no native language way to do this"):
Create a Tuple class with a static factory method with a fancy-pants "_" name for brevity:
import java.util.Map;
import java.util.HashMap;
class Tuple<T1,T2> {
private T1 t1;
private T2 t2;
public Tuple(T1 t1, T2 t2) {
this.t1 = t1; this.t2 = t2;
}
public T1 getT1() {return t1;}
public T2 getT2() {return t2;}
static public <X,Y> Tuple<X,Y> _(X t1, Y t2) { return new Tuple<X,Y>(t1,t2); }
static public <X,Y> Map<X,Y> mapFor(Tuple<X,Y>... tuples) {
Map<X,Y> map = new HashMap<X,Y>();
for( Tuple<X,Y> tuple: tuples ) {
map.put(tuple.getT1(), tuple.getT2());
}
return map;
}
public static void main(String[] args) {
Map<String,Integer> map = Tuple.mapFor( _("A", 1), _("B", 2), _("C",3));
}
}
If you want to allow variations on what kind of backing map is produced, you can just pass that in instead:
static public <X,Y> Map<X,Y> mapFor(Map<X,Y> map, Tuple<X,Y>... tuples) {
for( Tuple<X,Y> tuple: tuples ) {
map.put(tuple.getT1(), tuple.getT2());
}
return map;
}
Try this:
Hashtable<Integer, String> ht = new Hashtable<Integer, String>(){
{
put(1,"One");
put(2,"Two");
put(3,"Three");
}
};
There's a thing called double brace initialization, which isn't as nice...
Hashtable table = new Hashtable() {
{
table.put(1, 1);
table.put(2, 2);
}
};
You could even specify using anonymous array notation, and then iterate over it yourself, like this:
Hashtable table = new Hashtable() {
{
for (int[] entry : new int[][] { { 1, 1 }, { 2, 2 } }) {
table.put(entry[0], entry[1]);
}
}
};
Perhaps make a utility function if you're really missing python :)
One of C#'s features which I quite like is its ability to initialize inline like that. Unfortunately, Java doesn't have this feature.
The Java Hashtable does not have any constructors which allow for this either. See a list of its constructors in the Java API documentation:
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Hashtable.html
A safe approach is to use ImmutableMap.of from Guava and optionally wrapped with newHashSet if mutability is needed:
Maps.newHashMap(ImmutableMap.of(k1, v1, ...));
Here's a self-written map builder for inline use, e.g. newMap(1, "one", 2, "two", 3, "three").
public static <K, V> Map<K, V> newMap(final K key, final V value, final Object... elements) {
Preconditions.checkNotNull(key);
Preconditions.checkArgument(elements.length % 2 == 0, "Array length can't be " + elements.length);
final HashMap<Object, Object> map = Maps.newHashMap();
map.put(key, value);
for (int i = 0; i < elements.length; i += 2) {
map.put(elements[i], elements[i + 1]);
}
return (Map<K, V>) map;
}
i suggest something like this:
String[] names={"albert","john","michel"};
int[] id={1234,2345,3456};
Hashtable<int,String> persons = new Hashtable<int,String>();
for(int i=0;i<3;i++)
{
persons.put(id[i],names[i]);
}
This is not possible in Java. We all suffer from that.

Java invert map

I need create inverse map - select unique values and for them find keys.
Seems that only way is to iterate all key/value pairs, because entrySet returns set of <key,value> so value not unique?
The values in a map may not be unique. But if they are (in your case) you can do as you wrote in your question and create a generic method to convert it:
private static <V, K> Map<V, K> invert(Map<K, V> map) {
Map<V, K> inv = new HashMap<V, K>();
for (Entry<K, V> entry : map.entrySet())
inv.put(entry.getValue(), entry.getKey());
return inv;
}
Java 8:
public static <V, K> Map<V, K> invert(Map<K, V> map) {
return map.entrySet()
.stream()
.collect(Collectors.toMap(Entry::getValue, Entry::getKey));
}
Example of usage:
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Hello", 0);
map.put("World!", 1);
Map<Integer, String> inv = invert(map);
System.out.println(inv); // outputs something like "{0=Hello, 1=World!}"
}
Side note: the put(.., ..) method will return the the "old" value for a key. If it is not null you may throw a new IllegalArgumentException("Map values must be unique") or something like that.
Take a look at Google Guava BiMap.
Example usage
Map<Integer, String> map = new HashMap<>();
map.put(1, "one");
map.put(2, "two");
Map<String, Integer> inverted = HashBiMap.create(map).inverse();
To get an inverted form of a given map in java 8:
public static <K, V> Map<V, K> inverseMap(Map<K, V> sourceMap) {
return sourceMap.entrySet().stream().collect(
Collectors.toMap(Entry::getValue, Entry::getKey,
(a, b) -> a) //if sourceMap has duplicate values, keep only first
);
}
Example usage
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "one");
map.put(2, "two");
Map<String, Integer> inverted = inverseMap(map);
Seems that only way is to iterate all key/value pairs, because entrySet returns set of so value not unique?
It's one way at least. Here's an example:
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "one");
map.put(2, "two");
Map<String, Integer> inverted = new HashMap<String, Integer>();
for (Integer i : map.keySet())
inverted.put(map.get(i), i);
In case of non-unique values, this algorithm will map the last value found to it's key. (Since the iteration order is undefined for most maps, this should be as good as any solution.)
If you really do want to keep the first value found for each key, you could change it to
if (!inverted.containsKey(map.get(i)))
inverted.put(map.get(i), i);
I would give another approach to this problem giving an extra dimension:
duplicate values in EntrySet.
public static void main(String[] args) {
HashMap<Integer, String> s = new HashMap<Integer, String>();
s.put(1, "Value1");
s.put(2, "Value2");
s.put(3, "Value2");
s.put(4, "Value1");
/*
* swap goes here
*/
HashMap<String,List<Integer>> newMap = new HashMap<String, List<Integer>>();
for (Map.Entry<Integer, String> en : s.entrySet()) {
System.out.println(en.getKey() + " " + en.getValue());
if(newMap.containsKey(en.getValue())){
newMap.get(en.getValue()).add(en.getKey());
} else {
List<Integer> tmpList = new ArrayList<Integer>();
tmpList.add(en.getKey());
newMap.put(en.getValue(), tmpList);
}
}
for(Map.Entry<String, List<Integer>> entry: newMap.entrySet()){
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
T result will be that:
1 Value1 2 Value2 3 Value2 4 Value1 Value1 [1, 4] Value2 [2, 3]
Apache Commons Collections also provides a BidiMap interface for bi-directional maps, along with several implementations.
BidiMap JavaDoc
If your values duplicate and you need to store keys in list you can go with
val invertedMap = originalMap.entrySet().stream()
.collect(Collectors.groupingBy(
Map.Entry::getValue,
Collectors.mapping(Map.Entry::getKey, Collectors.toList()))
);
You have to assume that values may be identical, since the Map contract allows it.
In my opinion the best solution lies in using a wrapper. It will contain the original value, and add an id. Its hashCode() function will rely on the id, and you provide a Getter for the original value.
Code would be something like this:
public class MapKey
{
/**
* A new ID to differentiate equal values
*/
private int _id;
/**
* The original value now used as key
*/
private String _originalValue;
public MapKey(String originalValue)
{
_originalValue = originalValue;
//assuming some method for generating ids...
_id = getNextId();
}
public String getOriginalValue()
{
return _originalValue;
}
#Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + _id;
return result;
}
#Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MapKey other = (MapKey) obj;
if (_id != other._id)
return false;
return true;
}
#Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("MapKey value is ");
sb.append(_originalValue);
sb.append(" with ID number ");
sb.append(_id);
return sb.toString();
}
Inverting the map would be something like this:
public Map <MapKey, Integer> invertMap(Map <Integer, String> map)
{
Map <MapKey, Integer> invertedMap = new HashMap <MapKey, Integer>();
Iterator<Entry<Integer, String>> it = map.entrySet().iterator();
while(it.hasNext())
{
//getting the old values (to be reversed)
Entry<Integer, String> entry = it.next();
Integer oldKey = entry.getKey();
String oldValue = entry.getValue();
//creating the new MapKey
MapKey newMapKey = new MapKey(oldValue);
invertedMap.put(newMapKey, oldKey);
}
return invertedMap;
}
Printing the values something like this:
for(MapKey key : invertedMap.keySet())
{
System.out.println(key.toString() + " has a new value of " + invertedMap.get(key));
}
None of this code is tested, but I believe it's the best solution since it makes use of OO inheritance design instead of "c" style checks and allows you to display all the original keys and values.
With Guava
Multimaps.transformValues(Multimaps.index(map.entrySet(), Map.Entry::getValue),
Map.Entry::getKey)
You'll get a multimap (basically a map of lists) in return.

Categories