Java Nine: Collection system

Source: Internet
Author: User
Tags array length set set

After learning the new features of the previous JDK6.0, the JDK will be further studied because of the importance of the collection, so starting with the collection begins analysis:

I. Overview of the collection

Java is an object-oriented language, if we want to operate on multiple objects, then it is necessary to save a number of objects before the operation, then we will certainly think of the array to store, but the array length is fixed, can not meet the requirements of the change. So, Java provides a collection.

The difference between an array and a collection:

Array: Fixed length, can hold base type/reference type, elements in array must be of the same type

Collection: variable-length automatic expansion, only reference types, and element types in the collection can be inconsistent but generally consistent

Second, set system

A collection is a container that can store multiple elements, but because of the different data structures, Java provides a variety of collection classes. The function of the CPC in the collection class is extracted continuously, and finally the set architecture is formed.

Data structure: How to store

Diagram of the collection class in Java:

Three, Collection "single-Value interface"

Collection is the most basic set interface, and a collection represents a set of object, the collection element (Elements). Because some collection allow the same elements to be stored while others do not. Some can be ordered and others are not, thus deriving two sub-class interfaces list and set.

Common methods in collection interfaces:

A: Add features
boolean Add (Object obj): Adds an element to a collection
Boolean AddAll (Collection C): Adds an element of a collection to the collection.
B: Delete Feature
void Clear (): Deletes all elements in the collection.
boolean remove (Object obj): Removes the specified element from the collection
Boolean RemoveAll (Collection c): Removes a specified collection element from the collection.
C: Judging function
boolean isEmpty (): Determines whether the collection is empty.
Boolean contains (Object obj): Determines whether the specified element exists in the collection.
Boolean Containsall (Collection C): Determines whether an element in the specified collection exists in the collection.
D: Traversal function
Iterator Iterator (): It is used to get every element in the collection.
E: Length function
int size (): Gets the number of elements in the collection
F: Intersection function
Boolean Retainall (Collection C): Determines whether two sets have the same element.
G: Convert set to Array
object[] ToArray (): Turns the collection into an array.

Common methods in the list interface:

First of all we know that the list interface is a subclass of the collection interface, so it also has the above method, but in addition to the above method, the list interface in the typical ArrayList is the structure of the array, so it is also specific to some index operation method, as follows:

Unique features of List
A: Add Features
void Add (int index, Object obj): add element at specified position
B: Delete Feature
Object Remove (int index): Deletes the element according to the specified index and returns the deleted element.
C: Modify function
Object Set (int index, Object obj): Modifies the element at the specified index position to the specified value, returning the value before the modification.
D: Get Features
int indexOf (Object o): Returns the index of the first occurrence of the specified element in the collection
Object get (int index): Gets the element at the specified position
Listiterator listiterator (): List iterator
E: Interception function
List sublist (int fromIndex, int toindex): intercepts the collection.

Common methods in the set interface:

set interface are unordered and cannot be duplicated. The following are divided into HashSet and TreeSet.
hashset

guaranteed uniqueness depends on two methods: Hashcode () and Equals ().


same: proceed to Equals (), see return value


different: it is added to the collection.
treeset

The method that guarantees element uniqueness is based on whether the return value is 0.
two ways of guaranteeing sorting:

Iv. iterators (Iterator)

Iterators, as can be seen from the first Java Collection Class diagram relationship, the iterator iterator is placed on the top left side except for the middle collection.

1. Steps to use:

1. Gets the iterator object through the collection object.
2, through the iterator object judgment.
3. Get through an iterator object.

2. The principle of iterators

Because the data structure of various collections is different, the way of storage is different, so the way to take out is different. At this point in time, the judgment and acquisition functions are defined in an interface, and in the future, whenever a collection is traversed, the interface can be implemented internally. "Iterator Mode"

3.Collection stores strings and custom objects and iterates through iterators

New ArrayList ();  C.add ("Hello");  C.add ("World");  C.add ("Java");     = c.iterator ();    while (It.hasnext ())  {           = (String) It.next ();           System.out.println (s);  }

The 4.ListIterator iterator is a sub-interface of the iterator

So the list is traversed in a total of three kinds 1, iterator iterator 2, Listiterator iterator 3, General For+get ()

Five, map< key-value pair interface >

A map is a collection of key-value pairs. Its elements are all composed of a key and a value. The map key (key) is unique and the value can be duplicated.

Common methods in Map interfaces:

A: Add features
v put (K key, V value): add element When key does not exist in the collection, replace element when key exists
B: Judging function
boolean containskey (Object key): Determines whether the specified key exists in the collection
Boolean containsvalue (Object value): Determines whether the specified value exists in the collection
Boolean IsEmpty (): Determines whether the collection is empty
C: Delete function
Void Clear (): Clears all key-value pairs of data
D: Get features
object get (Object key): Gets the value according to the key
set<k> Keyset (): Collection of all keys
collection<v>values (): A collection of all values
set<map.entry<k,v>> entryset (): Returns the Set view of the mappings contained in this map
E: Length function
Int size ()

The map includes HashMap, Hashtable, and TreeMap. Among them, Hashtable has been basically replaced by HashMap, the new code is basically not in use Hashtable
(Note: HashMap supports NULL, Hashtable does not support null)

How map is traversed:

1. Key Search Value:

 Public Static voidMain (string[] args) {Map<String,Integer> map =NewHashmap<string,integer>(); Map.put ("Er Yang", 23); Map.put ("Two Zheng", 24); Map.put ("Two Lights", 25); Set<String> Keys=map.keyset ();//put the key together and deposit it into the set set.           for(String Key:keys) {//iterate through the key collection to get each key. Enhanced forInteger Value=map.get (key);//Get the key to find the value get (Object key)SYSTEM.OUT.PRINTLN (key+ "* * *" +value); }  }  

2. Key-value pairs

 Public Static voidMain (string[] args) {Map<String,Integer> map =NewHashmap<string,integer>(); Map.put ("Er Yang", 23); Map.put ("Two Zheng", 24); Map.put ("Two Lights", 25); Set<String> Keys=map.keyset ();//put the key together and deposit it into the set set.           for(String Key:keys) {//iterate through the key collection to get each key. Enhanced forInteger Value=map.get (key);//Get the key to find the value get (Object key)SYSTEM.OUT.PRINTLN (key+ "* * *" +value); }  }  

Through the above study, let us have a general understanding of the class diagram of the set, we will go into our most commonly used such as: ArrayList HashMap and other knowledge points of deep learning.

Java Nine: Collection system

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.