zoukankan      html  css  js  c++  java
  • HashMap练习

     1 import java.util.*;
    2 class HashMapTest //无序
    3 {
    4 public static void main(String [] args)
    5 {
    6 HashMap<Student,String> hs=new HashMap<Student,String>();
    7 hs.put(new Student("lisi01",21),"beijing");
    8 hs.put(new Student("lisi02",22),"tianjing");
    9 hs.put(new Student("lisi02",22),"dongjing");
    10 hs.put(new Student("lisi04",24),"shanghai");
    11 hs.put(new Student("lisi03",23),"xiamen");
    12 //第一种取出方式
    13 /*
    14 Set<Student> en=hs.keySet(); //这里取出的是student的键,非<Student,String>
    15 for(Iterator<Student> it=en.iterator();it.hasNext();)
    16 {
    17 Student stu=it.next();
    18 String address=hs.get(stu);
    19 //System.out.println(stu);
    20 System.out.println(stu.getName()+".."+stu.getAge()+".."+address); //或者在student类中覆写toString方法
    21 }
    22 */
    23 //第二种取出方式 entrySet
    24 Set<Map.Entry<Student,String>> et=hs.entrySet();
    25 for(Iterator<Map.Entry<Student,String>> it=et.iterator();it.hasNext();) //Iterator 泛型别忘了<Map.Entry<Student,String>>
    26 {
    27 Map.Entry<Student,String> me=it.next();
    28 String address=me.getValue();
    29 Student info =me.getKey();
    30 System.out.println(info.getName()+".."+info.getAge()+".."+address);
    31 }
    32
    33 }
    34
    35 }
    36 class Student implements Comparable<Student> //泛型的使用
    37 {
    38 private String name;
    39 private int age;
    40 private String address;
    41 Student(String name,int age)
    42 {
    43 this.name=name;
    44 this.age=age;
    45 }//用来初始化实例
    46 public void setName(String name) //eclipse 用多了,容易写错 (传参)
    47 {
    48 this.name=name;
    49 }
    50 public String getName() //类型 以及return语句(无参)
    51 {
    52 return name;
    53 }
    54 public void setAge(int age)
    55 {
    56 this.age=age;
    57 }
    58 public int getAge()
    59 {
    60 return age;
    61 }
    62 /*
    63 public String toString()
    64 {
    65 return this.name+"..."+this.age;
    66 }
    67 */
    68 /*只要是哈希表结构的集合 都需要覆写这两个方法*/
    69 public int hashCode() //整型 ,注意Code 首字母大写
    70 {
    71 return name.hashCode()+age*23;
    72 }
    73 public boolean equals(Object obj)
    74 {
    75 if(!(obj instanceof Student))
    76 {
    77 throw new ClassCastException("类型不匹配");
    78 }
    79 Student s=(Student)obj;
    80 return this.name.equals(s.name)&&this.age==s.age; //别什么都写成this.age.equals(s.age)
    81 }
    82 //如果存储对象使用到二叉树结构集合,则需要进行内部排序。
    83 public int compareTo(Student s)
    84 {
    85 int num =new Integer(this.age).compareTo(new Integer(s.age));
    86 if(num==0)
    87 {
    88 return this.name.compareTo(s.name);
    89 }
    90 return num;
    91 }
    92 }



  • 相关阅读:
    NYOJ 625 笨蛋的难题(二)
    NYOJ 102 次方求模
    ZJU Least Common Multiple
    ZJUOJ 1073 Round and Round We Go
    NYOJ 709 异形卵
    HDU 1279 验证角谷猜想
    BNUOJ 1015 信息战(一)——加密程序
    HDU 1202 The calculation of GPA
    "蓝桥杯“基础练习:字母图形
    "蓝桥杯“基础练习:数列特征
  • 原文地址:https://www.cnblogs.com/pinotao/p/2314735.html
Copyright © 2011-2022 走看看