Elaborating the Java Collection framework

Source: Internet
Author: User

1. Why use a set frame

When we don't know how many objects will be needed when the program runs, or need to store objects in a more complex way-you can use the Java Collection framework

2. What the Java Collection Framework contains

Interface: (parent Class)Collection interface with List(subclass) interface and Set(subclass)

The interface List interface is also included (ArrayList Collection Implementation class and LinkedList Collection Implementation Class)

The set interface is also included (HashSet Collection Implementation class and TreeSet Collection Implementation Class)

Interface: (parent Class) TheMap interface contains (HASHMAP Collection Implementation class and TreeMap Collection Implementation Class)

The *collections interface provides a variety of algorithms for sorting, traversing, and so on *java the set framework provides us with a set of well-performing, easy-to-use interfaces and classes that are located in the Java.util package

3. Features of Collection, List, and set:

Collection Interface stores a set of non-unique , unordered objects

The List interface stores a set of objects that are not unique , ordered (in order of insertion)

The set interface stores a set of unique, unordered object map interfaces that store a set of key-value objects that provide a key -to value mapping

4. Advantages of ArrayList set and LinkedList set

1.ArrayList implements a variable-length array that allocates contiguous space in memory. High efficiency for traversing elements and random access elements

2.LinkedList adopts the chain list storage mode. High efficiency when inserting and deleting elements

The list interface provides the appropriate method of remove (), contains (), which can be used directly

list Interface Common methods:

A boolean add (Object o) adds an element in the order of the end of the list, starting at the 0 start index position

void Add (int index,object o) adds an element at the specified index position. The index position must be between 0 and the number of elements in the list

int size () returns the number of elements in the list

Object Get (int index) returns the element at the specified index position. The removed element is of type object and requires a forced type conversion before use

Boolean contains (Object O) determines whether the specified element exists in the list

boolean remove (Object o) removes the element from the list

Object Remove (int index) removes the specified position element from the list, starting at index position 0

special methods of LinkedList

void AddFirst (Object o) adds an element to the header of the list

void AddLast (Object o) adds an element at the end of the list

Object GetFirst () returns the first element in a list

Object getlast () returns the last element in the list

Object Removefirst () deletes and returns the first element in a list

Object removelast () deletes and returns the last element in the list

Common methods for map interfaces:

object put (object key, Object val) is stored in a "key-value pair" manner

object get (Object key) returns the associated value according to the key, or null if the specified key does not exist

object Remove (Object key) deletes the "key-value pair" that is mapped by the specified key

int size () returns the number of elements

set KeySet () returns a collection of keys

Collection VALUES () returns a collection of value

boolean containskey (Object key) returns True if there is a "key-value pair" that is mapped by the specified key

Elaborating 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.