1. Collectiondefault 与public private 作用类似,多用于接口,其修饰得方法不需要被实现
public interface Collection1.1. Listextends Iterable { //--------查询操作--------- int size();//大小 boolean isEmpty();//是否为空 boolean contains(Object o);//是否包含某一个元素 Iterator iterator();//迭代器,因为其继承了Iterable,所以其可进行迭代 Object[] toArray();//返回一个具有集合所有元素得数组 T[] toArray(T[] a); //同上,返回指定类型得数组 //--------修改操作--------- boolean add(E e);//添加进集合 boolean remove(Object o);//从集合中删除 boolean containsAll(Collection> c); //是否包含传入集合中所有得元素 boolean addAll(Collection extends E> c);//向本集合中追加另一个集合得全部元素 boolean removeAll(Collection> c);//删除本集合中所包含传入集合中的所有元素 default boolean removeIf(Predicate super E> filter) { Objects.requireNonNull(filter); boolean removed = false; final Iterator each = iterator(); while (each.hasNext()) { if (filter.test(each.next())) { each.remove(); removed = true; } } return removed; } boolean retainAll(Collection> c); void clear();//删除集合中的所有元素 //--------比较与散列--------- boolean equals(Object o);//比较 int hashCode();//返回集合的hashCode default Stream stream() { return StreamSupport.stream(spliterator(), false); } default void forEach(Consumer super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } }
public interface List1.2. Setextends Collection { //省略父类中已有的方法和简单的增删改查方法 ...... default void replaceAll(UnaryOperator operator) { Objects.requireNonNull(operator); final ListIterator li = this.listIterator(); while (li.hasNext()) { li.set(operator.apply(li.next())); } } default void sort(Comparator super E> c) { Object[] a = this.toArray(); Arrays.sort(a, (Comparator) c); ListIterator i = this.listIterator(); for (Object e : a) { i.next(); i.set((E) e); } } int lastIndexOf(Object o); //返回该元素在集合中最后出现的位置 ListIterator listIterator(); // 返回一个ListIterator 使其具有ListIterator的能力 ListIterator listIterator(int index); //从指定位置(下标)开始返回一个ListIterator List subList(int fromIndex, int toIndex); //截取List,从指定开始位置(下标)截取到指定结束位置 }
1.3. QueueSet集合接口,继承了Collection
此接口,其目的是为了约定一个不同于List的不包含重复元素的集合
public interface Queue2. Mapextends Collection { boolean add(E e); //添加,向队列中添加元素 boolean offer(E e); //添加,向队列中添加元素 E remove();//检索并删除此队列的头部元素,为空时抛出异常 E poll(); //删除队列头部元素,为空时返回 null E element(); //查询队列的头部元素,在队列为空时,抛出一个异常 E peek(); //查询队列的头部元素,在队列为空时,返回 null。 }
public interface Map{ int size();//用以返回集合的大小 boolean isEmpty();//是否为空 boolean containsKey(Object key);//是否包含传入的键(Key) boolean containsValue(Object value);//是否包含传入的值(Value) V get(Object key); //通过键(Key)获取对应值(Value) V remove(Object key);//通过键(Key)删除对应值(Value) void putAll(Map extends K, ? extends V> m); //将传入的Map集合元素全部添加进本集合 void clear();//清除集合中的所有元素 Set keySet();//将本Map集合中的所有键(Key)组成Set集合返回 Collection values(); //将本Map集合中所有键所对应的值以Collection返回 Set > entrySet(); interface Entry {......} default V getOrDefault(Object key, V defaultValue) { V v; return (((v = get(key)) != null) || containsKey(key)) ? v : defaultValue; } default void forEach(BiConsumer super K, ? super V> action) { Objects.requireNonNull(action); for (Map.Entry entry : entrySet()) { K k; V v; try { k = entry.getKey(); v = entry.getValue(); } catch(IllegalStateException ise) { // this usually means the entry is no longer in the map. throw new ConcurrentModificationException(ise); } action.accept(k, v); } } default void replaceAll(BiFunction super K, ? super V, ? extends V> function) {...} default V putIfAbsent(K key, V value) { V v = get(key); if (v == null) { v = put(key, value); } return v; } default boolean replace(K key, V oldValue, V newValue) {...} default V replace(K key, V value) {...} default V computeIfAbsent(K key, Function super K, ? extends V> mappingFunction) {...} default V computeIfPresent(K key, BiFunction super K, ? super V, ? extends V> remappingFunction) {...} default V merge(K key, V value, BiFunction super V, ? super V, ? extends V> remappingFunction) { Objects.requireNonNull(remappingFunction); Objects.requireNonNull(value); V oldValue = get(key); V newValue = (oldValue == null) ? value : remappingFunction.apply(oldValue, value); if(newValue == null) { remove(key); } else { put(key, newValue); } return newValue; } }



