I am currently reading 2 million lines from a textfile as asked in the previous question
Java Fastest way to read through text file with 2 million lines
Now I store these information into HashMap and I want to sort it via TreeMap because I want to use ceilingkey. Is the following method correct?
private HashMap<Integer, String> hMap = new HashMap();
private TreeMap<Integer, String> tMap = new TreeMap<Integer, String>(hMap);
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
treeMap.putAll(hashMap);
Should work anyway.
This would work just fine:
HashMap<Integer, String> hashMap = new HashMap<>();
TreeMap<Integer, String> treeMap = new TreeMap<>(hashMap);
But I wouldn't advise using HashMap to store the input. You end up with two Maps holding the same huge data. Either do it on the fly and add directly into TreeMap or use List to TreeMap conversion.
Also, for even more efficiency consider primitive collections.
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
hashMap.remove(null);
treeMap.putAll(hashMap);
HashMap will allow null but TreeMap not so before adding into Treemap, remove null from keyset
Related
I want to save statistic data from a file in a treemap:
public static TreeMap<String, TreeMap<Integer, String>> playerstats_life = new TreeMap<String, TreeMap<Integer, String>>();
public static TreeMap<String, TreeMap<Integer, String>> playerstats_month = new TreeMap<String, TreeMap<Integer, String>>();
public static TreeMap<String, TreeMap<Integer, String>> playerstats_week = new TreeMap<String, TreeMap<Integer, String>>();
(The key is the statistic type like kills, deaths, wins... . In the value is an other treemap with the kill amount and the playername)
I am not very satisfied with this solution, because I have only a limited amount of ram -> performance tuning.
So my question is: Is this a good way, or should i save it in classes/objects?
Like: StatisticGroup -> StatisticEntry?
I apologize for my knowledge, because I am a learning developer.
Btw. I will not store more than 5000-10000 entries.
Thanks four your help.
This question seems to be very popular and yet I couldn't get correct results for my implementation. I had this thread as an example but so far no luck.
Here I have HashMap that I need to convert to TreeMap in order to have key values sorted:
HasMap<String, HashMap<String, SomeBean>> hashMap = (HashMap<String, HashMap<String, SomeBean>>)request.getAttribute("HASHMAP");
After applying iterator I could see results in unsorted order.
Now I want to convert it to TreeMap:
TreeMap<String, TreeMap<String, SomeBean>> treeMap = new TreeMap<String, TreeMap<String, SomeBean>>(hashMap);
Result:
The constructor TreeMap<String,TreeMap<String,SomeBean>>(HashMap<String,HashMap<String,SomeBean>>) is undefined
Well, it seems because i have nested map with my bean class it is not allowing me to create new tree map. It is understandable as I don't expect TreeMap to have constructor that suits my criteria but the question is how do I find workaround for this problem?
Since your maps have incompatible value types, you'll need to convert them manually:
Map<String, Map<String, SomeBean>> treeMap = new TreeMap<>();
for (Map.Entry<String, HashMap<String, Integer>> e : hashMap.entrySet())
treeMap.put(e.getKey(), new TreeMap<>(e.getValue()));
If your code uses the nested map as a regular Map, i.e. it doesn't use any specific method defined in HashMap or TreeMap, you can define the type parameter of your variables using Map instead:
HashMap<String, Map<String, SomeBean>> hashMap =
(HashMap<String, Map<String, SomeBean>>) request.getAttribute("HASHMAP");
TreeMap<String, Map<String, SomeBean>> treeMap =
new TreeMap<String, Map<String, SomeBean>>(hashMap);
Or else, you won't be able to use constructor(Map) or putAll(Map), and you will have to fill in the treeMap manually.
I have created two hashmaps:
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
Each map takes in values from text files on my PC
I have found a way to find duplicates in both of the maps:
Set<String> s = new HashSet<String>(map1.keySet());
s.retainAll(map2.keySet());
System.out.println("Duplicates" + s);
The overall program will do the following:
Place the duplicates in a separate hashmap
For example:
map1 contains:
1234,LID4321
918,LID6677
945, LID8765
map2 contains:
1234,LID1234
918,LID5242
2322,LID8837
7777,LID9871
The end result would be:
And mapDuplicates will contain the duplicates:
1234,LID4321
918,LID6677
1234,LID1234
918,LID5242
The keys are equal, therefore they are duplicates
I defined a hashmap as follows:
HashMap<String, List<String>> hashmap = new HashMap<String, List<String>>();
And I would like to retrieve the complete list of keys and print them. However, keySet() does not work for maps defined as <String, List<String>>
Set<String, List<String>> keys = hashmap.keySet();
How could I solve this problem?
What you need is not the .keySet(), but rather the .entrySet():
Set<Map.Entry<String, List<String>>> keys = hashmap.entrySet();
More info:
How to efficiently iterate over each entry in a Map?
This question already has answers here:
How can I combine two HashMap objects containing the same types?
(17 answers)
Closed 9 years ago.
I have two HashMaps defined like so:
HashMap<String, List<Incident>> map1 = new HashMap<String, List<Incident>>();
HashMap<String, List<Incident>> map2 = new HashMap<String, List<Incident>>();
Also, I have a 3rd HashMap Object:
HashMap<String, List<Incident>> map3;
and the merge list when combine both.
In short, you can't. map3 doesn't have the correct types to merge map1 and map2 into it.
However if it was also a HashMap<String, List<Incident>>. You could use the putAll method.
map3 = new HashMap<String, List<Incident>>();
map3.putAll(map1);
map3.putAll(map2);
If you wanted to merge the lists inside the HashMap. You could instead do this.
map3 = new HashMap<String, List<Incident>>();
map3.putAll(map1);
for(String key : map2.keySet()) {
List<Incident> list2 = map2.get(key);
List<Incident> list3 = map3.get(key);
if(list3 != null) {
list3.addAll(list2);
} else {
map3.put(key,list2);
}
}
create third map and use putAll() method to add data from ma
HashMap<String, Integer> map1 = new HashMap<String, Integer>();
HashMap<String, Integer> map2 = new HashMap<String, Integer>();
HashMap<String, Integer> map3 = new HashMap<String, Integer>();
map3.putAll(map1);
map3.putAll(map2);
You have different type in question for map3 if that is not by mistake then you need to iterate through both map using EntrySet
Use commons collections:
Map<String, List<Incident>> combined = CollectionUtils.union(map1, map2);
If you want an Integer map, I suppose you could apply the .hashCode method to all values in your Map.
HashMap has a putAll method.
Refer this :
http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html