HasMap增改删除遍历

 1 public void QWER(){
 2     Scanner sc=new Scanner(System.in);
 3     HashMap hm=new HashMap();
 4     Student stu=new Student();
 5 //    LinkedList liss=new LinkedList();//LinkeList类
 6     String name;
 7     int chengji;
 8     for (int qq = 0; qq < 2; qq++) {
 9         System.out.println("请输入学生姓名和成绩");
10         name=sc.next();
11         chengji=sc.nextInt();
12         stu=new Student(name,chengji);
13         hm.put(qq, stu);//映射
14     }
15     Set set =hm.entrySet();
16     Iterator it=set.iterator();
17     while(it.hasNext())
18     {
19         Map.Entry en=(Map.Entry)it.next();
20         Student dog =(Student)en.getValue();
21         System.out.println(en.getKey()+" "+en.getValue());
22     }
23     /**
24      * 换人实现
25      */
26     
27     
28     System.out.println("该数据");
29     int xiabiao=sc.nextInt();
30     name=sc.next();
31     chengji=sc.nextInt();
32     stu=new Student(name,chengji);
33     hm.put(xiabiao, stu);//映射
34     
35     
36     
37     System.out.println("遍历");
38     System.out.println(hm.values());//遍历
39     System.out.println("集合个数");
40 /*    System.out.println(hm.size());//显示集合个数
41     hm.clear();//清空
42     hm.put("q", 1);
43     hm.put("w", 2);
44     System.out.println("枚举");
45     System.out.println(hm.entrySet());//枚举
46     hm.put("q", 3);
47     System.out.println(hm.entrySet());//枚举
48     hm.remove("q");
49     System.out.println(hm.entrySet());//枚举
50     
51     *

 

posted @ 2018-08-10 18:55  米佳5714  阅读(97)  评论(0编辑  收藏  举报