import java.util.HashSet;
import java.util.Set;
public class Lotto {
public static void main(String[] args) {
Set<Integer> lottoNumbers = new HashSet<>();
while (lottoNumbers.size() < 6) {
int number = (int) (Math.random() * 45 + 1);
lottoNumbers.add(number);
}
System.out.println(lottoNumbers);
}
}
//문제
public class Test1 {
public static void main(String[] args) {
HashSet<Person> hSet = new HashSet<Person>();
hSet.add(new Person("LEE", 10));
hSet.add(new Person("LEE", 10));
hSet.add(new Person("PARK", 35));
hSet.add(new Person("PARK", 35));
System.out.println("저장된 데이터 수: " + hSet.size());
System.out.println(hSet);
}
}
import java.util.HashSet;
import java.util.Set;
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Person) {
Person p = (Person) obj;
return this.name.equals(p.name) && this.age == p.age;
} else {
return false;
}
}
@Override
public int hashCode() {
return name.hashCode() + age;
}
@Override
public String toString() {
return name + "(" + age + "세)";
}
}
public class Test1 {
public static void main(String[] args) {
Set<Person> hSet = new HashSet<>();
hSet.add(new Person("LEE", 10));
hSet.add(new Person("LEE", 10));
hSet.add(new Person("PARK", 35));
hSet.add(new Person("PARK", 35));
System.out.println("저장된 데이터 수: " + hSet.size());
System.out.println(hSet);
}
}
/
저장된 데이터 수: 2
[LEE(10세), PARK(35세)]
/
//문제
public class Test2 {
public static void main(String[] args) {
HashSet<Num> set = new HashSet<>();
set.add(new Num(7799));
set.add(new Num(9955));
set.add(new Num(7799));
System.out.println("인스턴스 수: " + set.size());
for(Num n : set)
System.out.print(n.toString() + '\t');
System.out.println();
}
}
import java.util.HashSet;
import java.util.Set;
class Num {
int num;
Num(int num) {
this.num = num;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Num) {
Num n = (Num) obj;
return this.num == n.num;
} else {
return false;
}
}
@Override
public int hashCode() {
return num;
}
@Override
public String toString() {
return String.valueOf(num);
}
}
public class Test2 {
public static void main(String[] args) {
Set<Num> set = new HashSet<>();
set.add(new Num(7799));
set.add(new Num(9955));
set.add(new Num(7799));
System.out.println("인스턴스 수: " + set.size());
for (Num n : set)
System.out.print(n.toString() + '\t');
System.out.println();
}
}
/
====출력
인스턴스 수: 2
7799 9955
/
Map<Integer, String> map = new HashMap<>();
map.put(45, "Brown");
map.put(37, "James");
map.put(23, "Martin");
Map<Integer, String> map = new TreeMap<>();
map.put(45, "Brown");
map.put(37, "James");
map.put(23, "Martin");
for (String value : map.values()) {
System.out.println(value);
}
"그만"이 입력될 때까지 나라 이름과 인구를 입력 받아 저장하고, 다시 나라 이름을 입력받아 인구를 출력하는 프로그램을 작성하라.
다음 해시맵을 이용하라.
-containsKey 라는 함수를 사용해 보셔도 좋습니다.
=================================================================================
나라 이름과 인구를 입력하세요.(예: Korea 5000)
나라 이름, 인구 >> Korea 5000
나라 이름, 인구 >> USA 1000000
나라 이름, 인구 >> Swiss 2000
나라 이름, 인구 >> France 3000
나라 이름, 인구 >> 그만
인구 검색 >> France
France의 인구는 3000
인구 검색 >> 스위스
스위스 나라는 없습니다.
인구 검색 >> 그만
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
HashMap<String, Integer> nations = new HashMap<>();
System.out.println("나라 이름과 인구를 입력하세요.(예: Korea 5000)");
while (true) {
System.out.print("나라 이름, 인구 >> ");
String nation = scanner.next();
if (nation.equals("그만")) break;
int population = scanner.nextInt();
nations.put(nation, population);
}
while (true) {
System.out.print("인구 검색 >> ");
String search = scanner.next();
if (search.equals("그만")) break;
if (nations.containsKey(search)) {
System.out.println(search + "의 인구는 " + nations.get(search));
} else {
System.out.println(search + " 나라는 없습니다.");
}
}
scanner.close();
}
}