실행 결과로 나와야 하는 값
나라 이름과 인구를 입력하세요.(예: Korea 5000)
나라 이름, 인구 >> Korea 5000
나라 이름, 인구 >> USA 1000000
나라 이름, 인구 >> Swiss 2000
나라 이름, 인구 >> France 3000
나라 이름, 인구 >> 그만
인구 검색 >> France
France의 인구는 3000
인구 검색 >> 스위스
스위스 나라는 없습니다.
인구 검색 >> 그만
소스코드
public class ForVelog2 {
public static void main(String[] args) {
Scanner sc1 = new Scanner(System.in);
HashMap<String, Integer> countrys = new HashMap<>();
System.out.println("나라 이름과 인구를 입력하세요. (예: Korea 5000)");
while (true) {
System.out.print("나라 이름, 인구 >>");
String country = sc1.next();
if (country.equals("그만"))
break;
int popu = sc1.nextInt();
countrys.put(country, popu);
}
Set<String> ks = countrys.keySet();
while (true) {
System.out.print("인구 검색>> ");
String searched_Country = sc1.next();
if (searched_Country.equals("그만"))
break;
if (countrys.get(searched_Country) == null) {
System.out.println(searched_Country + " 나라는 없습니다.");
continue;
}
System.out.println(searched_Country + "의 인구는 " + countrys.get(searched_Country));
}
}
}
큐란 FIFO ( First in First Out )
가장 먼저 들어간 것이 가장 먼저 나오는 저장 방식이다
스택이란 LIFO ( Last in First Out )
가장 나중에 들어간 것이 가장 먼저 나오는 저장 방식이다.
Map 컬렉션은 Key값과 Value값 ( Map <K, V> )으로 구성되어 있으며,
Key값은 중복될 수 없으며, 만약 중복해서 정의된다면 덮어써진다.
TreeMap<Integer, String> map = new TreeMap<>();
map.put(45, "Brown");
map.put(37, "James");
map.put(23, "Martin");
소스코드
System.out.println(map.get(45));
System.out.println(map.get(37));
System.out.println(map.get(23));
실행결과
Brown
James
Martin
이름과 학점>> 강나래 3.1
이름과 학점>> 니보라 3.3
이름과 학점>> 강하늘 4.3
이름과 학점>> 울나리 4.0
이름과 학점>> 그만해 3.7
장학생 선발 학점 기준 입력>> 3.2
장학생 명단: 그만해 강하늘 니보라 울나리
소스코드
public class ForVelog1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
HashMap<String, Double> Students = new HashMap<String, Double>();
HashMap<String, Double> Scholarship_Students = new HashMap<String, Double>();
for (int i = 0; i < 5; i++) {
System.out.println("이름과 학점 입력>>");
String name = sc.next();
double score = sc.nextDouble();
Students.put(name, score);
}
Set<String> ks = Students.keySet();
System.out.println("장학 기준 점수를 입력해주세요 >>");
double scholarship_standard = sc.nextDouble(); // 장학 기준 점수 받아오기
for (String std : ks) {
double student_score = Students.get(std); //
if (student_score >= scholarship_standard) {
Scholarship_Students.put(std, Students.get(std)); // 장학 기준 점수와의 비교
}
}
Set<String> ks2 = Scholarship_Students.keySet(); // 장학 선발자의 keySet만 받아옴
for (String sch_std : ks2) {
System.out.print(sch_std + " "); // 장학 선발자만 출력
}
}
}