Collection is the root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The JDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.
// Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available. booleanadd(E e); // The offer method inserts an element if possible, otherwise returning false. This differs from the Collection.add method, which can fail to add an element only by throwing an unchecked exception. The offer method is designed for use when failure is a normal, rather than exceptional occurrence, for example, in fixed-capacity (or "bounded") queues. booleanoffer(E e);
// Retrieves and removes the head of this queue. This method differs from poll() only in that it throws an exception if this queue is empty. E remove(); // Retrieves and removes the head of this queue, or returns null if this queue is empty. E poll();
// The element() and peek() methods return, but do not remove, the head of the queue. // This method differs from peek only in that it throws an exception if this queue is empty. E element(); // Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty. E peek();
E removeFirst(); // 移除队首元素并将其返回,当队列为空时抛出NoSuchElementException异常 E removeLast(); // 移除队尾元素并将其返回,当队列为空时抛出NoSuchElementException异常 E pollFirst(); // 移除队首元素并将其返回,当队列为空时返回null E pollLast(); // 移除队尾元素并将其返回,当队列为空时返回null
E getFirst(); // 返回队首元素,当队列为空时抛出NoSuchElementException异常 E getLast(); // 返回队尾元素,当队列为空时抛出NoSuchElementException异常 E peekFirst(); // 返回队首元素,当队列为空时返回null E peekLast(); // 返回队尾元素,当队列为空时返回null
publicstatic <T> intbinarySearch(T[] a, T key, Comparator<? super T> c); publicstatic <T> intbinarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)
该方法只能用于已有序,且升序排列的数组
数组中的两个元素按照比较器c排序必须是升序的
可以在整个数组中查找,也可以在某个范围内查找。
publicstatic <T> booleanequals(T[] a, T[] a2, Comparator<? super T> cmp);
当两个数组根据比较器cmp比较完全相等时返回true。
publicstaticvoidfill(Object[] a, Object val); publicstaticvoidfill(float[] a, int fromIndex, int toIndex, float val);
利用给定的val值去填充数组。
可以填充整个数组,也可以只填充部分数组。
publicstatic <T> voidsort(T[] a, Comparator<? super T> c); publicstatic <T> voidsort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c); publicstatic <T> voidparallelSort(T[] a, Comparator<? super T> cmp); publicstatic <T> voidparallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp);