Java list collection and hash table

Source: Internet
Author: User

List collection and set collection, first look at the list collection.

Features of the list collection storage elements:

1. Ordered (the elements in the list set have subscript): What is the deposit in, what is it?

2. Repeatable

Can be combined with the following simple code to take a look.

  import java.util.*; 
public  class  listtest01{
  public  static  void  main ( string[] args) {
   list l= new arraylist ();
   l. Add ();
   l. Add ();
   l. Add ();
   l. Add ();
   iterator it=l.iterator ();
    while (It.hasnext ()) {
     system. Out.println (It.next ());
   }
 }
} /span>

After compiling the run-time output:

100
32
32
90

Since the ArrayList collection is an array at the bottom, the array has subscript, so the ArrayList collection has many of its own features. The default initialization capacity of the ArrayList collection is 10, and the capacity after enlargement is 1.5 times times the original capacity.

Import java.util.*;
PublicClasslisttest02{
PublicStatic void  main ( string[] args) {
   list l= new arraylist ();
   l. Add ();
   l. Add ();
   l. Add ();
   
    //Insert element
   l at position subscript 1. Add ( 1, 555);
    //Get first element
   system. Out.println (L. get ( 0));
   system. out.println ( "-----------");
    for ( int i= 0;i<l.size (); i++) {
     object element=l. get (i);
     system. out.println (Element);
  &NBSP;}
 }
} /span>

After compiling the run-time output:

12
-----------
12
555
23
43

In the above code, the list L=new ArrayList (), the new List object L, and the add element using the Add () method, L.add (A, A, a, b) can add an element at the specified subscript A, and finally use the For loop to traverse the output list The element in the L. Note that the content in the For loop is a unique traversal method for the list.

The implementation class of set mainly has HashSet and TreeSet, before speaking hashset to see what is a hash table. A hash table (hash table, also known as a hash list) is a data structure that is accessed directly from a key value. That is, it accesses records by mapping key code values to a location in the table to speed up lookups. This mapping function is called a hash function, and the array that holds the record is called the hash table.

Storage location for records =f (keywords)

The corresponding relationship f is called the hash function, also known as hash (hash function), using hashing technology to store records in a contiguous storage space, this contiguous storage space is called a hash table or hash table.

The structure of the hash table is shown in the following diagram:

Hash table is a combination of arrays and unidirectional lists, we all know that the characteristics of the array is easy to address, insert and delete difficult, while the list of features is difficult to address, insert and delete easy, the hash table combines the two, get an easy way to address, insert and delete data structure easily. The hash table is essentially an array, but each element in the array is a one-way list, similar to a dictionary in the real world. Each node in a one-way list contains four components, Object key; Object value; final int hash; Entry Next; Entry next points to the next node of the node. final int hash; is a hash value, which is the value obtained by calling the Hashcode method through key, and the value obtained by the hash algorithm, and the hash value of each node in the unidirectional list is the same, which represents the subscript of the array. What do you do if you want to find an element in an array? For example, to find an O object, O object to call the Hashcode method, get the hash value, the hash value represents the subscript of the array, and the subscript of the array will know the O object in which one-way linked list. There is a method in HashMap, Object Get[object key], to get the value of object value. So how do you add elements to the hash table? There is a method for adding elements in HashMap, void Put[object key,object value]; First call the Hashcode method of Object key, get a hash value, if the hash value already exists in the array, you can locate the unidirectional linked list, Through key traversal of the one-way list, if the key is the same as the existing key (through the Equals method), because key unordered is not duplicated, so do not add, and vice versa in the one-way linked list; If the hash value does not exist in the array, a new one-way list is added to the location after the array. Therefore, the hash table's deletion efficiency and query efficiency are very high. From the above description we know that object key is very important and cannot be duplicated.

Search the public number "programmer Koala", welcome attention!

Java list collection and hash table

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.