Coding

「转」HashSet HashTable HashMap - J

2018-04-12  本文已影响0人  程序猪小羊

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

Map

[图片上传失败...(image-9bd10b-1523468288172)]

Key-Value pair.
Implementation: HashMap, TreeMap, LinkedHashMap

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>

上一篇 下一篇

猜你喜欢

热点阅读