Java Review (XXIV collection ----- Collection Interface)




Collection Interface Interface List is the parent, and Set Queue interface methods defined in this interface for operating both set Set, also be used to operate a set of List and Queue. Collection interface defined in a method of operating a set of elements:

  • boolean add (Object 0): This method is used to add an element to the collection. If the collection operation of changing the object is added,
    it returns true.
  • boolean addAll (Collection c): This method is added to the set of all elements c in the specified collection. If the collection operation of changing the object is added, it returns true.
  • void clear (): clearing all elements in the set, the set length becomes 0.
  • boolean contains (Object 0): returns the collection contains the specified element.
  • boolean containsAll (Collection c): returns the collection contains a collection of all the elements in c.
  • boolean isEmpty (): Returns the collection is empty. Returns true if the length is set to 0, otherwise returns false.
  • Iterator iterator (): Returns an Iterator objects, for traversing the collection elements.
  • boolean remove (Object 0): delete the specified element in the collection. When the set contains one or more elements. When this
    method removes only the first matching element, the method returns true o
  • boolean removeAll (Collection c): c deletes all elements in the set from the collection contains (by calling the method corresponding to
    the set of the set of minus c), if more than one element or deleted, the method returns true.
  • boolean retainAll (Collection c): Delete element c is not included in the collection from the collection (equivalent to calling the method of collection and the collection becomes the set intersection c), if the operation is to change the set of the method is called, the the method returns true.
  • int size (): This method returns the number of elements in the collection.
  • Object [] toArray (): This method converts into a set of arrays, all of the elements into a corresponding set of array elements.

Following procedure demonstrates how to operate the collection Collection element by the above method:


public class CollectionTest
{
	public static void main(String[] args)
	{
		Collection c = new ArrayList();
		// 添加元素
		c.add("孙悟空");
		// 虽然集合里不能放基本类型的值,但Java支持自动装箱
		c.add(6);
		System.out.println("c集合的元素个数为:" + c.size()); // 输出2
		// 删除指定元素
		c.remove(6);
		System.out.println("c集合的元素个数为:" + c.size()); // 输出1
		// 判断是否包含指定字符串
		System.out.println("c集合的是否包含\"孙悟空\"字符串:"
			+ c.contains("孙悟空")); // 输出true
		c.add("轻量级Java EE企业应用实战");
		System.out.println("c集合的元素:" + c);
		Collection books = new HashSet();
		books.add("轻量级Java EE企业应用实战");
		books.add("疯狂Java讲义");
		System.out.println("c集合是否完全包含books集合?"
			+ c.containsAll(books)); // 输出false
		// 用c集合减去books集合里的元素
		c.removeAll(books);
		System.out.println("c集合的元素:" + c);
		// 删除c集合里所有元素
		c.clear();
		System.out.println("c集合的元素:" + c);
		// 控制books集合里只剩下c集合里也包含的元素
		books.retainAll(c);
		System.out.println("books集合的元素:" + books);
	}
}

Collections like container, the container function in real life, nothing more than adding objects, deleting objects, empty the container, the container to determine whether it is blank, collection classes provides a corresponding method for these functions.

API:java.util.Collection



reference:

[1]: "crazy Java handouts"

Published 136 original articles · won praise 36 · views 30000 +

Guess you like

Origin blog.csdn.net/sinat_40770656/article/details/102963887
Recommended