(→Usage) |
No edit summary |
||
(12 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
{| class="wikitable" | {| class="wikitable" | ||
!Date | |||
!Version | !Version | ||
!File | !File | ||
!Description | !Description | ||
|- | |- | ||
|22. June 2013 | |||
|1.01 | |||
|http://files.subsecret.dk/ColtHashMap101.jar | |||
|Benchmark code updated | |||
|- | |||
|16. June 2013 | |||
|1.00 | |1.00 | ||
|http://files.subsecret.dk/ColtHashMap100.jar | |http://files.subsecret.dk/ColtHashMap100.jar | ||
Line 54: | Line 61: | ||
|} | |} | ||
Existing code can easily be changed to use new HashMaps's. | Existing code can easily be changed to use new HashMaps's. By changing to Colt's HashMap there will be a memory saving because of more efficient storage of keys and possibly values. However the penalty for autoboxing is still present unless using put(...) and get(...) on the native type. | ||
//From | //From | ||
Map<Integer, String> map = new HashMap<Integer,String>(); | Map<Integer, String> map = new HashMap<Integer,String>(); | ||
Line 66: | Line 73: | ||
//For maximum performance, its necessary to use the original type when putting and retrieving from the map. | //For maximum performance, its necessary to use the original type when putting and retrieving from the map. | ||
native.put(3,"test"); | native.put(3,"test"); | ||
//Caveat: When using ColtIntHashMap<Integer> the compiler cannot infer which of the two put methods you want, and you need to use | //Caveat: When using ColtIntHashMap<Integer> the compiler cannot infer which of the two put methods you want, and you need to use putNative(...) method | ||
native.putNative(3,"test"); | native.putNative(3,"test"); | ||
Line 82: | Line 89: | ||
!Map | !Map | ||
!Memory usage (mb) | !Memory usage (mb) | ||
!Insertion | !Insertion | ||
!get(...) (sec.) | |||
|- | |- | ||
|Java HashMap | |Java HashMap | ||
| | |2485 | ||
| | |8 | ||
|8 | |||
|- | |- | ||
|Colt HashMap | |Colt HashMap | ||
| | |1711 | ||
|7 | |7 | ||
|4 | |||
|- | |- | ||
|Colt (Native) HashMap | |Colt (Native) HashMap | ||
| | |1706 | ||
|6.1 | |6.1 | ||
|2.6 | |||
|- | |- | ||
|Colt (IntInt) HashMap | |Colt (IntInt) HashMap | ||
|1304 | |1304 | ||
|4.7 | |4.7 | ||
|1.9 | |||
|- | |- | ||
|Colt (Quick IntInt) HashMap | |Colt (Quick IntInt) HashMap | ||
|1304 | |1304 | ||
|4.8 | |4.8 | ||
|1.9 | |||
|- | |- | ||
|} | |} | ||
Latest revision as of 14:22, 28 April 2018
Extension of Colt HashMap using Generics for storage of Objects
This is based on part of the code from Colt Project: http://acs.lbl.gov/software/colt/ The Colt Project is made before generics was introduced in Java. This means that the HashMap from Colt Project cannot be used as a drop-in replacement for Java's HashMap.
This version contains only the HashMap part of Colt Project (version 1.2.0) and adds the support for generics, so that it can now be used as a drop-in replacement for Java's HashHamp
Download
JAR also contains source code
Date | Version | File | Description |
---|---|---|---|
22. June 2013 | 1.01 | http://files.subsecret.dk/ColtHashMap101.jar | Benchmark code updated |
16. June 2013 | 1.00 | http://files.subsecret.dk/ColtHashMap100.jar | Initial release |
Changes to original code
- Removed all non-hashmap relevant code
- Fixed bug where OpenLongObjectHashMap.indexOfValue(...) and OpenIntObjectHashMap.indexOfValue(...) used "==" rather than equals for Object types.
- Created new interfaces and classes for wrapping Colt Project HashMap in Java's Map<K,V> interface.
Usage
Class | Key type | Value type |
---|---|---|
ColtIntHashMap | Int | Object |
ColtIntIntHashMap | Int | Int |
ColtQuickIntIntHashMap | Int | Int |
ColtIntDoubleHashMap | Int | Double |
ColtLongHashMap | Long | Object |
ColtDoubleIntHashMap | Double | Int |
Existing code can easily be changed to use new HashMaps's. By changing to Colt's HashMap there will be a memory saving because of more efficient storage of keys and possibly values. However the penalty for autoboxing is still present unless using put(...) and get(...) on the native type.
//From Map<Integer, String> map = new HashMap<Integer,String>(); map.put(3,"test"); //To ColtIntHashMap<String> native = new ColtIntHashMap<String>(); Map<Integer, String> map = native; map.put(3,"test"); //For maximum performance, its necessary to use the original type when putting and retrieving from the map. native.put(3,"test"); //Caveat: When using ColtIntHashMap<Integer> the compiler cannot infer which of the two put methods you want, and you need to use putNative(...) method native.putNative(3,"test");
Benchmark
JVM options: -Xms8g -Xmx8g Insertions: 20 million (7 million duplicates) get(...) 13 million Contains key: 20 million Removals: 6 million containsValue(): 100
JVM 1.6
Map | Memory usage (mb) | Insertion | get(...) (sec.) |
---|---|---|---|
Java HashMap | 2485 | 8 | 8 |
Colt HashMap | 1711 | 7 | 4 |
Colt (Native) HashMap | 1706 | 6.1 | 2.6 |
Colt (IntInt) HashMap | 1304 | 4.7 | 1.9 |
Colt (Quick IntInt) HashMap | 1304 | 4.8 | 1.9 |