码迷,mamicode.com
首页 > 其他好文 > 详细

HashMap

时间:2017-04-23 18:05:29      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:bsp   键值   gets   div   oid   size   int   hash   log   

 1 package student;
 2 
 3 public class student {
 4 
 5     private String num;
 6     private String name;
 7      public student(String num,String name){
 8         this.num = num;
 9         this.name = name;
10      }
11      
12         @Override
13     public String toString() {
14         return "student [num=" + num + ", name=" + name + "]";
15     }
16 
17     public static void main(String[] args) {
18         // TODO Auto-generated method stub
19 
20     }
21 
22 }

 

 

 1 package student;
 2 
 3     import java.util.Map;
 4     import java.util.Scanner;
 5     import java.util.HashMap;
 6 
 7      public class test {
 8         private student S1 = new student("1","zhang");
 9         private student S2 = new student("2","li");
10         private student S3 = new student("3","wang");
11         Map<String,student> map = 
12                 new HashMap<String, student>();
13        public test(){
14               map.put("1",S1);    
15               map.put("2",S2);
16               map.put("3",S3);
17     }
18 public String getStr(){
19         //System.out.print("请输入:");
20         Scanner in = new Scanner(System.in);
21         String Str = in.next();
22         return Str;
23         
24     }
25     public void keyFind(){
26          System.out.println("请输入键值查询:");
27          String key = getStr();
28         boolean contains = map.containsKey(key);
29         
30         if(contains){
31             System.out.println("存在该学生 "+key+map.get(key).toString());
32         }
33         else{
34             System.out.println("不存在该学生 "+key);
35         }
36         
37     } 
38     
39    public void Add(){
40        System.out.print("请输入键值进行添加:");
41        String key = getStr();
42        System.out.print("请输入学号:");
43        String num = getStr();
44        System.out.print("请输入姓名:");
45        String name = getStr();
46        student S = new student(num,name);
47        map.put(key, S);
48    }
49    public void getSize(){
50        System.out.println("学生个数:"+map.size());
51        
52    }
53    public void remove(){
54        System.out.println("输入键值进行删除");
55        String key = getStr();
56        map.remove(key);
57    }
58     public static void main(String[] args) {
59     test t = new test();
60     
61     t.keyFind();
62     t.Add();
63     t.getSize();
64     t.remove();
65     t.getSize();
66 
67     }
68 
69 }

 

HashMap

标签:bsp   键值   gets   div   oid   size   int   hash   log   

原文地址:http://www.cnblogs.com/the-wang/p/6752974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!