TreeMap vs HashMap

Both TreeMap & HashMap are two different implementation of the Map interface. Even though this post is titled “TreeMap vs HashMap” I would like to say how they are connected and how much similar they are.

Both TreeMap & HashMap are not synchronized. To make it synchronized we have to explicitly call Collections.synchronizedMap( mapName ) . Both supports “fail-fast” iterators. Both of them doesn’t support duplicate keys.

HashMap

HashMap allows null as both keys and values. HashMap is useful when we need to access the map without cosidering how they are added to the map (means, unordered lookup of values using their keys). HashMap is synchronized while it is being looked up. HashMap doesn’t allow duplicated entries.

The performance of HashMap is based on two optional parameter which we can specify during the creation of the HashMap. Initial capacity & load factor. Initial capacity is the bucket size assigned to a HashMap during it’s creation. Load factor decides when the HashMap needs to be expanded. If the load factor is 0.75, the size will be increased when the current size of the map crosses 75% of its capacity.

TreeMap

The basic difference between HashMap & TreeMap is that, in a TreeMap the elements are stored in a tree. TreeMap allows us to retrieve the elements in some sorted order defined by the user. So we can say that TreeMap is slower than HashMap. This is the only implementation based on SortedMap interface.

TreeMap allows us to specify an optional Comparator object during its creation. The keys should be compatible with the comparator specified. This comparator decides the order by which the keys need to be sorted.


public interface Comparator

2 {

3 public int compare (Object object1, Object object2);

4 public boolean equals (Object object);

5 }

If we are not specifying the comparator, TreeMap will try to sort the keys in the natural order. Means will consider them as instances of Comparable interface.


public interface Comparable

2 {

3 public int compareTo (Object objectToCompare);

4 }

Classes like Integer, String, Double etc implements Comparable interface. So if we are to use an object of a custom class as the key, ensure that it’ s class implements the Comparable interface.


public class MyCustomKey implements Comparable

02 {

03 private int value;

04 public MyCustomKey(int value)

05 {

06 this.value = value;

07 }

08

09 public int compareTo (MyCustomKey key)

10 {

11 int comparison = 0;

12

13 // Note:

14 // Return -1 if this.value < key.value

15 // Return 0 if this.value = key.value

16 // Return 1 if this.value > key.value

17

18 return (comparison);

19 }

20 }


A common mistake that everyone does is not to override the hashcode(). If we are failing to do so, map.get(new MyCustomKey(<value>)); may not give you what you were expecting. So it is always adviced to override the hashCode() if objects of that class is being used as a key.


public class MyCustomKey implements Comparable

02 {

03 private int value;

04 public MyCustomKey(int value)

05 {}

06 public int compareTo (MyCustomKey key)

07 {}

08

09 public int hashCode()

10 {

11 // Note:

12 // If two objects are equal then their corresponding hashCode ()

13 // should return the same value too.

14 return (this.value * 199);

15 }

16 }



TreeMap 的底层算法是 自平衡二叉寻找树。 自平衡二叉寻找树相对于主要的竞争者Hash 算法有很多的优缺点。 一个优点就是可以快速的列出排序的Key。

[b]下面给出自平衡二叉寻找树插入自转的图。 删除比较简单。[/b]
[img]http://dl.iteye.com/upload/attachment/302373/f3a5b8ea-dc68-3cb9-ac2d-7f438b336823.gif[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值