import java.util.*;
public class JavaTest extends Object {
public static void main(String[] args) {
HashSet<String> hSet = new HashSet<>();
hSet.add(new String("7799"));
hSet.add(new String("7799"));
hSet.add(new String("7799"));
System.out.println("인스턴스 수 : " + hSet.size());
}
}
[Console]
인스턴스 수 : 1
import java.util.*;
class JavaPractice {
public static void main(String[] args) {
HashSet<Integer> lotto = new HashSet<>();
while (lotto.size() < 6) {
int random = (int) ((Math.random() * 45) + 1);
lotto.add(random);
}
System.out.print("번호 : " + lotto);
}
}
[Console]
번호 : [17, 18, 35, 6, 27, 11]
(번호는 돌릴 때마다 다르게 나온다)
@Override
public int hashCode() {
return Integer.hashCode(value);
}
public static int hashCode(int value) {
return value;
}
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
import java.util.*;
class JavaPractice {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(45, "Brown");
map.put(37, "James");
map.put(23, "Martin");
System.out.println("Key 23 : " + map.get(23));
System.out.println("Key 37 : " + map.get(37));
System.out.println("Key 45 : " + map.get(45));
map.remove(37);
System.out.println("Key 37 : " + map.get(37));
}
}
[Console]
Key 23 : Martin
Key 37 : James
Key 45 : Brown
Key 37 : null (해당하는 key의 value가 삭제되어 null로 표시)
map.put(45, "Brown");
map.put(37, "James");
map.put(23, "Martin");
map.put(23, "Tom");
System.out.println("Key 23 : " + map.get(23));
System.out.println("Key 37 : " + map.get(37));
System.out.println("Key 45 : " + map.get(45));
[Console]
Key 23 : Tom
Key 37 : James
Key 45 : Brown
import java.util.*;
class JavaPractice {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(45, "Brown");
map.put(37, "James");
map.put(23, "Martin");
Set<Integer> keyset = map.keySet();
System.out.println("전체 key 출력");
for (Integer n : keyset) {
System.out.print(n.toString() + '\t');
}
System.out.println();
System.out.println("전체 value 출력");
for (Integer n : keyset) {
System.out.print(map.get(n) + '\t');
}
System.out.println();
System.out.println("반복자 이용 가능");
for (Iterator<Integer> itr = keyset.iterator(); itr.hasNext();) {
System.out.print(map.get(itr.next()) + '\t');
}
}
}
[Console]
전체 key 출력
37 23 45
전체 value 출력
James Martin Brown
반복자 이용 가능
James Martin Brown
import java.util.*;
class JavaPractice {
public static void main(String[] args) {
Map<Integer, String> map = new TreeMap<>();
map.put(45, "Brown");
map.put(37, "James");
map.put(23, "Martin");
Set<Integer> keyset = map.keySet();
System.out.println("전체 key 출력");
for (Integer n : keyset) {
System.out.print(n.toString() + '\t');
}
System.out.println();
System.out.println("전체 value 출력");
for (Integer n : keyset) {
System.out.print(map.get(n) + '\t');
}
System.out.println();
System.out.println("반복자 이용 가능");
for (Iterator<Integer> itr = keyset.iterator(); itr.hasNext();) {
System.out.print(map.get(itr.next()) + '\t');
}
}
}
[Console]
전체 key 출력
23 37 45
전체 value 출력
Martin James Brown
반복자 이용 가능
Martin James Brown
Stack | Queue |
---|---|
Last-In-First-Out | First-In-First-Out |
import java.util.*;
class JavaPractice {
public static void main(String[] args) {
Queue<String> que = new LinkedList<>();
que.offer("Box");
que.offer("Toy");
que.offer("Robot");
System.out.println("next : " + que.peek());
System.out.println(que.poll());
System.out.println(que.poll());
System.out.println("next : " + que.peek());
System.out.println(que.poll());
}
}
[Console]
next : Box
Box
Toy
next : Robot
Robot