当前位置:   article > 正文

# Day18-Java基础_at java.util.hashtable.put(hashtable.java:401)

at java.util.hashtable.put(hashtable.java:401)

Day18-Java

1、Map集合

偶对象指的是一对对象,即:两个对象同时保存,这两个对象是按照了“key=value”的形式进行定义的,即:可以通过key找到对应的value数据,就好像电话本一样,例如,电话本之中保存了如下的信息:

Key =张三,value=123456;

Key=李四,value=234567;

现在如果要想找到张三的电话,那么肯定根据张三的key,取得对应的value,而如果现在要想找到王五的电话,由于没有王五这个key,所以返回的结果就是null。

Map就是实现这样一种操作的数据结构,这个接口之中的定义的主要操作方法如下。

方法名称类型描述
public V put(K key, V value)普通向集合之中保存数据
public V get(Object key)普通通过指定的key取得对应value
Public Set keyset()普通将Map中的所有key以Set集合的方式返回
Public Set<Map,Entry<K,V>>entrySet()普通将Map集合变为Set集合

在Map接口之中有两个常用的子类:HashMap,Hashtable。

1.1 子类:HashMap

HashMap是Map接口之中使用最多的一个子类

public class HashMap<K,V>
	extends AbstractMap<K,V>
	implements Map<K,V>, Cloneable, Serializable
  • 1
  • 2
  • 3

HashMap演示Map接口中各个主要方法的作用

验证Map

package com.day18.demo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapDemo {
	public static void main(String[] args) {
		Map<Integer,String> map = new HashMap<>();
		map.put(1, "Hello,world!!");
		map.put(2, "zsr");
		map.put(3, ".com");
		Set<Integer> keyset = map.keySet();//取得所有的key信息
		Iterator<Integer> iterator = keyset.iterator();
		while(iterator.hasNext()){
			Integer key = iterator.next();
			System.out.println(key + "=" + map.get(key));
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

面试题:解释HashMap的原理

​ 在数据量小的时候HashMap是按照链表的模式存储的。当数据量变大之后,为了进行快速的查找,将这个链表变为一个红黑树(均衡二叉树),用hash码作为数据定位,来进行保存的。

​ 所有的方法都是异步的,属于线程不安全操作。

1.2 子类:Hashtable

jdk1.0提供三大主要类:Vector、Enumeration、Hashtable。

Hashtable是最早实现这种二元偶对象数据结构,由于后期的设计也让其与Vector一样多实现了MMap接口。

package com.day18.demo;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashtableDemo {
	public static void main(String[] args) {
		Map<Integer,String> map = new Hashtable<>();
		map.put(1, "Hello,world!!");
		map.put(2, "zsr");
		map.put(3, ".com");
		map.put(null, null);
		System.out.println(map);
	}
}

Exception in thread "main" java.lang.NullPointerException
	at java.util.Hashtable.put(Hashtable.java:460)
	at com.day18.demo.HashtableDemo.main(HashtableDemo.java:15)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

以上Hashtable无法传入空值并对其进行输出。而HashMap可以

package com.day18.demo;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashtableDemo {
	public static void main(String[] args) {
		Map<Integer,String> map = new HashMap<>();
		map.put(1, "Hello,world!!");
		map.put(2, "zsr");
		map.put(3, ".com");
		map.put(null, null);
		System.out.println(map);
	}
}

{null=null, 1=Hello,world!!, 2=zsr, 3=.com}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
区别HashMapHashtable
时间JDK1.2JDK1.0
性能采用异步处理方式,性能高采用同步处理方式性能相对较低
安全性线程安全线程不安全
设置null允许key、value设置为null不允许key、value设置为null,否则出现异常

image-20210903151117112

1.3 ConcurrentHashMap子类

ConcurrentHashMap 的特点 = Hashtable的线程安全性 + HashMap的高性能,在使用ConcurrentHashMap处理的时候既可以保证多个线程更新数据的同步,又可以保证很高效的查询速度。

public class ConcurrentHashMap<K,V>
extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Serializable
  • 1
  • 2
  • 3

如果说现在采用一定的算法,将保存的大量数据平均分在不同的桶(数据区域),这样在进行数据查找的时候就可以避免这种全部的数据扫描。

数据分桶

package com.day18.demo;

import java.util.Random;

public class ConcurrentHashMapDemo {
	public static void main(String[] args) {
		for(int i = 0; i <10 ; i++){
			new Thread(()->{
				Random random = new Random();
				int temp = random.nextInt(9999);
				int result = temp % 3;
				switch(result){
					case 0:
						System.out.println("第0桶" + temp);
						break;
					case 1:
						System.out.println("第1桶" + temp);
						break;
					case 2:
						System.out.println("第2桶" + temp);
						break;
				}
			}).start();	
		}
		
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

采用分桶之后每一个数据必须有一个明确的分桶标记,很明显使用hashCode()。

image-20210904093623026

image-20210904093616074

image-20210904094132836

image-20210904094740183

1.4 Map使用Iterator输出

在实际的开发之中,如果你存储数据是为了输出,那么优先考虑一定是Collection,使用Map的主要操作就是设计我们的内容,而后通过get()进行查找。使用Map迭代输出的需求会有,但是不多。

通过一个简单的图形来观察Collection与Map保存数据的区别

image-20210904095939989

image-20210904100617165

通过Iterator输出Map内容

package com.day18.demo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapIteratorDemo {
	public static void main(String[] args) {
		Map<Integer,String> map = new HashMap<>();
		map.put(1, "hELLO,");
		map.put(2, "WORLD!!!");	
		//1.将map变为Set集合
		Set<Map.Entry<Integer,String>> set = map.entrySet();
		//2.实例化Iterator接口
		Iterator<Entry<Integer, String>> iter = set.iterator();
		//3.迭代输出每一个Map.Entry对象
		while(iter.hasNext()){
			//4.取出Map.Entry
			Map.Entry<Integer, String> me = iter.next();
			//5.取得key和value
			System.out.println(me.getKey() + "=" + me.getValue());
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
1.5 Map中的key实现说明

自定义Key类型

package com.day18.demo;

import java.util.HashMap;
import java.util.Map;

class Person2{
	private String name;
	public Person2(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person2 other = (Person2) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}
public class PersonDemo {
	public static void main(String[] args) {
		Map<Person2,String> map = new HashMap<Person2,String>();
		map.put(new Person2("张三"), new String("zs"));
		System.out.println(map.get(new Person2("张三")));
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

因为实际开发,对于map集合中key类型,不是String就是Integer,这些系统类都帮用户覆写了equals()、hashCode()方法了。

1.6 TreeMap子类

可以排序的Map子类,他是按照key的内容来进行排序的。

TreeMap操作

package com.day18.demo;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

class Person2{
	private String name;
	public Person2(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person2 other = (Person2) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}
public class PersonDemo {
	public static void main(String[] args) {
		Map<Person2,String> map = new TreeMap<Person2,String>();
		map.put(new Person2("张三"), new String("zs"));
		System.out.println(map.get(new Person2("张三")));
	}
}

Exception in thread "main" java.lang.ClassCastException: com.day18.demo.Person2 cannot be cast to java.lang.Comparable
	at java.util.TreeMap.compare(TreeMap.java:1294)
	at java.util.TreeMap.put(TreeMap.java:538)
	at com.day18.demo.PersonDemo.main(PersonDemo.java:51)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

出现以上问题是因为没有实现Comparable接口。

package com.day18.demo;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

class Person2 implements Comparable<Person2>{
	private String name;
	public Person2(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public int compareTo(Person2 o) {
		// TODO Auto-generated method stub
		return this.name.compareTo(o.name);
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person2 other = (Person2) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}
public class PersonDemo {
	public static void main(String[] args) {
		Map<Person2,String> map = new TreeMap<Person2,String>();
		map.put(new Person2("张三"), new String("zs"));
		System.out.println(map.get(new Person2("张三")));
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

Collection保存数据的目的是为了输出、Map保存数据的目的是为了根据key查找,

Map使用Iterator输出(Map.Entry的作用)

一些类的设计原理,这些是在你面试用得到的,而我们开发里面就是使用HashMap子类

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/501693
推荐阅读
相关标签
  

闽ICP备14008679号