Please stop using HashSet, HashMap, and Hashtable

maybe people have been following my work and don't need to be reminded, but i think it's important for me to make it clear what's going on and how other coders can help.
forge uses too much heap. i've reduced a fair portion of that in my efforts to purge use of forge.card.cardFactory.getAllCards and .getCards. i am now working on another heap waster: hashes. trees are more heap efficient and still have spiffy performance.
i am going through the code now, converting variable, field, parameter, and return types from Hashtable, HashMap, and HashSet to Map, Map, and Set, respectively. i am also converting instances to use TreeMap, TreeMap, and TreeSet implementations where feasible.
feasible? yes. there is one important subtle fact about trees. the keys of a TreeMap and the items in a TreeSet must have a "natural ordering". this is java's fancy way of saying that these things need to implement Comparable<T>. if your key (or set's item) type is forge.Card, String, an enum type, or any of Java's primitive or numeric classes like Integer, you're fine. if not, you need to make sure the key (or set's item) type implements Comparable correctly. you can write your own. for an example of a homemade Comparable, see forge.Card.compareTo. if you aren't confident enough for that, use a generic Map or Tree-typed variable/field/parameter/return type, but use {new HashTree} or {new HashMap} for the actual value.
here are some concrete examples.
forge uses too much heap. i've reduced a fair portion of that in my efforts to purge use of forge.card.cardFactory.getAllCards and .getCards. i am now working on another heap waster: hashes. trees are more heap efficient and still have spiffy performance.
i am going through the code now, converting variable, field, parameter, and return types from Hashtable, HashMap, and HashSet to Map, Map, and Set, respectively. i am also converting instances to use TreeMap, TreeMap, and TreeSet implementations where feasible.
feasible? yes. there is one important subtle fact about trees. the keys of a TreeMap and the items in a TreeSet must have a "natural ordering". this is java's fancy way of saying that these things need to implement Comparable<T>. if your key (or set's item) type is forge.Card, String, an enum type, or any of Java's primitive or numeric classes like Integer, you're fine. if not, you need to make sure the key (or set's item) type implements Comparable correctly. you can write your own. for an example of a homemade Comparable, see forge.Card.compareTo. if you aren't confident enough for that, use a generic Map or Tree-typed variable/field/parameter/return type, but use {new HashTree} or {new HashMap} for the actual value.
here are some concrete examples.
- Code: Select all
//No:
HashMap<String,String> variableName = new HashMap<String,String>();
HashSet<String> variableName2 = new HashSet<String>();
//Instead:
Map<String,String> variableName = new TreeMap<String,String>();
Set<String> variableName2 = new TreeSet<String>();
//No:
public HashMap<Object,String> methodName()
public HashSet<Object> methodName2()
//Instead:
public Map<Object,String> methodName()
public Set<Object> methodName2()
//No:
public void methodName3(HashMap<Object,String> map)
public void methodName4(HashSet<Object> set)
//Instead:
public void methodName3(Map<Object,String> map)
public void methodName4(Set<Object> set)
//No:
public MyClass {
private HashMap<String,String> fieldName;
private HashSet<String> fieldName2;
...
fieldName = new HashMap<String,String>();
fieldName2 = new HashSet<String>();
...
}
//Instead:
public MyClass {
private Map<String,String> fieldName;
private Set<String> fieldName2;
...
fieldName = new TreeMap<String,String>();
fieldName2 = new TreeSet<String>();
...
}
- Code: Select all
//No:
HashMap<Incomparable,String> variableName = new HashMap<Incomparable,String>();
HashSet<Incomparable> variableName2 = new HashSet<Incomparable>();
//Instead:
Map<Incomparable,String> variableName = new HashMap<Incomparable,String>();
Set<Incomparable> variableName2 = new HashSet<Incomparable>();
//No:
public MyClass {
private HashMap<Incomparable,String> fieldName;
private HashSet<Incomparable> fieldName2;
...
fieldName = new HashMap<Incomparable,String>();
fieldName2 = new HashSet<Incomparable>();
...
}
//Instead:
public MyClass {
private Map<Incomparable,String> fieldName;
private Set<Incomparable> fieldName2;
...
fieldName = new HashMap<Incomparable,String>();
fieldName2 = new HashSet<Incomparable>();
...
}