HashCode and Equals
- If two objects are equal according to equals() method, they must have the same hash code
- If two objects have smae hash code, they do not have to be equal
- Override equals() and hashcode() at same time
- How many buckets used in HashMap and rehashing
public class Car
{
private String maker;
private String color;
public Car(String maker, String color)
{
this.maker = maker;
this.color = color;
}
public String toString()
{
return maker+": "+color;
}
@Override
public boolean equals(Object c)
{
Car temp = (Car) c;
System.out.println("Equals ...");
if(maker.equals(temp.maker) && color.equals(temp.color))
return true;
else
return false;
}
@Override
public int hashCode()
{
int code = maker.hashCode()+color.hashCode();
System.out.println("Hash Code: "+code);
return code;
}
}
import java.util.*;
public class H
{
public static void main(String args[])
{
HashMap<Car, Integer> m = new HashMap<Car, Integer>();
Car c1 = new Car("Buick", "White");
Car c2 = new Car("Honda", "Blue");
//put
System.out.println("Insert objects into hashMap ...");
m.put(c1, 1);
m.put(c2, 2);
//get
System.out.println("Access object in hashMap ...");
System.out.println(m.get(new Car("Buick", "White")));
}
}
HashMap
- HashMap implements Hashing
- Does not maintain the order of insertion
- HashMap is non synchronized
- HashMap is an advanced version and improvement on the Hashtable, HashMap was created later
import java.util.*;
public class H
{
public static void main(String args[])
{
HashMap<String, Integer> m = new HashMap<String, Integer>();
//put
m.put("a", 10);
m.put("b", 20);
m.put("c", 30);
m.put("d", 40);
//get
System.out.println(m.get("a"));
//keySet
Set<String> keys = m.keySet();
System.out.println(keys);
//values
Collection<Integer> values = m.values();
System.out.println(values);
//entrySet
Set<Map.Entry<String, Integer>> entries = m.entrySet();
for(Map.Entry<String, Integer> e : entries)
System.out.println(e.getKey()+" "+e.getValue());
//remove
m.remove("c");
//toString
System.out.println(m);
}
}
LinkedHashMap
- Keep track of order of insertion
import java.util.*;
public class H
{
public static void main(String args[])
{
LinkedHashMap<String, Integer> m = new LinkedHashMap<String, Integer>();
//put
m.put("a", 10);
m.put("b", 20);
m.put("c", 30);
m.put("d", 40);
//get
System.out.println(m.get("a"));
//keySet
Set<String> keys = m.keySet();
System.out.println(keys);
//values
Collection<Integer> values = m.values();
System.out.println(values);
//entrySet
Set<Map.Entry<String, Integer>> entries = m.entrySet();
for(Map.Entry<String, Integer> e : entries)
System.out.println(e.getKey()+" "+e.getValue());
//remove
m.remove("c");
//toString
System.out.println(m);
}
}
TreeMap
- Store unique elements in a sorted order
- TreeMap implements Red-Black Tree
import java.util.*;
public class H
{
public static void main(String args[])
{
TreeMap<String, Integer> m = new TreeMap<String, Integer>();
//put
m.put("a", 10);
m.put("b", 20);
m.put("c", 30);
m.put("d", 40);
//firstKey
System.out.println(m.firstKey());
//lastKey
System.out.println(m.lastKey());
//lowerKey
System.out.println(m.lowerKey("b"));//a
//ceilingKey
System.out.println(m.ceilingKey("b"));//b
//higherKey
System.out.println(m.higherKey("b"));//c
//floorKey
System.out.println(m.floorKey("b"));//b
System.out.println(m);
//headMap
SortedMap<String, Integer> hMap = m.headMap("b");
System.out.println(hMap);
//subMap
SortedMap<String, Integer> sMap = m.subMap("b", "d");
System.out.println(sMap);
//tailMap
SortedMap<String, Integer> tMap = m.tailMap("b");
System.out.println(tMap);
}
}
EnumMap
- All keys of each EnumMap instance must be keys of a single enum type
- EnumMap doesn’t allow null key and throw NullPointerException, at same time null values are permitted
public enum Day
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday;
}
import java.util.*;
public class M
{
public static void main(String args[])
{
EnumMap<Day, String> m = new EnumMap<Day, String>(Day.class);
//put
m.put(Day.Monday, "Teaching and Research");
m.put(Day.Tuesday, "Prepare Course Materials");
m.put(Day.Wednesday, "Running ...");
m.put(Day.Thursday, "Meeting ...");
//get
System.out.println(m.get(Day.Thursday));
//toString
System.out.println(m);
}
}
- Hashtable is synchronized
import java.util.*;
public class H
{
public static void main(String args[])
{
Hashtable<String, Integer> grade = new Hashtable<String, Integer>();
//put
grade.put("David", 90);
grade.put("Jeremy", 80);
grade.put("Antonio", 85);
grade.put("Ray", 95);
//get
System.out.println("Grade for David: "+grade.get("David"));
//remove
grade.remove("Antonio");
//toString
System.out.println(grade);
//containsKey
if(grade.containsKey("Jeremy"))
System.out.println("Contains the record for Jeremy ...");
//keySet
System.out.println(grade.keySet());
//values
System.out.println(grade.values());
}
}
- The Properties can be saved to a stream or loaded from a stream
- Each key and its corresponding value in the property list is a string
import java.util.*;
import java.io.*;
public class P
{
public static void main(String args[])
{
Properties t = new Properties();
t.setProperty("color", "blue");
t.setProperty("maker", "Honda");
//save properties
try
{
FileOutputStream output = new FileOutputStream("temp.dat");
t.store(output, "Save Properties");
output.close();
}
catch (IOException ioException)
{
ioException.printStackTrace();
}
Properties t2 = new Properties();
//load properties
try
{
FileInputStream input = new FileInputStream("temp.dat");
t2.load(input);
input.close();
}
catch (IOException ioException)
{
ioException.printStackTrace();
}
t.list(System.out);
}
}