In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.