List Set Map differences and relationships in the Java set

Source: Internet
Author: User
Tags set set

Features of 1list:list elements!    

How to traverse the list collection: while-iterator traversal for-iterator traversal foreach-Iteration traversal for-get traversal (only list can be, Set,map can not)

Orderly and repeatable, because the underlying implementation arrays and linked lists are subscript to indicate uniqueness, in what order, to take out or in what order.

ArrayList: The underlying is an indexed array, sorted by index array subscript

LinkedList: The bottom layer is a list of sequential maintenance sequence.

Dive into the list collection

ArrayList (): The bottom is an indexed array, index subscript ArrayList collection The underlying default initialization capacity is 10, the expansion capacity is the original capacity of 1.5 times times the vector set of the bottom default is 10, the enlarged capacity is the original twice times the array Listshi vector upgrade, do not use the vector for the array code optimization: Try not to expand the operation, set up the collection when the initialization of the specified capacity list can have (iterator-while loop, for-iterator loop, for-get follow Ring (only list can))

Here is a list of examples for your reference:

Import java.util.*;

public class list01{

public static void Main (string[] args) {

List li = new ArrayList ();

Li.add (100);

Li.add (10);

Li.add (110);

Li.add (100);

for (Iterator it = Li.iterator (); It.hasnext ();) {

System.out.println (It.next ());

}

}

}

2, Set: No order, no repetition: the underlying is an indexed array. Part of the HashMap key hashset:hashtable

SortedSet: Red and black trees are unordered, but the elements can be automatically sorted by the size of the elements.

SortedSet why the collection storage element can be automatically sorted because the stored element implements the comparable interface;

The TreeSet collection that Sun writes will automatically call the CompareTo method when adding elements to complete the

Set set: HashSet 1 HashSet is an implementation of HashMap, essentially a hashmap, and the HASHMAP data structure is a hash table

2 Hash tables (hash table): Also known as the hash list, its bottom layer is a hash function combination of one: array and one-way list; Each unidirectional linked list has a unique hash value, which is also the subscript of the array, and the hash value of each individual list node is equal; The essence of the hash value is to call the value of the key Hashcode () method 3 to the hash table to add elements of the process 1) First call the stored key, call the Hashcode method, this calculates the hash value, that is, the array subscript, and then even call the Equals () method , and an array of all subscripts to be compared, if not, in the new bucket (that is, subscript) to create a linked list (if not present, as a new subscript, add an element as the head element of this new list) if there is, continue to call equal S () method, which compares the key and value to the value in the linked list, and if it returns true, it means that the key value is already in the list, discards the add, returns False if not, and then puts the value together in the current hash value list

4 HashSet Essence is the key part of HashMap, the two are exactly the same

5 HashSet and HashMap initialization capacity is 16, the default load factor is 0.75

Here is an example:

Import java.util.*;

public class collection_08_set_01{

public static void Main (string[] args) {

Set s = new HashSet ();

S.add (1);//Auto-boxing: Basic data type automatically transforms to relative reference data type//auto-unpacking: Refers to objects with corresponding basic data types, automatically transforms to their basic type

S.add (1);

S.add (3);

S.add (4);

Iterator it = S.iterator ();

The return value of while (It.hasnext ()) {//hasnext () is Boolean (Boolean)

System.out.println (It.next ());

}

for (Iterator its = S.iterator (); Its.hasnext ();) {

System.out.println (Its.next ());

}

}

}

3 Map:map non-sequential repeatable

Hash (hash): A method of generating a unique identifier for an object based on its attributes

Oject in Hashcode () is an application of hashing that takes an object's memory address as a unique identifier for an object by operation

HASHMAP: The bottom line is the array + one-way list: We put the subscript is a string of arrays called associative array

The subscript of an array is a string

The map collection is not the same as the collection, but the operation is the same, but the method of operation is slightly different with the object put (object key, object value); Adds a key-value pair to the collection delete void Clear (); Clear map Change data type conversion check int size (); Gets the number of key-value pairs in the map, Boolean isEmpty ();//Determines whether it is an empty object get (object key);//Gets the value of the corresponding object by key Collection values (); The collection of all value objects in the Map collection Boolean:containskey (object key);//Determine if there is such a key key in the set Boolean Containsvalue (object value)//judgment set Whether there is such a value set KeySet ();//Gets all the keys in the collection, returns the set entryset;//in the collection, returns the mappings contained in this map, and sets the set method to return
Traverse hashmap Default Initial value 16, default load factor is 0.75

Here is an example:

Import java.util.*;

public class collection_06_map_01{

public static void Main (string[] args) {

1 Creating a collection of HashMap

Map PS = new HashMap ();

2 Add elements inside

Ps.put ("+", "Jack");

Ps.put ("1011", "Jobs");

Ps.put ("1002", "Cook");

Ps.put (1003, "Rose");//This will call the integer Hashcode () method: 1003.hashCode (). Hashcode (1003), return-->1003

Ps.put ("+", "Sun");

Query the elements inside

System.out.println (Ps.size ());//Here is the return value 4 refers to who, there are 4 one by one mappings, a one by one mapping relationship in memory is a linked list node

System.out.println (Ps.containskey ("1004"));//check is the subscript of the array, ContainsKey first to call the object's Hashcode () to generate a hash code, and then call the object itself equals (), Go to the array subscript to compare, and then in the call Equals (), go to the linked list of the original value of the key

System.out.println (Ps.containsvalue ("Sun")),//1 "<----->" Sun ", each mapping," sun "found" 1000 "//2" ". Hashcode (). Equals () go to the array to compare the subscript (also called the bucket), find the bucket, and then//3 "" ". Equals () goes to the list to compare//4" Sun ". Equals () to compare their values//do not need to compare/ /Have a word: The purpose of our hash function is not to uniqueness, not to produce a unique hash code, different objects can produce the same hash code, because the same hash is not the same for our one by one mapping relationship has no effect,                 There is no effect on the two values in the relationship. When we go to query the value, we equals three times, to be hash code, the second is the original value of key, the third step is the value of the original value//SO: A stealth relationship, the key is equal, the value is equal, then they want to wait System.out.println (Ps.get ("1011"));

Change

Traversal: Iterating through the collection with key, while getting the key and value of the element

System.out.println ("---------");

Collection values = Ps.values ();

Iterator it = Values.iterator ();

while (It.hasnext ()) {

System.out.println (It.next ());

}

System.out.println ("---------");

By deleting

System.out.println (Ps.remove ("1002")); Data type conversion//Map conversion to set set

Set es = Ps.entryset ();

Iterator it3 = Es.iterator ();

while (It3.hasnext ()) {

System.out.println (It3.next ());

}

}

}

Summary: List and set have a common parent class their usage is the same as the only one is not too set can not have the same element in list
List and set are very widely used list can completely replace the array to use
Map is a separate collection it uses key-value pairs to store data keys that cannot have duplicate values that can be used
The map is not as large as the two sets above, but in the servlet and JSP, the map is the absolute weight of the page. The values are all based on map.

Here are the various relationships that the interface corresponds to

List Set Map differences and relationships in the Java set

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.