knj
Knj
knj
전체 방문자
오늘
어제
  • 분류 전체보기 (55) N
    • 정보처리기사 (2)
    • Spring (12)
    • JS (2)
    • java (6)
    • 컴퓨터 시스템 (9)
      • Chapter 1 컴퓨터 시스템 기초 (8)
      • Chapter 2 정보의 표현과 처리 (1)
    • 알고리즘 (4)
    • CODE (5)
    • 컴퓨터 네트워킹 (5)
      • Chapter 1 - 컴퓨터 네트워크와 인터넷 (5)
    • 혼자공부 하는 컴퓨터구조 운영체제 (5)
      • Chapter1 (2)
      • Chapter2 (1)
      • Chapter3 (1)
    • 컴퓨터 네트워크 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
knj

Knj

java

[java/자바] 자료구조 Map

2022. 7. 4. 23:44

공부한것

Key, Value 형태로 Map에 저장.

저장한 키의 이름으로 객체 생성 (Map에 저장할때 Value로 넣어준 객체).

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

class MyDate {
    int year;
    int month;
    int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }
}

public class TestMap {
    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("date", new MyDate(2020,1,1));
        map.put("date1", new MyDate(2021, 2,2));
        map.put("date2", new MyDate(2022, 3,3));

        MyDate myDate = (MyDate) map.get("date");
        MyDate myDate1 = (MyDate) map.get("date1");
        MyDate myDate2 = (MyDate) map.get("date2");

        System.out.println(myDate.getYear() + " 년 " + myDate.getMonth() + " 월 " + myDate.getDay() + " 일 ");
        System.out.println(myDate1.getYear() + " 년 " + myDate1.getMonth() + " 월 " + myDate1.getDay() + " 일 ");
        System.out.println(myDate2.getYear() + " 년 " + myDate2.getMonth() + " 월 " + myDate2.getDay() + " 일 ");
        
        	실행 결과
        	2020 년 1 월 1 일 
		2021 년 2 월 2 일 
		2022 년 3 월 3 일 
    }
}

'java' 카테고리의 다른 글

Lambda  (0) 2025.05.04
[java/자바] 디자인 패턴 (전략 패턴)  (0) 2022.07.04
[java/자바] 숫자 맞추기 게임 구현  (0) 2022.06.11
[java/자바]물건 구매 서비스 구현  (0) 2022.06.01
[java/자바] 객체의 응집도  (0) 2022.06.01
    'java' 카테고리의 다른 글
    • Lambda
    • [java/자바] 디자인 패턴 (전략 패턴)
    • [java/자바] 숫자 맞추기 게임 구현
    • [java/자바]물건 구매 서비스 구현
    knj
    knj

    티스토리툴바