zoukankan      html  css  js  c++  java
  • java --Using the Map Interface

    1.HashMap Class

    示例代码:

     1 package self;
     2 
     3 import java.util.HashMap;
     4 
     5 public class HashMapDemo {
     6     public static void main(String args[]) {
     7         HashMap<String,Integer> obj=new HashMap<String,Integer>();
     8         Integer iobj1=new Integer (11);
     9         Integer iobj2=new Integer (22);
    10         Integer iobj3=new Integer (33);
    11         Integer iobj4=new Integer (44);
    12         
    13         System.out.println("Size of HashMap is :"+obj.size());
    14         obj.put("L1", iobj1);
    15         obj.put("L2",iobj2);
    16         obj.put("L3",iobj3);
    17         obj.put("L4",iobj4);
    18         obj.put("L5", iobj1);
    19         
    20         System.out.println("
    HashMap after adding the objects:"+obj);
    21         System.out.println("Size of HashMap after adding objects:"+obj.size());
    22         obj.remove("L3");
    23         obj.remove("L5");
    24         
    25         System.out.println("
    HashMap after removing the objects:"+obj);
    26         System.out.println("Size of the HashMap after removing objects: "+obj.size());
    27     }
    28 }
    View Code

    测试结果:

    2.TreeMap Class

    示例代码:

     1 package self;
     2 
     3 import java.util.TreeMap;
     4 
     5 public class TreeMapDemo {
     6     public static void main(String args[]) {
     7         TreeMap<Integer,String> obj=new TreeMap<Integer,String>();
     8     
     9         String sobj1=new String("Value 1");
    10         String sobj2=new String("Value 2");
    11         String sobj3=new String("Value 3");
    12         String sobj4=new String("Value 4");
    13         
    14         
    15         System.out.println("Size of TreeMap is :"+obj.size());
    16         
    17         obj.put(101, sobj1);
    18         obj.put(102,sobj2);
    19         obj.put(103,sobj3);
    20         obj.put(104,sobj4);
    21         obj.put(105, sobj1);
    22         
    23         System.out.println("
    TreeMap after adding the objects:"+obj);
    24         System.out.println("Size of TreeMap after adding objects:"+obj.size());
    25         obj.remove(103);
    26         obj.remove(105);
    27         
    28         System.out.println("
    TreeMap after removing the objects:"+obj);
    29         System.out.println("Size of the TreeMap after removing objects: "+obj.size());
    30     }
    31 }
    View Code

    测试结果:

    3.HashMap Class

    示例代码:

     1 package self;
     2 
     3 import java.util.Hashtable;
     4 
     5 public class HashtableDemo {
     6     public static void main(String args[]) {
     7         Hashtable<Integer,Double> obj=new Hashtable<Integer,Double>();
     8     
     9         Double dobj1=new Double(55.6);
    10         Double dobj2=new Double(34.6);
    11         Double dobj3=new Double(98.6);
    12         Double dobj4=new Double(12.5);
    13         
    14         
    15         System.out.println("Size of Hashtable is :"+obj.size());
    16         
    17         obj.put(55, dobj1);
    18         obj.put(60,dobj2);
    19         obj.put(65,dobj3);
    20         obj.put(70,dobj4);
    21         obj.put(75, dobj1);
    22         
    23         System.out.println("
    Hashtable after adding the objects:"+obj);
    24         System.out.println("Size of Hashtable after adding objects:"+obj.size());
    25         obj.remove(65);
    26         obj.remove(75);
    27         
    28         System.out.println("
    Hashtable after removing the objects:"+obj);
    29         System.out.println("Size of the Hashtable after removing objects: "+obj.size());
    30     }
    31 }
    View Code

    测试结果:

  • 相关阅读:
    Knockoutjs 实践入门 (2) 绑定事件
    HTTP If-Modified-Since引发的浏览器缓存汇总
    net.sf.json和 com.fasterxml.jackson中对象转json的区别
    JPA EntityManager详解
    Spring JPA中OneToOne和OneToMany用法
    Spring Data JPA中CrudRepository与JpaRepository的不同
    Git提交撤销
    Tomcat启动报StackOverflowError
    Git分支合并冲突解决(续)
    Git分支合并冲突解决
  • 原文地址:https://www.cnblogs.com/Catherinezhilin/p/8990110.html
Copyright © 2011-2022 走看看