HashMap in selenium

Tram Ho

Java HashMap class implements Map interface using hastable (hash function), it inherits from AbstractMap class and implements Map interface

  • Key-Pair: A HashMap containing key-based values.
  • unique (unique): Only contain the key is unique, and can have duplicate values
  • Null value: can have 1 null key and many null values
  • Random order: remain unordered, so you should not access it by index. And the order may change over time.
  • Not Thread Safe (not thread safe): Java HashMap is not thread safe, for multi-threaded environment you should use ConcurrentHashMap class to replace HashMap
  • Constant Time: HashMap provides constant time for activities like get () and put () methods.
  • Capacity: The default initial capacity of HashMap is 16

Create a HashMap

Below is the constructor of HashMap

Constructors of HashMap

HashMap has 4 types of constructors, these constuctors are designed to provide developer options

  1. HashMap has no parameters: it is the default constructor created as an instance of HashMap with an initial capacity of 16 and a load factor of 0.75.

  1. HashMap with the size parameter: This constructor HashMap creates a HashMap instance with a specified initial size and with a default load factor of 0.75.

  1. HashMap with parameters of capacity and load factor: This constructor creates a HashMap instance with the capacity and load factor that has been determined from the beginning.

  1. HashMap with the parameter interface Map:

HashMap methods in java

  • boolean isEmpty ()
  • Object put (Object key, Object value)
  • int size ()
  • get (Object key)
  • void clear ()
  • boolean containsKey (Object key)
  • boolean containsValue (Object value)
  • Object clone ()
  • Set entrySet ()
  • Set keySet ()
  • Collection values ​​()

HashMap introduced methods in Java 8

  • computeIfAbsent ()
  • computeIfPresent
  • compute
  • forEach
  • getOrDefault
  • merge
  • putIfAbsent
  • remove
  • replace
  • boolean replace (K key, V oldValue, V newValue)
  • replaceAll

Program example using HashMap

boolean isEmpty ()

Check if the map is empty or not? There are no key and value pairs, after running in this function will return true or false?

Output of isEmpty ()

Object put (Object key, Object value)

Create a key with the corresponding value in HashMap, if the map contains the same key with different values, the old value will be replaced with the new value.

The output of put ()

get (Object key)

Returns a mapped value with the specified key and null if no map with any key

Output of get () method

int size ()

Returns the number of key-value pairs in HashMap

Output of size ()

remove (Object key)

Delete the key along with its value

output after deleting the element

values ​​()

values ​​() method returns all the values ​​in Map

Output of keySet () and values ​​()

Display elements included in HashMap.

To display the elements included in HashMap, we have the following:

Use the for loop for improvement.

output

Also, from Java 8 onwards we can get all entries in HashMap using forEach () as follows:

Get the entire key of HashMap.

To get the entire key of HashMap, Java provides us with the keySet () method. This method will return 1 Set including the keys included in HashMap:

output:

Get the entire value of HashMap.

To get the entire value of HashMap, Java provides us with the values ​​() method. This method will return a collection of values ​​included in HashMap:

output:

Refer to the article: https://chercher.tech/java/hashmap

Share the news now

Source : Viblo