자바기초강좌 교육자료] 헤쉬테이블(HashTable) 사용 예(2)

자바기초강좌 교육자료] 헤쉬테이블(HashTable) 사용 예(2)


헤쉬테이블(HashTable) 사용 예(2)

import java.util.*;

public class StudentHashtableTest {


  publicstatic void main(String[] args) {


  Hashtable table=new Hashtable();

  table.put("1",newStudent("Jee",1,"Seoul"));//0

  table.clear();//모두 제거

  table.put("2",newStudent("Gong",2,"Seoul"));//

  table.put("3",newStudent("Song",3,"Seoul"));//2

  table.put("4",newStudent("Lee",4,"Koyang"));//3

  table.put("4",newStudent("Lee",4,"Koyang"));//3과 중복x

  System.out.println(table.size());

  System.out.println(table.containsKey("4"));

  table.remove("3");

  Studentstu=(Student)table.get("2");//key

  System.out.println(stu.getId()+"  "+stu.getName()+"  "+stu.getAddr());


   //모든 elements 출력하기

  //Set set=table.keySet();//Map을 참고

  Enumeration enums=table.keys()

  while(enums.hasMoreElements()){


  Stringkey=(String)enums.nextElement() ;//핵심

  Student stus=(Student)table.get(key);

  System.out.println(stus.getId()+"  "+stus.getName()+"  "+stus.getAddr());

  }

  }

}


 

+ Recent posts