"List and set of the Java Collection framework"

Source: Internet
Author: User

I. Overview

The Java Collection Framework defines a series of classes that are containers for storing data. Compared to arrays, StringBuffer (StringBuilder), it is characterized by:

1. Used to store objects.

2. The set length is variable.

3. The base data type cannot be stored.

Compare the characteristics of three types of containers:

The array must hold the same element.
StringBuffer must be converted to a string to be used if it is almost impossible to come up with a single element.
Data has a lot of objects to use, objects have many, and use collections to save.

Collection container because the internal data structure has a variety of specific containers, the continuous upward extraction of the formation of a set of framework.
The top layer of the frame is the collection interface.

Second, collection interface

Both the set interface and the list interface implement the collection interface, so it is obvious that the collection interface holds the common content of the set interface and the list interface.

Methods in the collection interface:

1. Add.

 boolean add(E e)
Make sure that this collection contains the specified element (optional action).
 boolean addAll(Collection<? extends E> c)
Adds all the elements in the specified collection to this collection (optional action).

Where the parameter e can be temporarily understood as an object (actually used before JDK1.4 is always an object).

2. Delete

 boolean Remove (object o)
            removes a single instance of the specified element from this collection, if one exists (optional action).
  ; Boolean removeall (collection<?> c)
            removes all the elements in this collection that are also contained in the specified collection (optional action).
 void clear()
Removes all the elements in this collection (optional action).

3. Judging

 boolean contains (object o)
            returns true if this collection contains the specified element.
  ; Boolean containsall (collection<?> c)
            returns true if this collection contains all the elements in the specified collection.
 boolean isEmpty()
Returns Trueif this collection contains no elements.

4. Access.

 int size()
Returns the number of elements in this collection.
Iterator<E> iterator()
Returns an iterator that iterates over the elements of this collection.

5. Other

 boolean retainAll(Collection<?> c)
Leave only those elements in this collection that are also contained in the specified collection (optional action).
 Object[] toArray()
Returns an array that contains all the elements in this collection.
<T> T[]
toArray(T[] a)
Returns an array containing all the elements in this collection; The run-time type of the returned array is the same as the run-time type of the specified array.

"List and set of the Java Collection framework"

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.