集合框架之Map HashMap

HashMap是Map接口的实现类,Key以HashSet存储。

HashMap初始化对象:HashMap<Student,Object> map=new HashMaP<String,Object>();   (HashMap类型为一个实体类和一个Object)

    注意:

1、HashMap也用到哈希表,所以向HashMap中插入元素时,Key中一定要实现HashCode方法和equals方法。

2、一般建议用String类型作为Key。

 1 public class Demo1 {
 2 
 3     public static void main(String[] args) {
 4         HashMap<Student,Object> map=new HashMap<Student,Object>();
 5         Student student=new Student(1,"大明",20);
 6         Student student1=new Student(2,"小明",21);
 7         Student student2=new Student(3,"舔狗",22);
 8         
 9         ArrayList<String> list=new ArrayList<String>();
10         list.add("3333");
11         list.add("444");
12         map.put(student1, list);
13         map.put(student2, list); 
14         map.put(student, list);
15         System.out.println(map);
16         
17         
18         
19 
20     }
21 
22 }

猜你喜欢

转载自www.cnblogs.com/luojack/p/10817622.html