How to efficiently iterate a Multimap? - java

I have the following multimap:
Multimap<String,Multimap<String,Integer>> map = ArrayListMultimap.create();
I need to iterate it, but I am stuck.
How should I iterate it?

It looks like you are using google guava multimap and the create method of the class ArrayListMultimap to create it.
You can iterate through your multimap and your inner multimap like this:
package com.test.java;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class Main {
#SuppressWarnings("unchecked")
public static void main(String[] args) {
Multimap<String, Multimap<String,Integer>> map = ArrayListMultimap.create();
Multimap<String,Integer> m = ArrayListMultimap.create();
//put some values
m.put("value1", 1);
m.put("value2", 2);
m.put("value3", 3);
map.put("one", m);
for (Object value1 : map.values()) {
for (Object value2 : ((Multimap<String,Integer>)value1).values()) {
System.out.println((Integer)value2);
}
}
}
}
Output:
2
1
3
Note:
If you want to have a deterministic iteration order, use LinkedListMultimap:
Example:
LinkedListMultimap<String, LinkedListMultimap<String,Integer>> map = LinkedListMultimap.create();
LinkedListMultimap<String,Integer> m = LinkedListMultimap.create();
//put some values
m.put("value1", 1);
m.put("value2", 2);
m.put("value3", 3);
map.put("one", m);
for (Object value1 : map.values()) {
for (Object value2 : ((LinkedListMultimap<String,Integer>)value1).values()) {
System.out.println((Integer)value2);
}
}
Output:
1
2
3

Map<String, Multimap<String>> map = multimap.asMap();
System.out.println("Multimap as a map");
for (Map.Entry<String, Multimap<String>> entry : map.entrySet()) {
String key = entry.getKey();
Multimap<String> value = multimap.get("lower");
System.out.println(key + ":" + value);
}
please try it out.

my example
ListMultimap<Long, Long> items = elasticResult.getResult();
for (Long key : items.keySet()) {
items.get(key).forEach(value -> {
// ... use key and value
});
}

Related

How to get specific values from an ArrayList of HashMaps

I have created an ArrayList of HashMaps and I know how to get all keys and values of all HashMaps in the list, but then I decided to make it complicated and iterate through the ArrayList and get only specific HashMap values(based on keys). I have no idea how to do that.
How can I modify printArrayList method to get only idand sku values from all hashmaps?
Right now I have the following example:
public class HashmapArraylist {
public static void main(String[] args) throws Exception {
Map<String, Object> map1 = new HashMap<>();
map1.put("id", 1);
map1.put("sku", "test1");
map1.put("quantity", 1);
Map<String, Object> map2 = new HashMap<>();
map2.put("id", 2);
map2.put("sku", "test2");
map2.put("quantity", 2);
Map<String, Object> map3 = new HashMap<>();
map3.put("id", 3);
map3.put("sku", "test3");
map3.put("quantity", 3);
ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
arrayList.add(map1);
arrayList.add(map2);
arrayList.add(map3);
printArrayList(arrayList);
}
public static void printArrayList(ArrayList<Map<String, Object>> arrayList) {
for (Map<String, Object> entry : arrayList) {
for (String key : entry.keySet()) {
String value = entry.get(key).toString();
System.out.println(key + " : " + value);
}
System.out.println("-----------");
}
}
}
Your iterator for the arrayList is correct. To retrieve a value from a map, simply provide the key into the 'get' function of the entry. Since your map has a "String" key to an "Object" value, you can use "toString()" on it to get the string from the Object returned from your key.
public static void printArrayList(ArrayList<Map<String, Object>> arrayList) {
for (Map<String, Object> entry : arrayList) {
String myID = entry.get("id").toString();
String mySKU = entry.get("sku").toString();
System.out.print("id:" + myID + " sku: " + mySKU);
System.out.println("-------------------");
}
}
user681574 seems to have already answered your problem, but I will just add one Java8 example code to do the same thing as you need, using streams
public static void printArrayList(ArrayList<Map<String, Object>> arrayList) {
arrayList.stream() //stream out of arraylist
.forEach(map -> map.entrySet().stream() //iterate through each map in the list, create stream out of maps' entryset
.filter(entry -> entry.getKey().equals("id") || entry.getKey().equals("sku")) //filter out only entries that we need (where key is "id" or "sku")
.forEach(idOrSku -> System.out.println(idOrSku.getKey() + ":" + idOrSku.getValue()))); //Iterate through the id/sku entries and print them out just as we want to
}

How to convert a HashMap to a JavaPairRDD, in Spark?

I am very new to Apache Spark. I am trying to create a JavaPairRdd from HashMap. I have a HashMap of type <String,<Integer,Integer>>
How can I convert it into a JavaPairRdd? I have pasted my code below:
HashMap<String, HashMap<Integer,String>> canlist =
new HashMap<String, HashMap<Integer,String>>();
for (String key : entityKey)
{
HashMap<Integer, String> clkey = new HashMap<Integer, String>();
int f=0;
for (String val :mentionKey)
{
//do something
simiscore = (longerLength - costs[m.length()]) / (double) longerLength;
if (simiscore > 0.6) {
clkey.put(v1,val);
System.out.print(
" The mention " + val + " added to link entity " + key);
}
f++;
System.out.println("Scan Completed");
}
canlist.put(key,clkey);
JavaPairRDD<String, HashMap<Integer, String>> rad;
rad = context.parallelize(scala.collection.Seq(toScalaMap(canlist)));
}
public static <String,Object> Map<String,Object> toScalaMap(HashMap<String,Object> m) {
return (Map<String,Object>) JavaConverters.mapAsScalaMapConverter(m).asScala().toMap(
Predef.<Tuple2<String,Object>>conforms()
);}
}
If you convert the HashMap into a List<scala.Tuple2<Integer, String>>, then you can use JavaSparkContext.parallelizePairs.
Here is another way to convert java HashMap<String, HashMap<Integer,String>> to List<Tuple2<String, HashMap<Integer,String>>> and pass to parallelizePairs() method of JavaSparkContext.
import scala.Tuple2;
List<Tuple2<String, HashMap<Integer,String>>> list = new ArrayList<Tuple2<String, HashMap<Integer,String>>>();
for(Map.Entry<String, HashMap<Integer,String>> entry : canlist.entrySet()){
list1.add(new Tuple2<String, HashMap<Integer,String>>(entry.getKey(),entry.getValue()));
}
JavaPairRDD<String, HashMap<Integer, String>> javaPairRdd = jsc.parallelizePairs(list);
Code snippet of the generic method for conversion. Utilize JavaSparkContext.parallelizePairs() with the result of this method.
//fromMapToListTuple2() generic method to convert Map<T1, T2> to List<Tuple2<T1, T2>>
public static <T1, T2> List<Tuple2<T1, T2>> fromMapToListTuple2(Map<T1, T2> map)
{
//list of tuples
List<Tuple2<T1, T2>> list = new ArrayList<Tuple2<T1, T2>>();
//loop through all key-value pairs add them to the list
for(T1 key : map.keySet())
{
//get the value
T2 value = map.get(key);
//Tuple2 is not like a traditional Java collection, but a single k-v pair;
Tuple2<T1, T2> tuple2 = new Tuple2<T1, T2>(key, value);
//populate the list with created tupple2
list.add(tuple2);
} // for
return list;
} // fromMapToListTuple2

Java HashMap put in an enhanced for loop just like an ArrayList

For example, I can loop an ArrayList like this
for (String temp : arraylist)
Can I loop a HashMap by using the similar method?
You can iterate over the keys, entries or values.
for (String key : map.keySet()) {
String value = map.get(key);
}
for (String value : map.values()) {
}
for (Map.Entry<String,String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
}
This is assuming your map has String keys and String values.
You can't directly loop a Map like that in Java.
You can, however, loop the keys:
for (SomeKeyObject key : map.keySet())
The values:
for (SomeValueObject value : map.values())
Or even its entries:
for (Map.Entry<SomeKeyObject, SomeValueObject> entry : map.entrySet())
you can do
for (String key : hashmap.keySet()) {
String value = hashmap.get(key);
}
You can do it by using an Iterator http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html.
HashMap<String, String> yourHashMap = new HashMap<>();
Iterator<Map.Entry<String, String>> it = yourHashMap.entrySet().iterator();
while(it.hasNext()){
it.next();
System.out.println(yourHashMap.get(it));
}
At first sight it might be tempting to use a for-loop instead of an Iterator, but
you will need an Iterator if you want to modify the elements in your HashMap while iterating over them!
When using a for-loop you cannot remove elements from your map while
it.remove()
would work well in the above example.
Use this
map.forEach((k, v) -> System.out.printf("%s %s%n", k, v));
public class IterableHashMap<T,U> extends HashMap implements Iterable
{
#Override
public Iterator iterator() {
// TODO Auto-generated method stub
return this.keySet().iterator();
}
}
public static void main(String[] args) {
testA a=new testA();
IterableHashMap test=a.new IterableHashMap<Object,Object>();
for(Object o:test)
{
}
}

printing a hashmap of a key with multiple values

class hello {
string name;
int number;
}
class object {
public static void main(string args[]) {
HashMap hs = new HashMap();
hello c1 = new hello();
hello c2 = new hello();
hs.put("india",c1);
hs.put("america",c2);
}
}
how to print he key value pairs
key with multiple values how is it printed
Iterate Map or HashMap like this.
Map<String, Hello> map=new HashMap<>();
Set<Entry<String, Hello>> entries=map.entrySet();
for (Entry<String, Hello> entry : entries) {
String key=entry.getKey();
Hello hello=entry.getValue();
}
With Java 8:
map.forEach((key, value) -> System.out.println(key + ", " + value));
You need to iterate over hash map keys, then print the key with its value.
Example:
HashMap<String, hello> map = new HashMap<String, hello>();
for (Iterator<String> iterator = map.keySet().iterator(); iterator.hasNext();) {
String key = iterator.next();
System.out.println(key + map.get(keu).toString); suppose you already override the toString method in your hello class
}
You can iterate using Entry Set too.
HashMap<String,hello> hs=new HashMap<String, hello>();
// Put values for hashMap.
for(Map.Entry<String, hello> printPairs: hs.entrySet()) {
System.out.print(printPairs.getKey()+" ---- ");
System.out.println(printPairs.getValue());
}

Grouping similar items using hashmap

I have a input like this
0 [0.327097, 0.326998, 0.0]
0 [1.056364, 0.601873, 0.0]
0 [1.273154, 1.656441, 0.0]
1 [1.48469, 0.095074, 0.0]
1 [1.061504, -0.768175, 1.0]
i need to sort them as
0 : [ [0.327097, 0.326998, 0.0] ,[1.056364, 0.601873, 0.0], [1.273154, 1.656441, 0.0]]
1 : [ [1.48469, 0.095074, 0.0], [1.061504, -0.768175, 1.0]]
I did like this ..
but i am not getting the same output.my output is getting repeated.
Can u pls help me out...
Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>();
String[] subparts = finalline.split("\\[");
String groupKey;
String value;
if (subparts.length == 1) {
groupKey = null;
value = subparts[0];
}
else if (subparts.length == 2) {
groupKey = subparts[0];
value = subparts[1];
}
else {
throw new IllegalArgumentException("Can not parse string");
}
Collection<String> groupContents = groupMap.get(groupKey);
if (groupContents == null) {
groupMap.put(groupKey, groupContents = new ArrayList<String>());
}
groupContents.add(value);
}
The value of the groupMap map is another collection, so you can iterate through that collection within the outer loop as given below
Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>();
for(String key : groupMap.keySet()){
System.out.println("Key: " + key);
Collection<String> values = groupMap.get(key);
for(String value : values){
System.out.println("value: " + value);
}
}
Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>();
for (String s : groupMap.keySet()) {
for (String s1 : groupMap.get(s)) {
System.out.println(s1);
}
}
A collection within a collection just means nested loops--just like with a 2D array.
I'd suggest using a HashMultimap from "Guava" instead.
It helps make it easy to handle a mapping from keys to multiple values and is a general way to associate keys with arbitrarily many values.
Here's an example.
Multimap<String, String> map = HashMultimap.create();
map.put("1", "a");
map.put("1", "b");
map.put("2", "c");
map.put("2", "d");
And now you can use the "values()" view to iterate over the values in the map.
for(String value : map.values()) {
System.out.println(value);
}
This will give you
a
b
c
d
Or if you want the key along with the value, then you can use the "entries()" view.
for(Map.Entry<String, String> entry : map.entries()) {
System.out.println("Key: " + entry.getKey() + " Value : " + entry.getValue());
}
This will give you
Key: 1 Value : a
Key: 1 Value : b
Key: 2 Value : c
Key: 2 Value : d
And if you're looking for a plain old java simple solution
Map<String, List<String>> map = new HashMap<String, List<String>>();
// ... Some code to put values in the map
for(String key : map.keySet()){
System.out.println("\nKey: " + key);
List<String> values = map.get(key);
for(String value : values) {
System.out.println("Value: " + value);
}
}
The best and most efficient way to iterate over a map's entries is:
Map<String, Collection<String>> map;
for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
System.out.print(entry.getKey()+":");
for (String str : entry.getValue())
System.out.println(str);
}
This code will produce the output you requested.
Note that at no point are the keys looked up. When you iterate over the entry set, you have direct access to both the (typed) key and (typed) value.
The most efficient way to loop over the entries in your Map is as follows:
Map<String, Collection<String>> groupMap = new HashMap<String, Collection<String>>();
for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
System.out.println("Key: "+entry.getKey());
for (String val : values) {
System.out.printlnln("Value: "+entry.getValue());
}
}

Categories