「转」HashSet HashTable HashMap - J
1
Set的集合里不允许对象有重复的值,
List允许有重复,它对集合中的对象进行索引,
Queue的工作原理是FCFS算法(First Come, First Serve)。
1.HashSet
Set接口,不允许重复值。
public boolean add(Object o)
2.HashMap
Map接口(Kay-Value pair)
TreeMap
TreeMap保存了对象的排列次序,而HashMap则不能。
(Ordered by the key.)
HashMap
HashMap允许键和值为null。HashMap是非synchronized的,但collection框架提供方法能保证HashMap synchronized,这样多个线程同时访问HashMap时,能保证只有一个线程更改Map。
(No ordering)
public Object put(Object Key,Object value)
3.HashSet和HashMap的区别
HashMap HashSet
HashMap实现了Map接口 HashSet实现了Set接口
HashMap储存键值对(允许null) HashSet仅仅存储对象
使用put()方法将元素放入map中 使用add()方法将元素放入set中
HashMap中使用键对象来计算hashcode值 HashSet使用成员对象来计算hashcode值,对于两个对象来说hashcode可能相同,所以equals()方法用来判断对象的相等性,如果两个对象不同的话,那么返回false
HashMap比较快,因为是使用唯一的键来获取对象 HashSet较HashMap来说比较慢
Hash:[Set 用add, Map 和 Table用put]
HashSet()
add();
clear();
contains();
isEmpty();
iterator();
remove();
size();
HashMap
clear();
containsKey(), containsValue();
get(); // get() == containsValue()
isEmpty();
remove();
put();
size();
entrySet(), keySet();
HashTable
clear();
contains(), containsKey();
get();
hashCode();
entrySet(), keySet();
isEmpty();
merge();
put();
remove();
replace();
size();
toString();
2
感谢原作者大兔Leetcode刷题手册
Java Collection API Summary
最佳做题频繁用到Java Collection的API, 在此做个列举总结,供以后参考。
ArrayList
Constructor
ArrayList()
ArrayList(Collection c)
ArrayList(int capacity)
Method
- void add(int index, Object element): Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index > size())
- boolean add(Object): Appends to the end of list.
- boolean addAll(Collection c): Throws NullPointerException, if the specified collection is null.
- boolean addAll(int index, Collection c): Throws NullPointerException, if the specified collection is null.
- void clear()
- Object clone(): Shallow copy
- boolean contains(Object o): returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
- Object remove(int index): Throws IndexOutOfBoundsException if the index out is of range (index < 0 || index >= size()).
- Object set(int index, Object element): Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()).
- int size()
Map
[图片上传失败...(image-9bd10b-1523468288172)]
Key-Value pair.
Implementation: HashMap, TreeMap, LinkedHashMap
- HashMap is implemented as a hash table, and there is no ordering on keys or values.
- TreeMap is implemented based on red-black tree structure, and it is ordered by the key.
- LinkedHashMap preserves the insertion order
- Hashtable is synchronized, in contrast to HashMap. It has an overhead for synchronization.
Common Exceptions:
NoSuchElementException
ClassCastException: incompatible with the elements in the map.
NullPointerException
UnsupportedOperationException
Methods
boolean add(Object obj)
boolean addAll(Collection c)
boolean contains(Object obj)
boolean containsAll(Collection c)
boolean equals(Object obj)
boolean isEmpty()
boolean size()
HashMap
Constructor
HashMap()
HashMap(Map m)
HashMap(int capacity)
HashMap(int capacity, float fillRatio)
Methods
boolean containsKey(Object key)
boolean containsValue(Object value)
Set entrySet(): Map.Entry type
Set keySet(): a set of the keys in this map
Object put(Object key, Object value)
putAll(Map m)
Set
Implementation: AbstractSet, EnumSet, HashSet, LinkedHashSet, TreeSet, ConcurrentSkipListSet
Commonly used:
HashSet: based on hash table
TreeSet: TreeMap to store elements. sorted by comparator.
HashSet
Constructor
HashSet()
HashSet(Collection c)
HashSet(int intialCapacity)
HashSet(int intialCapacity, float loadFactor)
Methods
add(Object): Adds a new element, if it does not exist already.
addAll(Collection): Adds all the elements of the given collection, if the do not exist already. If the given collection is also a set, then the execution of the method results in the union of the two sets.
contains(Object): Returns true if the elements given exists in the set.
containsAll(Collection): Returns true if all the elements in the given collection exist in the set. In case the given collection is a set, the method return true if it is a subset of this set.
equals(Object): Returns true if the given object that is compared with this set is also a set, both of them contain the same number of elements and every element of the given set is contained in this set.
size(): Returns the number of the elements in the set.
remove(Object): Removes the specified elements from the set.
removeAll(Collection): Removes from the set all the elements that the collection contains.
clear(): Removes all the elements from the set, resulting in an empty set.
isEmpty(): Returns true if the set has no elements.
toArray(): Return an array containing all the elements of this set.
Iterator iterator()
Stack and Queue
ArrayDeque
Methods:
int size()
boolean isEmpety()
boolean contains(Object o)
Iterator iterator
For Stack:
void push(E e)
E pop()
For Queue:
boolean offer(E e): inserts at the end of queue
E poll(): remove the head of the queue
E peek(): return the head of the queue
</article>
</main>