Details Java Collection Framework _java

Source: Internet
Author: User
Tags int size

1. Why use the Set frame

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

2, the Java Collection Framework contains content

Interface: (parent Class) Collection interface contains list (subclass) interface and set (subclass)
Interface list interface also contains (ArrayList set implementation class and LinkedList set implementation Class)
Set interface also contains (HashSet set implementation class and TreeSet set implementation Class)
Interface: (parent Class) The Map interface contains (HashMap Collection implementation class and TreeMap collection implementation Class)
*collections interface provides a set of sorting, traversal and other algorithms to achieve the *java set framework provides us with a set of excellent performance, easy to use interface and classes, they are located in the Java.util package

3, Collection, List and set characteristics:

The Collection interface stores a set of objects that are not unique and unordered
The list interface stores a set of objects that are not unique, orderly (insert order)
The set interface stores a set of unique, unordered object map interfaces that store a set of key-value objects that provide a mapping of key to value

4, ArrayList collection and the advantages of the LinkedList set

1, ArrayList implemented a variable length of the array, in memory to allocate contiguous space. High efficiency of traversing elements and random access elements
2, LinkedList using the linked list storage mode. High efficiency when inserting and deleting elements

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

List interface Common methods:

The Boolean add (Object o) adds an element in the order at the end of the list, starting at the start index position from 0
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 a list
Object get (int index) returns the element at the specified index position. The extracted element is 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 an element from the list
Object Remove (int index) deletes the specified position element from the list, starting at the beginning of the index position from 0
Special methods of LinkedList
void AddFirst (Object o) adds elements to the header of the list
void AddLast (Object o) adding elements at the end of the list
Object GetFirst () returns the first element in the list
Object GetLast () returns the last element in the list
Object Removefirst () deletes and returns the first element in the list
Object removelast () deletes and returns the last element in the list

Map interface Common methods:

Object put (Object key, object val) stored in "key-value pairs"
Object get (Object key) returns the associated value based on the key, or null if the specified key does not exist
Object remove (object key) deletes the key-value pairs that are 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

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.