1,HashMap基本用法
1 package list;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 /**
7 * 测试map的基本用法
8 * @author acer
9 *
10 */
11 public class Hashmap {
12 public static void main(String[] args)
13 {
14 Map map=new HashMap();
15 System.out.println(map.isEmpty());
16 map.put("日本汽车", new car("丰田"));
17 map.put("德国汽车", new car("奥迪"));
18 map.put("日本电器", new camera("canon"));
19 map.put("one", new Integer(1));
20 System.out.println(map.size());
21 car c=(car)map.get("日本汽车");
22 System.out.println(c.name);
23 System.out.println(map.containsKey("日本汽车"));
24 System.out.println(map.containsValue(new car("canon")));
25 System.out.println(map.containsValue(new Integer(1)));
26 map.remove("日本");
27 }
28 }
29 class car{
30 String name;
31 public car(String name)
32 {
33 this.name=name;
34 }
35 }
36 class camera{
37 String name;
38 public camera(String name)
39 {
40 this.name=name;
41 }
42 }
2.自己定义HashMap()(原始版)
1 package list;
2 /**
3 * 实现较为粗糙的HashMap类
4 * Map:1,存放键值对2根据键对象找相应的值对象3键值不能重复
5 * @author acer
6 *
7 */
8 public class myHashMap {
9 arrmap[] arr=new arrmap[100];//此时暂时不考虑数组扩容问题,只实现一些基本的方法
10 int size;
11 public void put(Object key,Object value)
12 {
13 arrmap newarr=new arrmap(key,value);
14 //解决键值重复的问题
15 for(int i=0;i<size;i++)
16 {
17 if(arr[i].key.equals(key))
18 {
19 arr[i].value=value;
20 return;
21 }
22 }
23 arr[size++]=newarr;
24 }
25 public Object get(Object key)
26 {
27 for(int i=0;i<size;i++)
28 {
29 if(arr[i].key.equals(key))
30 return arr[i].value;
31 }
32 return null;33 }
34 public boolean containsKey(Object key)
35 {
36 for(int i=0;i<size;i++)
37 {
38 if(arr[i].key.equals(key))
39 {
40 return true;
41 }
42 }
43 return false;
44 }
45 public boolean containsValue(Object value)
46 {
47 for(int i=0;i<size;i++)
48 {
49 if(arr[i].value.equals(value))
50 {
51 return true;
52 }
53 }
54 return false;
55 }
56 public static void main(String[] args)
57 {
58 myHashMap arr=new myHashMap();
59 arr.put("日本", new car("丰田"));
60 arr.put("德国", new car("奔驰"));
61 arr.put("美国", new car("福特"));
62 arr.put("日本电器", new camera("canon"));
63 car c=(car)arr.get("日本");
64 System.out.println(c.name);
65 camera d=(camera)arr.get("日本电器");
66 System.out.println(d.name);
67 System.out.println(arr.containsKey("日本"));
68 System.out.println(arr.containsValue(new car("丰田")));
69 arr.put("日本", new car("本田"));
70 c=(car)arr.get("日本");
71 System.out.println(c.name);
72 }
73 }
74 class arrmap
75 {
76 Object key;
77 Object value;
78 public arrmap(Object key,Object value)
79 {
80 this.key=key;
81 this.value=value;
82 }
83 }
3.升级版
1 package list;
2
3 import java.util.LinkedList;
4
5 /**
6 * 完善粗糙的myhashmap
7 * 提高查询效率
8 * @author acer
9 *
10 */
11 public class myHashMapUpdate {
12 LinkedList[] arr=new LinkedList[999];
13 int size;
14 public void put(Object key,Object value)
15 {
16 arrmap arra=new arrmap(key,value);
17 int a=key.hashCode()%999;
18 if(arr[a]==null)
19 {
20 LinkedList list=new LinkedList();
21 arr[a]=list;
22 list.add(arra);
23 }
24 else
25 {
26 LinkedList list=arr[a];
27 for(int i=0;i<list.size();i++)
28 {
29 arrmap arra1=(arrmap)list.get(i);
30 if(arra1.key.equals(key))
31 {
32 arra1.value=value;
33 return;
34 }
35 }
36 arr[a].add(arra);
37 }
38 }
39 public Object get(Object key)
40 {
41 int a=key.hashCode()%999;
42 if(arr[a]!=null)
43 {
44 LinkedList list=arr[a];
45 for(int i=0;i<list.size();i++)
46 {
47 arrmap arra=(arrmap)list.get(i);
48 if(arra.key.equals(key))
49 {
50 return arra.value;
51 }
52 }
53 }
54 return null;
55 }
56 public static void main(String[] args)
57 {
58 myHashMapUpdate arr=new myHashMapUpdate();
59 arr.put("日本", new car("丰田"));
60 arr.put("日本", new car("本田"));
61 arr.put("德国", new car("奔驰"));
62 arr.put("美国", new car("福特"));
63 arr.put("日本电器", new camera("canon"));
64 car c=(car)arr.get("日本");
65 System.out.println(c.name);
66 }
67 }
java开始到熟悉105-107,布布扣,bubuko.com