Java Data Structures

James Davis
Jan 18, 2017 · 3 min read

I’ve run into a couple data structures I’ve found useful and I thought I would share them.

HashMapList — This is basically a HashMap<Key, List<Value>> that’s useful for caching reference data or categorizing data in some way. Below is an implementation

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
* The Class HashMapList.
*
* @param <K> the key type
* @param <V> the value type
*/
public class HashMapList<K,V> {

/** The map. */
private Map<K,List<V>> map;

/**
* Instantiates a new hash map list.
*/
public HashMapList(){
this.map = new HashMap<>();
}

/**
* Put.
*
* @param k the k
* @param v the v
*/
public void put(K k, V v){
if(map.get(k) == null){
map.put(k, new ArrayList<>());
}
map.get(k).add(v);
}

/**
* Put all.
*
* @param k the k
* @param vList the v list
*/
public void putAll(K k, List<V> vList){
if(map.get(k) == null){
map.put(k, new ArrayList<>());
}
map.get(k).addAll(vList);
}

/**
* Gets the list.
*
* @param k the k
* @return the list
*/
public List<V> getList(K k){
return map.get(k);
}

/**
* Checks if is empty.
*
* @return true, if is empty
*/
public boolean isEmpty(){
return map.size()==0;
}

/**
* Get Entry Set
*
* @return
*/
public Set<Entry<K, List<V>>> entrySet(){
return map.entrySet();
}
}

MapCount — This is a HashMap that keeps a BigInteger count of how many times a Value was added for a specific Key. Below is an implementation.

import java.math.BigInteger;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

/**

*

*

*/

public class MapCount {

private Map<String, BigInteger> map = new HashMap<>();

private Long count = 1L;

/**

*

* @param key

* @param value

*/

public void put(String key, BigInteger value){

if(map.containsKey(key)){

map.put(key, map.get(key).add(value));

}else{

map.put(key, value);

}

count++;

}

/**

*

* @param key

*/

public void put(String key){

BigInteger value = BigInteger.ONE;

if(map.containsKey(key)){

map.put(key, map.get(key).add(value));

}else{

map.put(key, value);

}

count++;

}

/**

*

* @param keyList

*/

public void putAll(List<String> keyList){

BigInteger value = BigInteger.ONE;

for(String key: keyList){

if(map.containsKey(key)){

map.put(key, map.get(key).add(value));

}else{

map.put(key, value);

}

count++;

}

}

/**

*

* @param key

* @return

*/

public BigInteger get(String key){

BigInteger foo = map.get(key);

if(foo == null){

foo = BigInteger.ZERO;

}

return foo;

}

/**

*

* @return

*/

public Set<Entry<String, BigInteger>> entrySet(){

return map.entrySet();

}

/**

*

* @return

*/

public Long getCount(){

return count;

}

}

MapAmount — This is a HashMap that keeps a sum of BigDecimals for each Key.

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
*
*
*
*/
public class MapAmount {
private Map<String, BigDecimal> map = new HashMap<>();
private MapCount mapCount = new MapCount();
/**
*
* @param key
* @param value
*/
public void put(String key, BigDecimal value){
if(map.containsKey(key)){
map.put(key, map.get(key).add(value));
}else{
map.put(key, value);
}
mapCount.put(key, BigInteger.ONE);
}

/**
*
* @param key
* @return
*/
public BigDecimal get(String key){
BigDecimal foo = map.get(key);
if(foo == null){
foo = BigDecimal.ZERO;
}
return foo;
}

/**
*
* @return
*/
public Set<Entry<String, BigDecimal>> entrySet(){
return map.entrySet();
}

public Set<String> keySet(){
return map.keySet();
}

/**
*
* @return
*/
public MapCount getCount(){
return mapCount;
}
}