总结一下 Java collections 在oj中使用
创建数组
//不可改变
int size=10;
int[] data = new int [size];
//可变
ArrayList<Integer> staff = new ArrayList<Integer>();
ArrayList
//Constuctor
ArrayList() ;
ArrayList(int initialCapacity)
//ArrayList 即使固定大小,也先要通过add添加,才能利用索引进行搜索
//eg
ArrayList<Integer> alist = new ArrayList<Integer>();
//method
void add(int index, E element)
boolean add(E e) //Appends the specified element to the end of this list.
E get(int index);
int indexOf(Object o); //-1 if this list does not contain the element.
int lastIndexOf(Object o);
E set(int index, E element);
E remove(int index);
boolean remove(Object o);
Iterator<E> iterator();
int size();
boolean isEmpty();
void clear();
Object[] toArray();
LinkedList
//Constructor
// 默认构造函数
LinkedList()
// 创建一个LinkedList,保护Collection中的全部元素。
LinkedList(Collection<? extends E> collection)
//add
boolean add(E object)
void add(int location, E object)
boolean addAll(Collection<? extends E> collection)
boolean addAll(int location, Collection<? extends E> collection)
void addFirst(E object)
void addLast(E object)
//delete
void clear()
//copy
Object clone()
//judge
boolean contains(Object object)
//get element
E element()
E get(int location)
E getFirst()
E getLast()
int indexOf(Object object)
int lastIndexOf(Object object)
ListIterator<E> listIterator(int location)
boolean offer(E o)
boolean offerFirst(E e)
boolean offerLast(E e)
//peek and poll
E peek()
E peekFirst()
E peekLast()
E poll()
E pollFirst()
E pollLast()
//remove
E pop()
void push(E e)
E remove()
E remove(int location)
boolean remove(Object object)
E removeFirst()
boolean removeFirstOccurrence(Object o)
E removeLast()
boolean removeLastOccurrence(Object o)
//set
E set(int location, E object)
int size()
<T> T[] toArray(T[] contents)
Object[] toArray()
直接使用特数值的函数实现 栈, 队列 即可。(其他接口记起来容易乱,,,)
抛出异常 | 特殊值 | 抛出异常 | 特殊值 | |
---|---|---|---|---|
插入 | addFirst(e) | offerFirst(e) | addLast(e) | offerLast(e) |
移除 | removeFirst() | pollFirst() | removeLast() | pollLast() |
检查 | getFirst() | peekFirst() | getLast() | peekLast() |
Set
- HashSet : 能快速访问的Set
- TreeSet : 排序Set,
- LinedHashSet : 记录下插入时的顺序
//Constructor
new HashSet();
new HashSet(int initialCapacity);
//add
boolean add();
//contain
boolean contains(Object o);
//remove
boolean remove(Object o);
//other
int size()
boolean isEmpty();
Iterator<?> iterator();
void clear();
Object clone();
//TreeSet (Other)
E first();
E last();
E higher(E object);
E lower(E object);
E pollFirst();
E pollLast();
ArrayDeque
//Constructor
ArrayDeque()
ArrayDeque(int initialCapacity)
//add
void addFirst(E element)
void addLast(E element)
boolean offerFirst(E element)
boolean offerLast(E element)
//pop
E removeFirst()
E removeLast()
E pollFirst()
E pollLast()
//get
E getFirst()
E getLast()
E peekFirst()
E peekLast()
PriorityQueue
PriorityQueue()
PriorityQueue(int initialCapacity)
PriorityQueue(int initialCapacity, Comparator<? super E> c)
//add
void add()
boolean offer()
//get
E element()
E peek()
//pop
void remove()
boolean poll()
void remove(Object o)
HashMap
// Constructor
HashMap()
// 指定“容量大小”的构造函数
HashMap(int capacity)
// 指定“容量大小”和“加载因子”的构造函数
HashMap(int capacity, float loadFactor)
// 包含“子Map”的构造函数
HashMap(Map<? extends K, ? extends V> map)
//add
V put(K key, V value)
void putAll(Map<? extends K, ? extends V> map)
//get
V get(Object key)
Set<Entry<K, V>> entrySet()
Set<K> keySet()
//pop
V remove(Object key)
//judge
boolean isEmpty()
boolean containsKey(Object key)
boolean containsValue(Object value)
//other
void clear()
Object clone()
int size()
Collection<V> values()
//遍历方法
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for(Map.Entry<Integer, Integer> entry : map.entrySet()){
System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue())
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, Integer> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
TreeMap
// Construtor
TreeMap()
// 创建的TreeMap包含Map
TreeMap(Map<? extends K, ? extends V> copyFrom)
// 指定Tree的比较器
TreeMap(Comparator<? super K> comparator)
// 创建的TreeSet包含copyFrom
TreeMap(SortedMap<K, ? extends V> copyFrom)
//add
V put(K key, V value)
//get
V get(Object key)
Set<Entry<K, V>> entrySet()
Set<K> keySet()
Entry<K, V> firstEntry()
Entry<K, V> lastEntry()
K firstKey()
K lastKey()
Entry<K, V> floorEntry(K key)
K floorKey(K key)
Entry<K, V> lowerEntry(K key)
K lowerKey(K key)
Entry<K, V> higherEntry(K key)
K higherKey(K key)
Entry<K, V> ceilingEntry(K key)
K ceilingKey(K key)
//judge
boolean containsKey(Object key)
void clear()
Object clone()
Comparator<? super K> comparator()
//delete
Entry<K, V> pollFirstEntry()
Entry<K, V> pollLastEntry()
V remove(Object key)
//other
int size()
boolean isEmpty()
Collections interface
Classes in the collections framework