HashMap usage summary, hashmap usage

Source: Internet
Author: User

HashMap usage summary, hashmap usage

The HashMap format in Java is <Key, Value>

Compared with hashtable, It is unsynchronized, And the null value is allowed.


Common method:

void clear()Removes all of the mappings from this map.

boolean containsKey(Object key)ReturnsTrueIf this map contains a mapping for the specified key.
boolean containsValue(Object value)ReturnsTrueIf this map maps one or more keys to the specified value.
V get(Object key)Returns the value to which the specified key is mapped, ornullIf this map contains no mapping for the key.
boolean isEmpty()ReturnsTr
V put(K key, V value)Associates the specified value with the specified key in this map.
V remove(Object key)Removes the mapping for the specified key from this map if present.
boolean remove(Object key, Object value)Removes the entry for the specified key only if it is currently mapped to the specified value.

V replace(K key, V value)Replaces the entry for the specified key only if it is currently mapped to some value.
boolean replace(K key, V oldValue, V newValue)Replaces the entry for the specified key only if currently mapped to the specified value.
int size()Returns the number of key-value mappings in this map.
Set<Map.Entry<K,V>> entrySet()ReturnsSetView of the mappings contained in this map.

Note the types of input and output. For example, the type of value is directly returned by the get method.

About Map. entry:

Modifier and Type Method and Description
boolean equals(Object o)Compares the specified object with this entry for duplicate ity.
K getKey()Returns the key corresponding to this entry.
V getValue()Returns the value corresponding to this entry.
int hashCode()Returns the hash code value for this map entry.
V setValue(V value)Replaces the value corresponding to this entry with the specified value (optional operation ).
About set:

Iterator<E> iterator()Returns an iterator over the elements in this set.

About Iterator:

boolean hasNext()ReturnstrueIf the iteration has more elements.
E next()Returns the next element in the iteration.
void remove()Removes from the underlying collection the last element returned by this iterator (optional operation ).

For example, the following is a short piece of code written at a time. The type of the returned value is object or key, and the type of the value itself is very confusing, so there are a lot of redundant cast:

Iterator iterator = map.entrySet().iterator();       while (iterator.hasNext()) {      Map.Entry pairs = (Map.Entry)iterator.next();      if (Integer.parseInt(pairs.getValue().toString()) % 2 == 1) {        iterator.remove();        return pairs.getKey().toString();      }    }

HashMap I used <String, Integer>

Among them, pairs. getValue () is already required and does not need to be used.
Integer.parseInt(pairs.getValue().toString())
So complicated to express ~ Similarly, pairs. getKey () does not require pairs. getKey (). toString ();


Hashmap usage

Your class is your own class, and you must have your own get method.
It seems to be a hash method. Instead of your own method.
That is, the hash. get () method. It is wrong if you write it like that.

Basic HashMap usage

Import javax. swing .*;
Import java. awt .*;
Import java. awt. event .*;
Import java. util .*;
Import com. bruceeckel. swing .*;

Public class TrackEvent extends JApplet {
Private HashMap h = new HashMap ();
Private String [] event = {
"FocusGained", "focusLost", "keyPressed ",
"KeyReleased", "keyTyped", "mouseClicked ",
"MouseEntered", "mouseExited", "mousePressed ",
"MouseReleased", "mouseDragged", "mouseMoved"
};
Private MyButton
B1 = new MyButton (Color. BLUE, "test1 "),
B2 = new MyButton (Color. RED, "test2 ");
Class MyButton extends JButton {
Void report (String field, String msg ){
(JTextField) h. get (field). setText (msg );
}
FocusListener fl = new FocusListener (){
Public void focusGained (FocusEvent e ){
Report ("focusGained", e. paramString ());
}
Public void focusLost (FocusEvent e ){
Report ("focusLost", e. paramString ());
}
};
KeyListener kl = new KeyListener (){
Public void keyPressed (KeyEvent e ){
Report ("keyPressed", e. paramString ());
}
Public void keyReleased (KeyEvent e ){
Report ("keyReleased", e. paramString ());
}
Public void keyTyped (KeyEvent e ){
Report ("keyTyped", e. paramString ());
}
};
MouseListener ml = new MouseListener (){
Publ ...... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.