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

    测试结果:

  • 相关阅读:
    【SignalR学习系列】3. SignalR实时高刷新率程序
    【SignalR学习系列】4. SignalR广播程序
    【SignalR学习系列】5. SignalR WPF程序
    python gb2312 转换为 utf-8
    爬虫 需要什么样的 CPU,内存 和带宽
    TypeError: sequence item 0: expected string, Tag found
    MySQL 数据的 截取,数据清洗
    MySQL (1366, "Incorrect string value: '\xF0\x9F\x8E\xAC\xE5\x89...' for column 'description' at row 1")
    微博爬虫 ----- 微博发布时间清洗
    ReferenceError: weakly-referenced object no longer exists Python kafka
  • 原文地址:https://www.cnblogs.com/Catherinezhilin/p/8990110.html
Copyright © 2011-2022 走看看