0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Java】マップにキーとオブジェクトを使う

Posted at

はじめに

Javaプログラミング言語では、マップ(Map)を使用してキーと値のペアを関連付けることができます。この記事では、Javaにおけるマップの値にオブジェクトを付与する方法について詳しく解説します。

マップの作成

Javaでは、HashMap、TreeMap、LinkedHashMapなどのマップ実装クラスを使用してマップを作成します。以下はHashMapの例です

HashMapの宣言
Map<KeyType, ValueType> map = new HashMap<KeyType, ValueType>();

キーと値を埋めるデータ型

KeyTypeとValueTypeは、具体的なデータ型を指す仮の用語です。これらは実際のコードで使用するデータ型に置き換える必要があります。以下のキーワードは一般的なデータ型の例を示します。

  • Integer: キーが整数値の場合
  • String: キーが文字列の場合
  • 自作のクラス: カスタムクラスをキーとして使用する場合
  • など

値を追加する

マップに値を追加するには、putメソッドを使用します。
putメソッドはキーと値のペアを指定し、マップに関連付けます。

map.put(key, value);

マップにオブジェクトを追加するためには実際の値を格納するクラスと、クラスを呼び込むためのクラスそれぞれを作成する必要があります。

StudentManagementSystem.java
import java.util.HashMap;
import java.util.Map;

public class StudentManagementSystem {

    private Map<Integer, Student> students = new HashMap<Integer, Student>();

    public StudentManagementSystem() {
        // オブジェクトの生成とマッピングを行う
        // stucentsというマップに整数値とクラスを追加する
        // クラスはインスタンス化しなければエラーが出るのでnewを付与する
        // Studentクラスは引数が必要なので名前,年齢,科目の順で引数を与える
        this.students.put(1, new Student("John Doe", 20, "Computer Science"));
        this.students.put(2, new Student("Jane Smith", 19, "Mathematics"));
        this.students.put(3, new Student("Mike Johnson", 21, "Physics"));
    }

    public void displayStudentDetails() {
        // マッピングの活用
        this.students.forEach((id, student) -> {
            System.out.println("ID: " + id);
            System.out.println("Name: " + student.getName());
            System.out.println("Age: " + student.getAge());
            System.out.println("Major: " + student.getMajor());
            System.out.println("------------------------");
        });
    }

    public static void main(String[] args) {
        StudentManagementSystem system = new StudentManagementSystem();
        system.displayStudentDetails();
    }
}



Student.java
public class Student {
    private String name;
    private int age;
    private String major;

    public Student(String name, int age, String major) {
        this.name = name;
        this.age = age;
        this.major = major;
    }

    // 外部から値を読み込むためのゲッター

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getMajor() {
        return major;
    }


    // 外部から値を変更する場合に呼び出すセッター
    public void setName(String name) {
        this.name = name;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    public void setMajor(String major) {
        this.major = major;
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?