Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the working principle and implementation method of ​ HashMap in Java

Shulou Source: shulou.com Published: 2022-06-02 03:34:14 09月29日 Update

Today, the editor will share with you the relevant knowledge about the working principle and implementation of HashMap in Java. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Working principle and implementation of HashMap

1. How to implement a Map

1.1 knowledge related to Map

1.1.1 Map.Entry interface

A class that implements the Map.Entry interface represents an entry (a key-value pair) in a Map. So a Map must have a class that implements the Map.Entry interface, and use this class to store the key-value pair in the Map.

1.1.2 public abstract Set entrySet () function

1) the entrySet () function returns a Set, and each element in the Set is an object of type Map.Entry. In the entrySet () function, all the key-value pair in Map should be encapsulated in Map.Entry and stored in Set.

2) when the Map is modified, the entrySet () function will be called. So changes to the Map will also result in changes to the Set.

3) when using the iterator of this Set, you cannot perform add and addAll operations.

1.2 implement a simple example of Map

Import Java.util.*

/ * * XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" / >

* the MPair class implements Map.Entry

, /

Class MPair

Implements Map.Entry, Comparable {

Object key, value; / / key and value are used to store key and value in Map, respectively.

MPair (Object k, Object v) {

Key = k

Value = v

}

/ / the following methods implement the methods in the Map.Entry interface

Public Object getKey () {return key;}

Public Object getValue () {return value;}

Public Object setValue (Object v) {

Object result = value

Value = v

Return result

}

/ / the following methods implement the methods in the Comparable interface

Public boolean equals (Object o) {

Return key.equals (MPair) o) .key)

}

Public int compareTo (Object rv) {

Return ((Comparable) key) .compareTo (MPair) rv) .key)

}

}

Class SlowMap extends AbstractMap {

Private ArrayList

Keys = new ArrayList ()

Values = new ArrayList ()

Public Object put (Object key, Object value) {

Object result = get (key)

If (! keys.contains (key)) {/ (1)

Keys.add (key)

Values.add (value)

}

Else

Values.set (keys.indexOf (key), value)

Return result

}

Public Object get (Object key) {

If (! keys.contains (key)) {

Return null

}

Else

Return values.get (keys.indexOf (key))

}

/ / encapsulate the key-value pair in Map with Mpair and store it in Set

Public Set entrySet () {

Set entries = new HashSet ()

Iterator

Ki = keys.iterator ()

Vi = values.iterator ()

While (ki.hasNext ())

Entries.add (new MPair (ki.next (), vi.next ()

Return entries

}

}

Public class ExplicitStatic {

Public static void main (String [] args) {

SlowMap m = new SlowMap ()

For (int iTunes 1; I

Tags: Function element object method interface encapsulation principle array knowledge work same article index iteration two point update generate memory content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Apple Huawei Shulou Tech Info Microsoft