..열심히 공부하세../JAVA 문법

[예제] set, map 예제

댄스댄스 2012. 6. 26. 18:49

 

  ---------------------------------------------------문제
 String word="write.do=my.board.WriteForm";
  int ch=word.indexOf("=");  
  String command=word.substring(0,ch);//write.do
  String classnm=word.substring(ch+1);//my.board.WriteForm  
  System.out.println(command);
  System.out.println(classnm);

 

  //command가 key, classnm이 value로
  //해시맵 hm객체에 추가

  HashMap hm=new HashMap();

  hm.put(command, classnm);

 

----------------------------------------------------문제

  HashSet list=new HashSet();
  list.add("list.do=my.board.ListAction");
  list.add("update.do=my.board.UpdateAction");
  list.add("delete.do=my.board.DeleteAction");
  
  HashMap map=new HashMap();
  // =앞의 문자열은 key값
  // =뒤의 문자열은 value값으로 map에 추가
  
  Iterator iter=list.iterator();
  while(iter.hasNext()){
   String word=(String)iter.next();
   int ch=word.indexOf("=");   
   String command=word.substring(0,ch);
   String classnm=word.substring(ch+1);
   map.put(command, classnm);
   System.out.println(map.get(command));
  }