Java- Collections Overview (Collection / Map)

About the collection

  Prior to collections, we use the storage array, the array can store both the value of the basic data types can be stored objects (object reference variable), but can only store a collection of objects.
  A container similar set of Java, an object of the same type (actual object references) are stored together, to form a set. The previous article, we have to introduce, Java after 5 added generics, Java collections can specify a particular object type.
  The basic interface is Java collection classes and interfaces Map Collection interfaces. The interface has two basic methods: add()and iterator()methods.

public interface Collection<E> {
	boolean add(E element);
	Iterator<E> iterator();
	...
}

Wherein the add()method is used to add an element to the collection, the collection change if additional element, return true; conversely, returns false. If, when the set of elements to be added already exists, added again, returns false. iterator()Method returns an object that implements the Iterator interface, using the iterator object in turn access a collection of elements.

Collection system

Collection inheritance tree collection system

Here Insert Picture Description
Collection system, Set on behalf of unordered collections; representatives List ordered set; Queue represents the set of queues.

Map collection system inheritance tree

Here Insert Picture Description
Map system, the stored data has a mapping relation (key-value) is, the key Map not be repeated.

CLASSIFICATION

  • Set: random, non-repeatable set;
  • List: orderly, duplicate collections;
  • Queue: set of queues;
  • Map: There are a set of mappings;

Collection common method

  • boolean add(E e): Was added to a collection of elements, if a collection has changed, returns true; conversely, returns false.
  • boolean addAll(Collection<? extends E> c): Add all the elements in the set to a specified set of c, if the change set, return true; conversely, returns false.
  • void clear(): Clear all elements in the set, a set of zero length.
  • boolean remove(Object o): Remove the specified element in the collection O, when the collection element comprises one or more O, removes only the first matching element, and return true.
  • boolean removeAll(Collection<?> c);: Remove all elements in the set from this set c equal differencing set, and if a delete or more elements, it returns true.
  • boolean contains(Object o): O determining whether there is a specified element within the set, if present, returns true; conversely, returns false.
  • boolean containsAll(Collection<?> c): Determining whether all elements contained within the set of the set of c which, if included, return true; conversely, returns false.
  • boolean isEmpty(): Determines whether the set is empty, if it is empty, it returns true; conversely, returns false. Length set to 0, that is empty.
  • Iterator iterator(): Returns an Iterator objects, traverse the collection of elements.
  • int size(): Returns the number of elements in the set;
  • Object[] toArray(): The set is converted to an array of all the elements becomes a corresponding set of array elements.

Lambda expressions through the collection

   Java 8 Iterator interface introduced in the Lambda expressions: forEach(Consumer<? super T> action)when the method is called to traverse the collection of elements, elements are passed to a collection Consumer接口of accept(T t)methods and Consumer is 函数式接口, so this is a 函数式编程.
Source as follows:

    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }

Wherein the retuireNonNull(T obj)method of Object, source code is as follows:

    public static <T> T requireNonNull(T obj) {
       if (obj == null)
           throw new NullPointerException();
       return obj;
   }

Practical example

public class DemoApplication {

   public static void main(String[] args) {

       Collection collection = new ArrayList();
       // 添加元素
       collection.add("add()方法");
       collection.add(1);
       collection.add(2.7);
       collection.add("value1");
       collection.add("value2");

       System.out.println("collection 集合元素为:" + collection);
       
       //lambda表达式遍历
       collection.forEach(c -> System.out.println("forEach: " + c.toString()));

       // 删除指定元素
       System.out.println("删除指定元素1:" + collection.remove(1));
       System.out.println("删除指定元素2:" + collection.remove(2));

       //判断是否包含指定元素
       System.out.println("是否包含2.7:" + collection.contains(2.7));
       System.out.println("是否包含2:" + collection.contains(2));


       //判断是否包含某个集合
       Collection valueSet = new HashSet();
       valueSet.add("value1");
       valueSet.add("value2");
       System.out.println("是否包含集合: " + collection.containsAll(valueSet));

       //求差集:去除另一个集合的所有元素
       collection.removeAll(valueSet);
       System.out.println("求差集后:" + collection);

       //清空集合元素
       collection.clear();
       System.out.println("清空集合后: " + collection);

       //集合长度
       System.out.println("集合长度: " + collection.size());

       //求交集:只保留另一个集合的元素
       valueSet.retainAll(collection);
       System.out.println("求交集: " + valueSet);
       }
}

operation result:

collection 集合元素为:[add()方法, 1, 2.7, value1, value2]
forEach: add()方法
forEach: 1
forEach: 2.7
forEach: value1
forEach: value2
删除指定元素1:true
删除指定元素2:false
是否包含2.7:true
是否包含2:false
是否包含集合: true
求差集后:[add()方法, 2.7]
清空集合后: []
集合长度: 0
求交集: []

Reference books
"madness the Java"
"the Java core technology Volume"

Guess you like

Origin www.cnblogs.com/Andya/p/12548619.html