Java集合工具包com.google.guava:guava


    com.google.guava
    guava
    19.0

Lists


package com.google.common.collect;

@GwtCompatible(emulated = true)
public final class Lists {
  private Lists() {}

  /**
   * 创建一个空元素的ArrayList
   */
  @GwtCompatible(serializable = true)
  public static  ArrayList newArrayList() {
    return new ArrayList();
  }

  /**
   * 创建一个含有指定元素的ArrayList
   */
  @GwtCompatible(serializable = true)
  public static  ArrayList newArrayList(E... elements) {
    checkNotNull(elements); // for GWT
    // Avoid integer overflow when a large array is passed in
    int capacity = computeArrayListCapacity(elements.length);
    ArrayList list = new ArrayList(capacity);
    Collections.addAll(list, elements);
    return list;
  }

  @VisibleForTesting
  static int computeArrayListCapacity(int arraySize) {
    checkNonnegative(arraySize, "arraySize");

    // TODO(kevinb): Figure out the right behavior, and document it
    return Ints.saturatedCast(5L + arraySize + (arraySize / 10));
  }

  /**
   * 创建一个新ArrayList, 并将Iterable初始化到这个新集合
   */
  @GwtCompatible(serializable = true)
  public static  ArrayList newArrayList(Iterable elements) {
    checkNotNull(elements); // for GWT
    // Let ArrayList's sizing logic work, if possible
    return (elements instanceof Collection)
        ? new ArrayList(Collections2.cast(elements))
        : newArrayList(elements.iterator());
  }

  /**
   * 创建一个新的ArrayList,并将Iterator初始化到这个新集合
   */
  @GwtCompatible(serializable = true)
  public static  ArrayList newArrayList(Iterator elements) {
    ArrayList list = newArrayList();
    Iterators.addAll(list, elements);
    return list;
  }

  /**
   * 
   */
  @GwtCompatible(serializable = true)
  public static  ArrayList newArrayListWithCapacity(int initialArraySize) {
    checkNonnegative(initialArraySize, "initialArraySize"); // for GWT.
    return new ArrayList(initialArraySize);
  }

  /**
   * 
   */
  @GwtCompatible(serializable = true)
  public static  ArrayList newArrayListWithExpectedSize(int estimatedSize) {
    return new ArrayList(computeArrayListCapacity(estimatedSize));
  }

  /**
   * 
   */
  @GwtCompatible(serializable = true)
  public static  LinkedList newLinkedList() {
    return new LinkedList();
  }

  /**
   * 
   */
  @GwtCompatible(serializable = true)
  public static  LinkedList newLinkedList(Iterable elements) {
    LinkedList list = newLinkedList();
    Iterables.addAll(list, elements);
    return list;
  }

  /**
   * 创建一个空的CopyOnWriteArrayList
   */
  @GwtIncompatible("CopyOnWriteArrayList")
  public static  CopyOnWriteArrayList newCopyOnWriteArrayList() {
    return new CopyOnWriteArrayList();
  }

  /**
   * 
   */
  @GwtIncompatible("CopyOnWriteArrayList")
  public static  CopyOnWriteArrayList newCopyOnWriteArrayList(
      Iterable elements) {
    // We copy elements to an ArrayList first, rather than incurring the
    // quadratic cost of adding them to the COWAL directly.
    Collection elementsCollection =
        (elements instanceof Collection)
            ? Collections2.cast(elements)
            : newArrayList(elements);
    return new CopyOnWriteArrayList(elementsCollection);
  }

  /**
   * 创建一个不可变的OnePlusArrayList,且包含指定的第一个元素
   */
  public static  List asList(@Nullable E first, E[] rest) {
    return new OnePlusArrayList(first, rest);
  }

  /** @see Lists#asList(Object, Object[]) */
  private static class OnePlusArrayList extends AbstractList
      implements Serializable, RandomAccess {
    final E first;
    final E[] rest;

    OnePlusArrayList(@Nullable E first, E[] rest) {
      this.first = first;
      this.rest = checkNotNull(rest);
    }

    @Override
    public int size() {
      return rest.length + 1;
    }

    @Override
    public E get(int index) {
      // check explicitly so the IOOBE will have the right message
      checkElementIndex(index, size());
      return (index == 0) ? first : rest[index - 1];
    }

    private static final long serialVersionUID = 0;
  }

  /**
   * 
   */
  public static  List asList(@Nullable E first, @Nullable E second, E[] rest) {
    return new TwoPlusArrayList(first, second, rest);
  }

  /** @see Lists#asList(Object, Object, Object[]) */
  private static class TwoPlusArrayList extends AbstractList
      implements Serializable, RandomAccess {
    final E first;
    final E second;
    final E[] rest;

    TwoPlusArrayList(@Nullable E first, @Nullable E second, E[] rest) {
      this.first = first;
      this.second = second;
      this.rest = checkNotNull(rest);
    }

    @Override
    public int size() {
      return rest.length + 2;
    }

    @Override
    public E get(int index) {
      switch (index) {
        case 0:
          return first;
        case 1:
          return second;
        default:
          // check explicitly so the IOOBE will have the right message
          checkElementIndex(index, size());
          return rest[index - 2];
      }
    }

    private static final long serialVersionUID = 0;
  }

  /**
   * 创建一个CartesianList,包含lists中元素的笛卡尔积
   */
  public static  List> cartesianProduct(List> lists) {
    return CartesianList.create(lists);
  }

  /**
   * 
   */
  public static  List> cartesianProduct(List... lists) {
    return cartesianProduct(Arrays.asList(lists));
  }

  /**
   * 创建一个List,原fromList中的每个元素应用function进行处理
   */
  @CheckReturnValue
  public static  List transform(
      List fromList, Function function) {
    return (fromList instanceof RandomAccess)
        ? new TransformingRandomAccessList(fromList, function)
        : new TransformingSequentialList(fromList, function);
  }

  /**
   * Implementation of a sequential transforming list.
   *
   * @see Lists#transform
   */
  private static class TransformingSequentialList extends AbstractSequentialList
      implements Serializable {
    final List fromList;
    final Function function;

    TransformingSequentialList(List fromList, Function function) {
      this.fromList = checkNotNull(fromList);
      this.function = checkNotNull(function);
    }
    /**
     * The default implementation inherited is based on iteration and removal of
     * each element which can be overkill. That's why we forward this call
     * directly to the backing list.
     */
    @Override
    public void clear() {
      fromList.clear();
    }

    @Override
    public int size() {
      return fromList.size();
    }

    @Override
    public ListIterator listIterator(final int index) {
      return new TransformedListIterator(fromList.listIterator(index)) {
        @Override
        T transform(F from) {
          return function.apply(from);
        }
      };
    }

    private static final long serialVersionUID = 0;
  }

  /**
   * Implementation of a transforming random access list. We try to make as many
   * of these methods pass-through to the source list as possible so that the
   * performance characteristics of the source list and transformed list are
   * similar.
   *
   * @see Lists#transform
   */
  private static class TransformingRandomAccessList extends AbstractList
      implements RandomAccess, Serializable {
    final List fromList;
    final Function function;

    TransformingRandomAccessList(List fromList, Function function) {
      this.fromList = checkNotNull(fromList);
      this.function = checkNotNull(function);
    }

    @Override
    public void clear() {
      fromList.clear();
    }

    @Override
    public T get(int index) {
      return function.apply(fromList.get(index));
    }

    @Override
    public Iterator iterator() {
      return listIterator();
    }

    @Override
    public ListIterator listIterator(int index) {
      return new TransformedListIterator(fromList.listIterator(index)) {
        @Override
        T transform(F from) {
          return function.apply(from);
        }
      };
    }

    @Override
    public boolean isEmpty() {
      return fromList.isEmpty();
    }

    @Override
    public T remove(int index) {
      return function.apply(fromList.remove(index));
    }

    @Override
    public int size() {
      return fromList.size();
    }

    private static final long serialVersionUID = 0;
  }

  /**
   * 返回连续的subList,元素个数由size控制
   */
  public static  List> partition(List list, int size) {
    checkNotNull(list);
    checkArgument(size > 0);
    return (list instanceof RandomAccess)
        ? new RandomAccessPartition(list, size)
        : new Partition(list, size);
  }
  
  ...
}


Sets

 

Maps

 

Queues

你可能感兴趣的:(Java)