[TOC]

A collection of

Collection

vector

Thread synchronization, which is inefficient, has been replaced by arrayLists

The Set interface

  • Inherited the collection
  • There are no indexed methods, and you can’t iterate with normal for loops
  • Duplicate elements are not allowed to be stored

Set interface implementation class :HashSet

Supported by a hash table, which is actually a HashMap instance

  • This implementation is out of sync
  • Is an unordered collection in which elements may be stored and retrieved in different order
  • The bottom layer is a hash table structure (very fast queries)
  • Use iterators to traverse

set.iterator().var

while(it.hasNext()){


}
Copy the code

Use enhanced for traversal

set.for

for(Interger integer:set){

}

Hash table

Hash value

  • Hash: a decimal integer randomly assigned by the system (the address value of the object, the logical address, is the simulated address, not the physical address of the actual data store)

public native int hasCode()

Native: indicates that this method invokes methods of the local operating system

The ToString method is the default

Is the hexadecimal representation of a hash value

The string class overrides the hashCode method

Some coincidence weight is the same hash as the call

Hash table

Before JDk1.8: Hash: array + linked list

After JDk1.8: Hash table = array + red-black tree (faster query)

The set collection is called when the AD method is called

The add method calls the element’s hashCode method and equal method to determine whether the element is duplicated

The add method first calls the Hascode method to look for elements in the collection that have the same hash value

If a hash conflict is found, call equals to compare the elements with the same hash value

If the hashes of two elements are the same, equals returns a tue that considers them the same and does not store them in the collection

Otherwise join the set

–> If the hash is the same,equals is different

Hang it in the same place in the array

The stored elements must override Hascode and Equals to ensure that the stored objects are unique

LinkedHashSet

Inherits the HashSet collection

The bottom layer is a hash table + linked list

Add a linked list (keep track of the order of the elements to ensure that they are in order)

Variable parameter

New features after JDK1.5

Method name (data type… Variable name)

The bottom layer is an array, depending on the number of parameters passed, create a different length of the array can be 0 (not passed),1 or more

  • The argument list of a method can have only one variable argument
  • And mutable arguments must be unloaded from the end of the argument list
  • Ultimate: Accept any type, any number of arguments

The collections utility class

  • The default rule of sort is ascending sort
  • Custom classes need to extend the Comparbale interface
  • Override the compareTo method

Comparable with the Comparator interface

  • Comparable: Compare yourself (this) to someone else (parameters)

, need to implement their own comaprable interface, rewrite the comparison of rules compareTo method

  • Comparator: Find a third party judge and compare them

Public int compare(o1,o2)

Return O1-O2: sort in ascending order

  • Create a new inner class comparator

Map collections

  • Map collection: a two-column collection, storing one pair of yuan at a time

  • Collection is a single-column collection

  • Cannot contain duplicate keys, each key corresponds to a value, and the value can be repeated

Common implementation class :HashMap

A HashMapl set contains an unordered set of elements, and the order in which the elements are stored and retrieved may not be in the same order

LinkedHashMap

Subclass Hashmap (hash table + linked list)

Common Map interface methods

  • Put (): returns a value. If a key already exists, replace the value and return the replaced value
  • Remove: return a value
  • contains Key
  • get()
  • # # collection

Collection

vector

Thread synchronization, which is inefficient, has been replaced by arrayLists

The Set interface

  • Inherited the collection
  • There are no indexed methods, and you can’t iterate with normal for loops
  • Duplicate elements are not allowed to be stored

Set interface implementation class :HashSet

Supported by a hash table, which is actually a HashMap instance

  • This implementation is out of sync
  • Is an unordered collection in which elements may be stored and retrieved in different order
  • The bottom layer is a hash table structure (very fast queries)
  • Use iterators to traverse

set.iterator().var

while(it.hasNext()){
    

}
Copy the code

Use enhanced for traversal

set.for

for(Interger integer:set){

}

Hash table

Hash value

  • Hash: a decimal integer randomly assigned by the system (the address value of the object, the logical address, is the simulated address, not the physical address of the actual data store)

public native int hasCode()

Native: indicates that this method invokes methods of the local operating system

The ToString method is the default

Is the hexadecimal representation of a hash value

The string class overrides the hashCode method

Some coincidence weight is the same hash as the call

Hash table

Before JDk1.8: Hash: array + linked list

After JDk1.8: Hash table = array + red-black tree (faster query)

The set collection is called when the AD method is called

The add method calls the element’s hashCode method and equal method to determine whether the element is duplicated

The add method first calls the Hascode method to look for elements in the collection that have the same hash value

If a hash conflict is found, call equals to compare the elements with the same hash value

If the hashes of two elements are the same, equals returns a tue that considers them the same and does not store them in the collection

Otherwise join the set

–> If the hash is the same,equals is different

Hang it in the same place in the array

The stored elements must override Hascode and Equals to ensure that the stored objects are unique

LinkedHashSet

Inherits the HashSet collection

The bottom layer is a hash table + linked list

Add a linked list (keep track of the order of the elements to ensure that they are in order)

Variable parameter

New features after JDK1.5

Method name (data type… Variable name)

The bottom layer is an array, depending on the number of parameters passed, create a different length of the array can be 0 (not passed),1 or more

  • The argument list of a method can have only one variable argument
  • And mutable arguments must be unloaded from the end of the argument list
  • Ultimate: Accept any type, any number of arguments

The collections utility class

  • The default rule of sort is ascending sort
  • Custom classes need to extend the Comparbale interface
  • Override the compareTo method

Comparable with the Comparator interface

  • Comparable: Compare yourself (this) to someone else (parameters)

, need to implement their own comaprable interface, rewrite the comparison of rules compareTo method

  • Comparator: Find a third party judge and compare them

Public int compare(o1,o2)

Return O1-O2: sort in ascending order

  • Create a new inner class comparator

Map collections

  • Map collection: a two-column collection, storing one pair of yuan at a time

  • Collection is a single-column collection

  • Cannot contain duplicate keys, each key corresponds to a value, and the value can be repeated

Common implementation class :HashMap

A HashMapl set contains an unordered set of elements, and the order in which the elements are stored and retrieved may not be in the same order

LinkedHashMap

Subclass Hashmap (hash table + linked list)

Common Map interface methods

  • Put (): returns a value. If a key already exists, replace the value and return the replaced value
  • Remove: return a value
  • contains Key
  • get()