Jam's story
26일차 본문
HashSet으로 하니, 자체적인 방식으로 오름차순으로 출력 -> LinkedHashSet , 저장된 순서로 출력된다.
public class Ex01 {
public static void main(String[] args) {
Set<Integer> set=new LinkedHashSet<Integer>();
while(set.size()<25) {
int n=(int)(Math.random()*50)+1;
set.add(n);
}
int[][] bingo=new int[5][5];
int idx=0;
Iterator<Integer> ir=set.iterator();
while (ir.hasNext()) {
Integer n = (Integer) ir.next();
bingo[idx/5][idx%5] =n;
idx++;
System.out.printf("[%02d]",n);
if(idx%5==0) System.out.println();
}
}
}
HashTable HashMap
HashMap<K,V>
Key, Value 어떤 타입을 사용할 껀지,
두가지 방법으로 선언가능, jdk 업그레이드로 2번처럼 쓴다 .
HashMap<String, String > hm=new HashMap<String, String>();
HashMap<String, String > hm=new HashMap();
Key 중복X
Value 중복O
public class Ex02 {
public static void main(String[] args) {
//id name //
// HashMap<String, String > hm=new HashMap<String, String>();
HashMap<String, String > hm=new HashMap();
hm.put("Kenik", "이창익");
hm.put("admin", "관리자");
hm.put("hong", "관리자");
System.out.println(hm);
// hm.clear(); 모두 삭제
/// hm.clone(); 복제
//id가 있는지 확인후 ,name출력
String id="admin";
if(hm.containsKey(id)) {
String name= hm.get(id);
System.out.printf("key(id)=%s, value(name)=%s",id,name);
}else {
System.out.println("찾고자 하는 ke는 존재하지 않아 ");
}
hm.put("admin", "김재우");
hm.put("park", "홍길동");
hm.put(null, null);
hm.put(null, "널");
System.out.println(hm);
//모든 키만 출력
hm.keySet();
Set<String> ks=hm.keySet();
Iterator<String> ir=ks.iterator();
while (ir.hasNext()) {
String key= (String) ir.next();
String value=hm.get(key);
System.out.printf("key=%s",key);
}
System.out.println();
System.out.println("========value값 출력 ===========");
//모든 value 출력
Collection<String> hs=hm.values();
Iterator<String> is=hs.iterator();
while (is.hasNext()) {
String value = (String) is.next();
System.out.printf("value: %s",value);
}
//import java.util.map
Set<Entry<String, String>> es =hm.entrySet();
Iterator<Entry<String, String>> ie= es.iterator();
while (ie.hasNext()) {
Entry<java.lang.String, java.lang.String> entry = (Entry<java.lang.String, java.lang.String>) ie.next();
}
}
}
'Java' 카테고리의 다른 글
days27 -입출력 (0) | 2022.03.28 |
---|---|
[컬렉션 프레임워크] - TreeSet , HashMap (0) | 2022.03.26 |
[컬렉션 프레임워크] - 스택 , 큐 (0) | 2022.03.25 |
[컬렉션 프레임워크] -ArrayList (0) | 2022.03.24 |
25일차 (0) | 2022.03.24 |
Comments