Java Learning: Structure Set interface HashSet collection of stored data (hash table)

Set Interface

java.util.Set the interface extends Collection Interface

Set interface features:

  1. Storage is not repeated elements
  2. No index, no indexed methods , can not be used for normal loop iterates
java.util.HashSet collection implements Set Interface

HashSet Features:

  1. Storage is not repeated elements
  2. No index, no band index method can not be used for normal loop iterates
  3. Is an unordered collection of elements and the order of storage elements may be inconsistent removed
  4. The bottom layer is a hash table structure (query speed is very fast)
SET <Integer> = new new SET HashSet <> (); 
// add elements to the collection method using add 
set.add (. 1 ); 
set.add (. 3 ); 
set.add (2 );  set.add (. 1 ) ;  // set using iterates over the set of  the iterator <Integer> = IT set.iterator (); the while (it.hasNext () = {n-the iterator it.next (); System.out.println (n-); //. 1 2,3 disorder not repeated}

 

HashSet set of structures for storing data (hash table)

Hash values: is a decimal integer, is given by the random system (that is, the address value of an object, is a logical address, the address is simulated to obtain physical address data is not actually stored)
a method in the class Object, can Gets the object hash value

int hashCode () Returns the hash code value for this object.

HashCode method Source:

Native int public the hashCode (); 
Native: representing the method call is a method of operating a local

 


Hash table

A hash table: hashSet storing a set of data structures
before jdk1.8 versions: a hash table array + = list
jdk1.8 later versions:

  • = + Chain hash table array;
  • + = Array hash table red-black tree (increase the speed of queries)

Features hash table: speed

Hash value is stored into the data collection, calculation of the first element

  • abc: 96,354 storage locations in the array
  • Powerhouse - Call: Different 1,179,395 two elements, but the same hash value hash collision


Array of structures: the grouping elements (elements of the same hash value is set)

List / red-black tree structure: the elements of the same hash value connected together (how the linked list is longer than 8, it will convert the chain bit red-black trees (increase the speed of the query))

 

Set a collection of storage elements of the principle of non-repetition

Set a collection of storage elements that do not duplicate elements of the premise:

Prerequisite: storage elements must override hashCode method and the equals method

// create the collection object HashSet 
HashSet <String> set = new HashSet <> (); // hash table: Array + list / black tree 
String S1 = new new String ( "ABC" ); 
String S2 = new new String ( " ABC " ); 

set.add (S1); 
set.add (S2); 
set.add (" powerhouse " ); 
set.add (" call " );  set.add (" ABC " );  System.out.println (set); / [heavily, call, ABC]

 

The reason:
the Set collection when you call the add method, add elements method calls the hashCode method and the equals method to determine whether the element repeat


HashSet custom type storage element

Set reasons given set of elements:
a storage element (String, Integer, ... Student, Person ...), hashCode method and the equals method must be rewritten

LinkedHashSet collection

java.util.LinkedHashSet collection extends HashSet collection

 

LinkedHashSet set of features:
the bottom layer is a hash table ( array + list / black tree list +: more than one chain (sequential recording storage elements), hold the elements of the ordered

HashSet <String> set = new HashSet <> (); // [] disorder, allowed to repeat 
LinkedHashSet <String> set = new LinkedHashSet <> (); [] // ordered, allowed to repeat

 


variable parameter

Variable parameters: it is a new feature that appears after JDK1.5

Use the premise:

  • When the process parameter list data type has been determined, but not the number of parameters determined fixed, variable parameters can be used.

Using the format: using the definition of the method

  • Modifier return type method name (variable name data type ...)} {

The principle of variable parameters:

  • Underlying variable parameter is an array, depending on the number of parameters passed, it creates an array of different lengths, to store these parameters
  • The number of parameters passed, and may be 0 (no transmission), a plurality of 1,2, ...


Variable parameters Note:

  1. A method parameter list, only one variable parameter
  2. If there are multiple parameters of the method, the variable parameters must be written at the end of the parameter list
Method static void public (B String, Double C, D int, int ... A) {} 

// special variable parameters (ultimate) written 
public static void Method (Object obj ...)} { 

the Collections collection tools the method 
java.util.Collections is a collection of tools for the collection operation. Part follows: 
    public static <T> void Sort (List <T> List, Comparator <Super T?> ): The set of elements according to an specified rules. 
    
    Comparator and Comparable differences 
        Comparable: own (the this ) and others (parameters) comparison, we need to implement the Comparable interface, rewrite the rules of comparison compareTo method  Comparator: find the equivalent of a third-party referee compare two 
         collation of Comparator:  O1 -o2: ascending

 

Guess you like

Origin www.cnblogs.com/cainiao-chuanqi/p/11223170.html