Java 使用HashMap基本操作

来源:互联网 发布:js日期比大小 编辑:程序博客网 时间:2024/06/05 03:23

代码

package beginnersbook.com;import java.util.HashMap;import java.util.Map;import java.util.Iterator;import java.util.Set;public class Details {   public static void main(String args[]) {      /* This is how to declare HashMap */      HashMap<Integer, String> hmap = new HashMap<Integer, String>();      /*Adding elements to HashMap*/      hmap.put(12, "Chaitanya");      hmap.put(2, "Rahul");      hmap.put(7, "Singh");      hmap.put(49, "Ajeet");      hmap.put(3, "Anuj");      /* Display content using Iterator*/      Set set = hmap.entrySet();      Iterator iterator = set.iterator();      while(iterator.hasNext()) {         Map.Entry mentry = (Map.Entry)iterator.next();         System.out.print("key is: "+ mentry.getKey() + " & Value is: ");         System.out.println(mentry.getValue());      }      /* Get values based on key*/      String var= hmap.get(2);      System.out.println("Value at index 2 is: "+var);      /* Remove values based on key*/      hmap.remove(3);      System.out.println("Map key and values after removal:");      Set set2 = hmap.entrySet();      Iterator iterator2 = set2.iterator();      while(iterator2.hasNext()) {          Map.Entry mentry2 = (Map.Entry)iterator2.next();          System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");          System.out.println(mentry2.getValue());       }   }}


输出

key is: 49 & Value is: Ajeetkey is: 2 & Value is: Rahulkey is: 3 & Value is: Anujkey is: 7 & Value is: Singhkey is: 12 & Value is: ChaitanyaValue at index 2 is: RahulMap key and values after removal:Key is: 49 & Value is: AjeetKey is: 2 & Value is: RahulKey is: 7 & Value is: SinghKey is: 12 & Value is: Chaitanya



0 0
原创粉丝点击