HashMap and HashSet classes

HashMap class

HashMap is a collection that can manage a collection of elements that are a set of keys and values.

The following is output as a set of element name and atomic number.


import java.util.HashMap; //import

public class Sample {
  public static void main(String[] args){
    //The key is a string and the value is an integer
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    String atom[] = {"hydrogen", "helium", "lithium", "beryllium"};
    map.put(atom[0], 1); // .put()Store data in
    map.put(atom[1], 2);
    map.put(atom[2], 3);
    map.put(atom[3], 4);
    System.out.println("---Atomic number---");
    for(String s:atom){
      System.out.println(s + "Is" + map.get(s) + "Turn"); // .get()Get the value with
    }
  }
}
---Atomic number---
Hydrogen is number one
Helium is number 2
Lithium is number 3
Beryllium is number 4

Main methods

Method function
get() Returns the element of the specified key.
put() Associate the key with the element.
remove() Deletes the element with the specified key.
clear() Erases all keys and elements.
containsKey() Returns true if the specified key value exists, false otherwise.
isEmpty() Returns true if there are no elements.
size() Returns the number of elements.

HashSet class

HashSet is a collection that can store data without duplication.

The following stores APPLE character by character and outputs it.

  
import java.util.HashSet;

public class Sample {
  public static void main(String[] args){
    HashSet<String> hs = new HashSet<String>(); //Generated as String type
    hs.add("A"); //.add()Store data in
    hs.add("P");
    hs.add("P");
    hs.add("L");
    hs.add("E");
    for(String s:hs){ //Display all with a special for statement
        System.out.println(s);
    }
  }
}

Two Ps are stored, and since duplication has occurred, it is output only once.

P
A
E
L

Main methods

Method function
add() Add an element.
remove() Deletes the specified element.
clear() Erase all elements.
contains Returns true if the specified element exists, false otherwise.
isEmpty() Returns true if there are no elements.
size() Returns the number of elements.

Recommended Posts

HashMap and HashSet classes
Classes and instances
[Ruby] Classes and instances
About classes and instances
Ruby classes and instances
List and happy classes
java (classes and instances)
[Java] Generics classes and generics methods
About classes and instances (evolution)
Consideration about classes and instances
[Ruby] Singular methods and singular classes
About Ruby classes and instances
Ruby methods and classes (basic)
Creating Ruby classes and instances
Java abstract methods and classes
[Ruby] Singular methods and singular classes
[Java] Difference between Hashmap and HashTable
How to call classes and methods
Writing code with classes and instances
Organize classes, instances, and instance variables
Java generics (defines classes and methods)
Classes and instances Java for beginners
Mock and test Autowired classes (MockitoExtension, initMocks)
Java programming (classes and instances, main methods)
Memorandum (Ruby: Basic Grammar: Classes and Instances)
JAVA learning history abstract classes and methods
Comparison of JavaScript objects and Ruby classes
[Details] Let's master abstract classes and interfaces! !!
Write code using Ruby classes and instances
A simple and convenient method for HashMap
[Java ~ Classes and External Libraries ~] Study Memo (6)
HashMap # putAll () behaves differently between Java 7 and Java 8