Java advanced -- container (1)

Source: Internet
Author: User

Container Concept

An array is a container that loads various other objects,

Collection Interface

The Collection interface defines how to store a group of objects. Its Sub-interfaces Set and List define the storage methods respectively.

Data Objects in the Set cannot be duplicated without any sequence.

The data objects in the List can be split sequentially.

Methods defined by the Collection interface:

int size()
Returns the number of elements in the collection. If the collection contains more Integer. MAX_VALUE, Returns Integer. MAX_VALUE.
boolean isEmpty()
If this collection does not contain elements, return True.
boolean contains(Object o)
If the collection contains the specified element True. More specifically, when and only when this collection contains at least one (O = null? E = null: o. equals (e ))Element E, Return True.
Iterator
  
  iterator()
 
Returns the iterator that iterates on the elements of this collection. There is no guarantee on the order in which elements return (unless this collection is a class instance that can guarantee the order ).
boolean add(E e)
Make sure that the collection contains the specified elements (optional ). If this collection is changed due to a call, return True. (If the collection does not allow repeated elements and already contains the specified elements False.)
boolean remove(Object o)
Remove a single instance of the specified element from this collection. If yes (optional ). More specifically, if this collection contains one or more (O = null? E = null: o. equals (e ))Element ETo remove such elements. If the collection contains the specified element (or the collection is changed due to a call ), True.
boolean containsAll(Collection
  c)
If the collection contains all elements in the specified collection True.
boolean addAll(Collection
  c)
Add all elements in the specified collection to this collection (optional ). If you modify the specified collection while performing this operation, the operation is not clear. (This means that if the specified collection is the collection and the collection is not empty, the calling behavior is uncertain .)
boolean removeAll(Collection
  c)
Removes all elements in the collection that are also included in the specified collection (optional ). After this call is returned, the collection does not contain any elements identical to the specified collection.
void clear()
Removes all elements from the collection (optional ). After this method is returned, unless an exception is thrown.
boolean equals(Object o)
Compare whether the collection is equal to the specified object.

The following is a simple example:

import java.util.*;public class Test {public static void main(String[] args) {Collection c = new ArrayList();c.add("hello");c.add("100");c.add(10);System.out.println(c.size());System.out.println(c);}}

When calling methods such as remove and contains, the container class object must compare whether the objects are equal. This will involve the equals and hashCode methods of the object type. For custom types, you need to override the equals and hashCode methods to implement custom object equality rules.

Iterator Interface

All the container classes that implement the Collection interface have an iterator method to return an object that implements the Iterator interface.

The Iterator object is called an Iterator for convenient traversal of elements in the container.

The Iterator interface defines the following methods:

boolean hasNext()
If there are still elements that can be iterated, return True. (In other words, if NextReturns an element instead of an exception. True).

Next()

Returns the next element of the iteration.
void remove()
Remove the last element returned by the iterator from the collection to which the iterator points (optional ). Each call NextThis method can be called only once. If the collection point pointed to by the iterator is modified in other ways than this method, the iterator behavior is uncertain.

For example:

import java.util.*;public class Test {public static void main(String[] args) {Collection c = new ArrayList();c.add("a");c.add("100");c.add(10);Iterator t = c.iterator();while(t.hasNext()){System.out.println(t.next());}}}


The remove Method of the Iterator object is the only security method for deleting elements during iteration.

Set Interface

The Set interface is a Collection sub-interface. The Set interface does not provide additional methods, but the elements in the container class that implements the Set interface are unordered and cannot be repeated.

Classes that implement the Set container include HashSet and TreeSet.

For example:

import java.util.*;public class HashSetTest {public static void main(String[] args) {Set s1=new HashSet();Set s2=new HashSet();s1.add("a");s1.add("b");s1.add("c");s2.add("e");s2.add("c");s2.add("a");Set sn=new HashSet(s1);sn.retainAll(s2);Set su=new HashSet(s1);su.addAll(s2);System.out.println(sn);System.out.println(su);}


If any omissions exist, please add them.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.