I'm trying to get results HashMap sorted by value.
This is HashMap's keys and values:
map.put("ertu", 5);
map.put("burak", 4);
map.put("selin", 2);
map.put("can", 1);
I try to get results like this:
1 = can
2 = selin
4 = burak
5 = ertu
Here is my code:
import java.util.*;
public class mapTers {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("ertu", 5);
map.put("burak", 4);
map.put("selin", 2);
map.put("can", 1);
Integer dizi[] = new Integer[map.size()];
Set anahtarlar = map.keySet();
Iterator t = anahtarlar.iterator();
int a = 0;
while (t.hasNext()) {
dizi[a] = map.get(t.next());
a++;
}
Arrays.sort(dizi);
for (int i = 0; i < map.size(); i++) {
while (t.hasNext()) {
if (dizi[i].equals(map.get(t.next()))) {
System.out.println(dizi[i] + " = " + t.next());
}
}
}
}
}
You can sort the entries as follows (but note this won't sort the map itself, and also HashMap cannot be sorted) -
List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
#Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
Every time that you call t.next(), the iterator's pointer is moved forward. Eventually, the iterator reaches the end. You need to reset the iterator. Also, calling t.next() twice moves the pointer twice.
Here's my solution:
import java.util.*;
public class mapTers
{
public static void main(String[] args)
{
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("ertu", 5);
map.put("burak", 4);
map.put("selin", 2);
map.put("can", 1);
Integer dizi[] = new Integer[map.size()];
Set anahtarlar = map.keySet();
Iterator t = anahtarlar.iterator();
int a = 0;
while (t.hasNext())
{
dizi[a] = map.get(t.next());
a++;
}
Arrays.sort(dizi);
for (int i = 0; i < map.size(); i++)
{
t = anahtarlar.iterator();
while (t.hasNext())
{
String temp = (String)t.next();
if (dizi[i].equals(map.get(temp)))
{
System.out.println(dizi[i] + " = " + temp);
}
}
}
}
}
You cannot do that from a Map. At least not directly.
Retrieve the keys/entries, get all the map data in a more suitable structure (hint: a class that encapsulates both attributes and is is stored in a sortable (hint2: SortedSet, List)) and sort.
Do not forget to extend Comparable (and implement compareTo) or, otherwise, create a Comparator.
This is one of the solutions take from: https://stackoverflow.com/a/13913206/1256583
Just pass in the unsorted map, and you'll get the sorted one.
private static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap, final boolean order) {
List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(unsortMap.entrySet());
// Sorting the list based on values
Collections.sort(list, new Comparator<Entry<String, Integer>>() {
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
if (order) {
return o1.getValue().compareTo(o2.getValue());
}
else {
return o2.getValue().compareTo(o1.getValue());
}
}
});
// Maintaining insertion order with the help of LinkedList
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Entry<String, Integer> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
To print, do a simple iteration over the entry set:
public static void printMap(Map<String, Integer> map) {
for (Entry<String, Integer> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " Value : "+ entry.getValue());
}
}
You probably have the wrong data structure for this problem. Either:
Reverse the map so the integers are the keys and the words the values and make the map a SortedMap, or
Use a bidirectional map as provided by libraries like Google Guava.
Reversed Map
private final SortedMap<Integer, String> TRANSLATIONS;
static {
SortedMap<Integer, String> map = new TreeMap<>();
map.put(1, "can");
// ...
TRANSLATIONS = Collections.unmodifiableSortedMap(map);
}
Guava BiMap
private final BiMap TRANSLATIONS =
new ImmutableBiMap.Builder<String, Integer>()
.put("ertu", 5);
.put("burak", 4);
.put("selin", 2);
.put("can", 1);
.build();
Then, iterate over a sorted version of the key set or value set as needed. For example,
TRANSLATIONS.inverse.get(4); // "burak"
I'm just curious. What language are your strings in?
Related
I have a list of users (stored in a properties file) that have a level. I sort the users by their level and then send the sorted list back to the guild. I paginate the list, but its still showing all of the users instead of just 10 per page.
Map<String, Integer> unsortedMap = new HashMap<String, Integer>();
for (String key : prop.stringPropertyNames()) {
String value = prop.getProperty(key);
unsortedMap.put(key, Integer.valueOf(value));
}
Map<String, Integer> sortedMap = sortByValue(unsortedMap);
EmbedBuilder eb = new EmbedBuilder();
eb.setTitle("aaaaaaaaaaaa");
ArrayList<Page> pages = new ArrayList<>();
for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
String key = entry.getKey();
int value = entry.getValue();
if (key.contains(".level")) {
User users = ctx.getJDA().retrieveUserById(key.replace(".level", ""), true).complete();
eb.addField(users.getName(), String.valueOf(value), false);
}
}
for (int i = 0; i < sortedMap.size(); i++){
pages.add(new InteractPage(eb.build()));
}
channel.sendMessageEmbeds((MessageEmbed) pages.get(0).getContent()).queue(success -> {
Pages.paginate(success, pages, true);
});
}
private static Map<String, Integer> sortByValue(Map<String, Integer> unsortedMap) {
List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortedMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
#Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o2.getValue().compareTo(o1.getValue()));
}
});
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
Partition the for loop into smaller parts using the for loop. Initialize the int outside of the loop and then increment the int for every entry and then divide the int by how many items you want displayed on the list. Ex:
int i = 0;
for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
i++;
if (i % 6 == 0) {
//add page here
I have created 2 HashMaps that store string values from two txt files. I am able to successfully print which values are duplicated amongst both Maps, however, I am having trouble determining how many times each values are duplicated.
Here is my code to identify duplicate values:
// find if hashmaps contain duplicate values
boolean val = wordsMap.keySet().containsAll(wordsMap1.keySet());
// create new entry for hashmap
for (Entry<String, Integer> str : wordsMap.entrySet()) {
System.out.println("================= " + str.getKey());
if(wordsMap1.containsKey(str.getKey())){
System.out.println("Map2 Contains Map 1 Key");
}
}
System.out.println("================= " + val);
Any suggestions? Thank you
I am thinking of something along the lines of
Map.values().equals(Map1.values()){
count++;
}
If I understand correctly, you want to find values which are common to two map objects. (Add null and/or other checks as required)
public static int duplicateCount(Map<String, Integer> m1, Map<String, Integer> m2) {
int count = 0;
Collection<Integer> m1Values = m1.values();
Collection<Integer> m2Values = m2.values();
for (Integer i : m1Values) {
if (m2Values.contains(i)) {
count++;
}
}
return count;
}
Here's a sample program have written based on my understanding of your doubt. It finds and displays the duplicate keys and also the duplicate values along with their frequencies of repetition. The logic is: To find the frequency, pass the duplicate element from map1 and the map2 as a parameter to countFrequency() method, which in turn will return the count of repetition.
package com.rahul.stackoverflow;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class DuplicateValHashMap {
public static void main(String[] args) {
Map<String, Integer> map1 = new HashMap<String, Integer>();
Map<String, Integer> map2 = new HashMap<String, Integer>();
map1.put("A", 1);
map1.put("B", 2);
map1.put("C", 3);
map1.put("D", 4);
map1.put("E", 5);
map2.put("A", 1);
map2.put("F", 2);
map2.put("G", 1);
map2.put("H", 3);
map2.put("B", 2);
for(Entry<String, Integer> entrySet : map1.entrySet()){
if(map2.containsKey(entrySet.getKey())){
System.out.println("Map2 contains keys of map1.");
System.out.println("Duplicate keys are : " + entrySet.getKey());
}
if(map2.containsValue(entrySet.getValue())){
System.out.println("Map2 contains values of map1.");
System.out.println("Duplicate values are : " + entrySet.getValue()+
" which is repeated " + countFrequency(map2, entrySet.getValue())+ " times.");
}
}
}
public static int countFrequency(Map<String, Integer> map, Integer value){
int count = 0;
for(Entry<String, Integer> entrySet : map.entrySet()){
if(value == entrySet.getValue()){
count++;
}
}
return count;
}
}
To get the number of duplicate keys between Maps,
public static int getDuplicateKeyCount(Map<?, ?> m1, Map<?, ?> m2) {
Set<?> tempSet = new HashSet<>(m1.keySet());
tempSet.retainAll(new ArrayList<>(m2.keySet()));
return tempSet.size();
}
To get the number of duplicate values between Maps,
public static int getDuplicateValueCount(Map<?, ?> m1, Map<?, ?> m2) {
List<?> tempList = new ArrayList<>(m1.values());
tempList.retainAll(m2.values());
return tempList.size();
}
To get a Map containing the frequency of the values of m1 in m2
public static <K, V> Map<V, Integer> getValueFrequencyMap(Map<K, V> m1, Map<K, V> m2) {
Map<V, Integer> freq = new HashMap<>();
Collection<V> col = m2.values();
for(V val : m1.values()) {
freq.put(val, Collections.frequency(col, val));
}
return freq;
}
I need to reverse the order of what the below method returns. For example if it
returns:
1=ball, 2=save, 3=take 4=till
I want to reverse and return:
1=till, 2=take, 3=save, 4=ball
My method is:
public Map<Integer, String> returnMapOfValues(ArrayList<String> wordsList) {
int getFrequencyValue = 0;
Set<String> uniqueSetWords = new HashSet<String>(wordsList);
for (String temp : uniqueSetWords) {
getFrequencyValue = Collections.frequency(wordsList, temp);
//prints the Collection of words and they frequency, For testing only
//System.out.println(temp + ":" + Collections.frequency(wordsList,
//temp));
map.put(getFrequencyValue, temp);
getFrequencyValue = 0;
}
return map;
}
Please check if the below code I've written is useful for you as a reference:
public void reverseMap()
{
NavigableMap<Integer,String> map = new TreeMap<Integer,String>();
LinkedHashMap<Integer,String> reverseMap = new LinkedHashMap<Integer,String>();
map.put(1,"Apple");
map.put(2,"Ball");
map.put(3,"Cat");
NavigableSet<Integer> keySet = map.navigableKeySet();
Iterator<Integer> iterator = keySet.descendingIterator();
Integer i;
while(iterator.hasNext())
{
i = iterator.next();
reverseMap.put(i,map.get(i));
}
System.out.println(reverseMap);
}
//map to arrange inizialized
LinkedHashMap<String,object> mta;
//transforms into arrayList hashmap keys and values
ArrayList<Object> valuesTMP=new ArrayList<Object>(mta.values());
ArrayList<String> keysTMP;
keysTMP=new ArrayList<String>(mta.keySet());
//reverse
Collections.reverse(valuesTMP);
Collections.reverse(keysTMP);
LinkedHashMap <String,Object> mtarranged=new LinkedHashMap<String, Object>();
int index=0;
for( String key : keysTMP){
mtarranged.put(key,valuesTMP.get(index));
index++;
}
To answer this question, we need to know how you're iterating the Map since Maps don't have entry order. However, you may try using a TreeMap which has the method descendingMap:
public Map<Integer, String> returnMapOfValues(ArrayList<String> wordsList) {
TreeMap<Integer, String> map = new TreeMap<Integer, String>();
int getFrequencyValue = 0;
Set<String> uniqueSetWords = new HashSet<String>(wordsList);
for (String temp : uniqueSetWords) {
getFrequencyValue = Collections.frequency(wordsList, temp);
//prints the Collection of words and they frequency, For testing only
//System.out.println(temp + ":" + Collections.frequency(wordsList,
//temp));
map.put(getFrequencyValue, temp);
getFrequencyValue = 0;
}
return map.descendingMap();
}
Edit: From your comment, your intent is more clear. A TreeMap will help you with your goal because it is a Map ordered by the natural order of the keys.
I've made some changes to the code. Please check if this is OK for you now.
public void reverseMap()
{
NavigableMap<Integer,String> map = new TreeMap<Integer,String>();
LinkedHashMap<Integer,String> reverseMap = new LinkedHashMap<Integer,String>();
map.put(1,"Apple");
map.put(2,"Ball");
map.put(3,"Cat");
NavigableSet<Integer> keySet = map.navigableKeySet();
Iterator<Integer> forwardIterator = keySet.iterator();
Iterator<Integer> reverseIterator = keySet.descendingIterator();
Integer i;
Integer j;
while(forwardIterator.hasNext())
{
i = forwardIterator.next();
j = reverseIterator.next();
reverseMap.put(i,map.get(j));
}
System.out.println(reverseMap);
}
Please find the below code and Let me know Which is suitable for you ...
int iterationValue = 0;
List<String> stringList = new LinkedList<String>();
stringList.add("ball");
stringList.add("save");
stringList.add("tick");
Map<Integer, String> map = new HashMap<Integer, String>();
for (String temp : stringList) {
map.put(iterationValue++, temp);
}
// Input MAP
System.out.println(map); // {0=ball, 1=save, 2=tick}
Collections.reverse(stringList);
iterationValue = 0;
for (String temp : stringList) {
map.put(iterationValue++, temp);
}
// Output MAP
System.out.println(map); // {0=tick, 1=save, 2=ball}
I have finally gotten my code to where it works. Although it isnt the easiest to read.
I am reading from a text file that has
Date/time Tip from xxx
tip totalAmount
My code now takes the amount tipped by a person and adds them together. eg X tip 10, X tip 20, X tip 30, Y tip 200, Z tip 30, Z tip 40 and outputs
X=60
Y=200
Z=70
I did this by turning my Map< String,Integer> into an Object[] tipsPerPerson
So how would I go about sorting this Object[] tipsPerPerson into something a bit easier to read (theres over 2000 names) a bit like this
Y=200
Z=70
X=60
Here is a portion of the code that im stuck at
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
public class Tip {
public static void main(String[] args)
{
int lineNumber = 1;
Map<String, Integer> tipsByName = new HashMap<String, Integer>();
String fileName = "C:\\Users\\David\\Desktop\\tips.txt";
System.out.println("Reading text from file");
try {
FileReader inputFile = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(inputFile);
String line;
String currentTipper = null;
while ((line = bufferedReader.readLine()) != null) {
if (lineNumber % 2 != 0) {
final String tipperName = line.substring(line.indexOf("from ") + 5);
currentTipper = tipperName;
} else {
final Integer tipValue = Integer.parseInt(line.substring(0, line.indexOf("\t")));
// here we store the tip in the map. If we have a record we sum, else
// we store as is
tipsByName.put(currentTipper, (tipsByName.get(currentTipper) == null ? 0 : tipsByName.get(currentTipper))
+ tipValue);
}
lineNumber++;
}
bufferedReader.close();
Object[] tipsName = tipsByName.entrySet().toArray();
for (int i = 0; i < tipsByName.size(); i++) {
System.out.println(tipsName[i]); // output the map
}
} catch (Exception e) {
System.out.println("Error while reading file line by line: " + e.getMessage());
}
}
}
Just dump the entries into a list and sort them using a Comparator:
List<Map.Entry<String, Integer>> entries = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
return Integer.compare(b.getValue(), a.getValue());
}
});
Note that b is compared to a, rather than the other way around, to give us reverse order (largest to smallest).
All done in two lines. If you then iterate over the list, it will be in the order you wanted. Use getKey() and getValue() to print, or simply use the default toString() of the Entry:
for (Map.Entry<String, Integer> entry : entries)
System.out.println(entry + '\n');
An advice: use the map and d'ont make any array convertions.
The map class gives you all that you need.
You can iterate over the keys or over the values or over both like you do right now.
Just use the key values of your map and pass it as an argument to
Collections.sort(). This works because the returned key set is of type Collection
which can be passed to the Collections.sort() method.
Than it will be ordered in alphabetical order.
If you want something else just pass an additonal Comparator class to the Collection.sort()
or wrap your object within a customized class which implements the
interface Comparable.
After that you just iterate throught you keys and
invoke map.get(key) method.
Example:
Iterator<String> sortedKeys = map.keySet().iteator();
for (String key :keys) {
sysout(key: "+key+" value : "+ map.get(key));
}
Hope it was thge answer you needed.
Ouch. Don't do that! Try this instead...
// Get the Entries.
java.util.Set<Entry<String, Integer>> entrySet = tipsByName
.entrySet();
// Make a SortedSet with a Custom Comparator.
SortedSet<Entry<String, Integer>> sortedSet = new TreeSet<Entry<String, Integer>>(
new Comparator<Entry<String, Integer>>() {
public int compare(
Entry<String, Integer> o1,
Entry<String, Integer> o2) {
if (o1 == o2) {
return 0;
} else if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
}
return o1.getValue().compareTo(
o2.getValue());
}
});
// Add the Entries to the SortedSet.
for (Entry<String, Integer> entry : entrySet) {
sortedSet.add(entry);
}
// Iterate the sortedSet.
Iterator<Entry<String, Integer>> iter = sortedSet
.iterator();
while (iter.hasNext()) {
System.out.println(iter.next()); // print your sorted items.
}
I have a List of HashMap's which has key of type Integer and value of type Long.
List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>();
for (int i = 0; i < 10; i++) {
HashMap<Integer, Long> mMap = new HashMap<Integer, Long>();
mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i));
ListofHash.add(mMap);
}
Now, how do I retrieve the key and value from the list of HashMap?
If using Collection class is the solution, please enlight me.
Update 1:
Actually i am getting the value from the database and putting that into a HashMap
public static List<Map<Integer, Long>> populateMyHashMapFromDB()
{
List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>();
for (int i = 0; i < 10; i++) {
HashMap<Integer, Long> mMap = new HashMap<Integer, Long>();
mMap.put(Integer.valueOf(getIntFromDB(i)), Long.valueOf(getLongFromDB(i)));
ListofHash.add(mMap);
}
return ListofHash;
}
where getIntFromDB and getLongFromDB can retreive any int and long values respectively.
Now this is where i want to get my values that i got from DB both keys and values
public void getmyDataBaseValues()
{
List<HashMap<Integer,Long>> ListofHash=populateMyHashMapFromDB();
// This is where i got confused
}
ANSWER This is why Peter Lawrey suggested
public class HashMaps {
public static void main(String[] args) {
// TODO Auto-generated method stub
testPrintHashmapValues(putToHashMap());
testPrintHashmapValuesWithList(putToHashMapWithList());
}
public static Map<Integer, Long> putToHashMap() {
Map<Integer, Long> map = new LinkedHashMap<Integer, Long>();
for (int i = 0; i < 10; i++) {
map.put(Integer.valueOf(i), Long.valueOf(200000000000L + i));
}
return map;
}
public static void testPrintHashmapValues(Map<Integer, Long> map) {
for (Map.Entry<Integer, Long> entry : map.entrySet()) {
System.out.println("key: " + entry.getKey() + " value: "
+ entry.getValue());
}
}
public static List<Map<Integer, Long>> putToHashMapWithList() {
List<Map<Integer, Long>> listOfHash = new ArrayList<Map<Integer, Long>>();
for (int i = 0; i < 10; i++) {
Map<Integer, Long> mMap = new HashMap<Integer, Long>();
mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L + i));
listOfHash.add(mMap);
}
return listOfHash;
}
public static void testPrintHashmapValuesWithList(
List<Map<Integer, Long>> listOfHash) {
for (int i = 0; i < 10; i++) {
Map<Integer, Long> map = listOfHash.get(i);
for (Map.Entry<Integer, Long> entry : map.entrySet()) {
System.out.println(i + " hashMap");
System.out.println("key: " + entry.getKey() + " value: "
+ entry.getValue());
}
}
}
}
My Task is done even without creating a List.
Its still not clear to me why you want a list.
public static Map<Integer, Long> populateMyHashMapFromDB() {
Map<Integer, Long> map = new LinkedHashMap<Integer, Long>();
for (int i = 0; i < 10; i++)
map.put(getIntFromDB(i), getLongFromDB(i));
return map;
}
Map<Integer, Long> map = populateMyHashMapFromDB();
Long value = map.get(key);
This collection isn't designed to give you the key/values pairs easily. I fyou need to this functionality I would suggest using a different structure.
Assuming you have a some bad code you cannot change, you can do
List<Map<Integer, Long>> maps = new ArrayList<Map<Integer, Long>>();
Map<Integer, Long> all = new HashMap<Integer, Long>();
for(Map<Integer, Long> map: maps)
all.putAll(map);
for(Map.Entry<Integer, Long> entry: all.entrySet() {
// do something which each key/value.
}
In this example you don't need a List or a Map.
long[] longs = new long[10];
for (int i = 0; i < 10; i++)
longs[i] = i;
int key = 1;
int num = longs[key];
You iterate over the list to get the maps, and then iterate over their key set:
public static void main(String[] args) throws NoSuchAlgorithmException {
List<Map<Integer, Long>> ListofHash = new ArrayList<Map<Integer,Long>>();
for (int i = 0; i < 10; i++) {
Map<Integer, Long> mMap = new HashMap<Integer, Long>();
mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L + i));
ListofHash.add(mMap);
}
for (Map<Integer, Long> map : ListofHash) {
for (Integer key : map.keySet()) {
System.out.println(map.get(key));
}
}
}
Note: I've also changed a little your code, using Map instead of HashMap when possible
Something of this effect:
public static Long getValue(Integer key)) {
for (HashMap<Integer, Long> entry : ListofHash) {
if (entry.containsKey(key)) {
return entry.get(key);
}
}
return null;
}
//Retrieve all key/value
for (HashMap<Integer, Long> entry : ListofHash) {
for (Integer key : entry.keySet()) {
System.out.println("Key : " + key + ", value: " + entry.get(key));
}
}
PS Untested.
I am assuming you are asking how to get it from ArrayList,
ListofHash.get(0).get(Object key);
Object key means whatever Integer key you want
Good Luck!
List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>();
for (int i = 0; i < 10; i++) {
HashMap<Integer, Long> mMap = new HashMap<Integer, Long>();
mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i));
ListofHash.add(mMap);
}
You have a total of 10 Maps with only one element stored. And those Maps are stored in a List. You can retrieve those Maps and its elements doing this:
public void testPrintHashmapValues() {
List<HashMap<Integer, Long>> listOfHash = new ArrayList<HashMap<Integer, Long>>();
for (int i = 0; i < 10; i++) {
HashMap<Integer, Long> mMap = new HashMap<Integer, Long>();
mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i));
listOfHash.add(mMap);
}
for (int i = 0; i < 10; i++) {
Map<Integer, Long> map = listOfHash.get(i);
for (Map.Entry<Integer, Long> entry : map.entrySet()) {
System.out.println(i + " hashMap");
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}
}
}
Also the variables should not start with uppercase (ListOfHash). Even though java won't complain it is a bad practice and difficult to read code like that because one might think it's a class name instead of a variable.