
나라 이름과 인구를 입력하세요.(예: Korea 5000)
나라 이름, 인구 >> Korea 5000
나라 이름, 인구 >> USA 1000000
나라 이름, 인구 >> Swiss 2000
나라 이름, 인구 >> France 3000
나라 이름, 인구 >> 그만
인구 검색 >> France
France의 인구는 3000
인구 검색 >> 스위스
스위스 나라는 없습니다.
인구 검색 >> 그만
답
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class HashMapCountry2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int MAX_COUNTRY = 4;
Map<String, Integer> map = new HashMap<>();
map.put("Korea", 5000);
map.put("USA", 1000000);
map.put("Swiss", 2000);
map.put("France", 3000);
System.out.println("나라 이름과 인구를 입력하세요.
(예: Korea 5000)");
for(int i=0; i<MAX_COUNTRY; i++) {
System.out.println("나라 이름, 인구>> ");
String country = sc.next();
int popu = sc.nextInt();
map.put(country, popu);
}
System.out.println("인구 검색>> ");
while(true) {
String country = sc.next();
if(map.get(country) == null) {
System.out.println(country + " 나라는 없습니다.");
continue;
}
// == 쓰면 주소값 나오는 거 주의
if(country.equals("그만")) {
break;
}
System.out.println(map.get(country));
}
}
}
센세의 답 / 아직 비교를 안 해봄...
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/*
1.아래를 작성 하시오.프로그래밍
나라 이름과 인구를 입력하세요.(예: Korea 5000)
나라 이름, 인구 >> Korea 5000
나라 이름, 인구 >> USA 1000000
나라 이름, 인구 >> Swiss 2000
나라 이름, 인구 >> France 3000
나라 이름, 인구 >> 그만
인구 검색 >> France
France의 인구는 3000
인구 검색 >> 스위스
스위스 나라는 없습니다.
인구 검색 >> 그만
*/
public class HashScannerCountrySearch {
public static void main(String[] args) {
Map<String,Integer> countryMap = new HashMap<>();
System.out.println("나라 이름과 인구를 입력하세요.(예: Korea 5000)");
//출력
while(true) {
try {
Scanner sc = new Scanner(System.in);
System.out.print("나라 이름, 인구 >>");
String county = sc.next();
if(county.equals("그만"))
break;
int popu = sc.nextInt();
countryMap.put(county, popu);
} catch (Exception e) {
e.printStackTrace();
System.out.println("잘못된 입력입니다.");
}
}
while(true) {
try {
Scanner sc = new Scanner(System.in);
System.out.print("인구 검색 >>");
String county = sc.next();
if(county.equals("그만"))
break;
if(countryMap.containsKey(county)) {
System.out.println(county + " 의 인구수는" + countryMap.get(county) + "명 입니다.");
}else {
System.out.println("[" + county + "] 나라는 없습니다.");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("잘못된 입력입니다.");
}
}
}
}

📌 특징

📌 특징
HashMap은 Map 인터페이스를 구현한 대표적인 Map 컬렉션이다.
값은 중복 저장될 수 있지만 키는 중복 저장될 수 없다.
HashMap은 많은 양의 데이터를 검색하는 데 뛰어나다.
Map에 keySet() 함수를 사용하여 Map의 전체 key를 꺼낸다.
foreach 반복문을 사용한다.
// Map 선언 및 데이터 넣기 (테스트용 Map)
Map<String, Object> testMap = new HashMap<String, Object>();
testMap.put("red", "apple");
testMap.put("yellow", "banana");
testMap.put("green", "melon");
// keySet()을 사용하여 Map에 들어있는 모든 key값을 꺼낸다.
// 전체 key를 가지고 Map에서 value값을 꺼낼 수 있다.
for (String mapKey : testMap.keySet())
{
System.out.println("key : " + mapKey + " / " +
"value : " + testMap.get(mapKey));
}
특정 key값의 value를 가져오고싶다면 get(key)를 사용
TreeMap<Integer, String> map = new TreeMap<>();
map.put(45, "Brown");
map.put(37, "James");
map.put(23, "Martin");
답
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSet1 {
public static void main(String[] args) {
TreeSet<Integer> tree = new TreeSet<Integer>();
tree.add(3); tree.add(1);
tree.add(2); tree.add(4);
System.out.println("인스턴스 수: " + tree.size());
// for-each문에 의한 반복
for (Integer n : tree) {
System.out.print(n.toString() + '\t');
}System.out.println();
// Iterator 반복자에 의한 반복
for(Iterator<Integer> itr = tree.iterator(); itr.hasNext();)
System.out.print(itr.next().toString() + '\t');
System.out.println();
}
}
이름과 학점>> 강나래 3.1
이름과 학점>> 니보라 3.3
이름과 학점>> 강하늘 4.3
이름과 학점>> 울나리 4.0
이름과 학점>> 그만해 3.7
장학생 선발 학점 기준 입력>> 3.2
장학생 명단: 그만해 강하늘 니보라 울나리
답
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class HashMapScholarship {
public static void main(String[] args) {
final int STUDENT = 5;
HashMap<String, Double> sch = new HashMap<>();
Scanner sc = new Scanner(System.in);
System.out.print("이름과 학점을 5개 입력하시오.");
for (int i = 0; i < STUDENT; i++) {
System.out.println("이름과 학점>> ");
String name = sc.next();
double grade = sc.nextDouble();
sch.put(name, grade);
}
System.out.println("장학생 선발 기준 학점: ");
double gr = sc.nextDouble();
double grade = 0;
String name = null;
Set<String> keys = sch.keySet();
for (String key : keys) {
if (sch.get(key) >= gr) {
name = key;
System.out.println("장학생은 " + name);
}
}
}
}