容器的深入研究(二)—Set与Map
一、Set类的作用
二、Set类延生的四种形式
三、非基础类型如何使用Set的四种形式
四、Queue的使用
五、PriorityQueue的使用
六、Map的六种形式
七、HashMap散列码的实现
八、如何制作优秀的散列码
回答:
1、Set的特点:①、存入Set的每个元素在Set中都是唯一的 ②、输出的时候不保证元素的次序
2、Set延生的四种类型:
①、HashSet:为快速查找而设计的,存入Set的元素必须实现hashCode()方法
②、TreeSet:输出的时候保证元素的优先顺序(不是按照输入时候的顺序,而是根据元素的大小)。存入Set的元素必须实现Comparable接口
③、LinkedHashSet:具有HashSet快速查找的特性。还能够按照输入Set的顺序输出数据, 存入Set的元素必须实现hashCode()方法
④、SortedSet:对TreeSet的扩展,扩展了一些方法。(获取头,尾元素的方法。截取容器中部分元素的方法等)
3、对各种类型的使用。
步骤①、制作原始类TestType,制作HashCodeType继承TestType重写hashCode()方法,制作ComparableType继承TestType和Comparable接口
public class TestType {
protected int index;
public TestType(int index){
this.index = index;
}
public int getIndex(){
return index;
}
@Override
public String toString() {
return "TestType [index=" + index + "]";
}
}
TestType
public class HashCodeType extends TestType {
public HashCodeType(int index) {
super(index);
// TODO Auto-generated constructor stub
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return index;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
//判断是否是相同类比较。
if (obj instanceof TestType && ((TestType) obj).getIndex() == index){
return true;
}
else {
return false;
}
}
}
HashCodeType
public class ComparableType extends TestType implements Comparable<ComparableType>{
public ComparableType(int index) {
super(index);
// TODO Auto-generated constructor stub
}
@Override
public int compareTo(ComparableType o) {
// TODO Auto-generated method stub
if (index > o.getIndex()){
return -1;
}
else if (index == o.getIndex()){
return 0;
}
else {
return 1;
}
}
}
ComparableType
②、进行测试
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
fill(new HashSet<TestType>(), TestType.class);
fill(new LinkedHashSet<HashCodeType>(), HashCodeType.class);
fill(new TreeSet<>(), ComparableType.class);
}
public static <E> void fill(Set<E> set,Class<E> type ){
for(int i=0; i<10; ++i){
//通过反射创建类
try {
Constructor<E> constructor = type.getConstructor(int.class);
set.add(constructor.newInstance(i));
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(set.toString());
}
}
/**
*输出:
*[TestType [index=4], TestType [index=7], TestType [index=8], TestType [index=0], TestType [index=6], TestType [index=1], TestType [index=2], TestType [index=3], TestType [index=5], TestType [index=9]]
[TestType [index=0], TestType [index=1], TestType [index=2], TestType [index=3], TestType [index=4], TestType [index=5], TestType [index=6], TestType [index=7], TestType [index=8], TestType [index=9]]
[TestType [index=9], TestType [index=8], TestType [index=7], TestType [index=6], TestType [index=5], TestType [index=4], TestType [index=3], TestType [index=2], TestType [index=1], TestType [index=0]]
*/
Main
四、Queue的使用
①、顾名思义按照先进先出的顺序执行。 ②、除了ProrityQueue不是按照该顺序执行
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//不介绍队列的种类,但要知道LinkedList是一个队列
Queue<Integer> data = new LinkedList<>();
for(int i=0; i<10; ++i){
//添加数据
data.offer(i);
}
//返回顶部数据
while(data.peek() != null){
//移除顶部数据,并返回该条数据
System.out.println(data.remove()+" ");
}
}
}
Main
PriorityQueue的使用详解
①、需要继承Comparable接口,确定优先级 ②、可分为多层优先级(等会示例可以看到)
public class ProrityItem implements Comparable<ProrityItem>{
public char firstPrority;
public int secondPrority;
private String name;
public ProrityItem(char firstPrority, int secondPrority, String name) {
this.firstPrority = firstPrority;
this.secondPrority = secondPrority;
this.name = name;
}
@Override
public String toString() {
return "ProrityItem [firstPrority=" + firstPrority + ", secondPrority="
+ secondPrority + ", name=" + name + "]";
}
@Override
public int compareTo(ProrityItem o) {
// TODO Auto-generated method stub
//第一级优先级
if (firstPrority > o.firstPrority){
return 1;
}
else if (firstPrority == o.firstPrority){
//第二层优先级
if (secondPrority > o.secondPrority){
return 1;
}
else if (secondPrority == o.secondPrority) {
return 0;
}
else {
return -1;
}
}
else {
return -1;
}
}
}
PriorityItem
public class Main {
public static void main(String[] args){
PriorityQueue<ProrityItem> queue = new PriorityQueue<>();
queue.add(new ProrityItem('B', 1, "你好"));
queue.add(new ProrityItem('A', 6, "HaHa"));
queue.add(new ProrityItem('A', 2, "Yes"));
//不要用toString方法,数据结果不符
while(queue.peek()!= null){
System.out.println(queue.remove()+" ");
}
}
}
/*
*ProrityItem [firstPrority=A, secondPrority=2, name=Yes]
*ProrityItem [firstPrority=A, secondPrority=6, name=HaHa]
*ProrityItem [firstPrority=B, secondPrority=1, name=你好]
*
*/
Main
五、Map
Map的种类:
HashMap、TreeMap、
LinkedHashMap:首次查询输出时候按照插入的顺序, 注意:当查找的时候,查询次数高的会被排在后面,查询次数少的会被排在前面。
举例:就是我存入了1 ,2 ,3 当一开始查询 时候输出是1 ,2 ,3 。但是当我查了 1之后,我再查询全部的时候,输出的就是 2, 3, 1
WeakHashMap:弱类型,容易被垃圾回收机制回收
ConcurrentHashMap:多线程情况下使用的map
SortMap:形式同SortSet都是TreeMap的延生
Map的散列原理(继承AbstractMap实现原生的HashMap):
①、了解散列码的原理和快速查找的原理
首先散列集是由一段数组组成的,每个数组的控件称为桶。
通过数学公式将hashCode()方法中获取到的数据转换为数组的下标,找到对应的桶。
桶中存储的是一个容器对象(Collection),因为可能会有hashCode()转换成下标相同的情况。将hash值相同的对象放入容器中。
查找:
因为容器的查找,是从头开始一个个匹配的原则。
散列码能够先快速定位到相同的位置,再进行依次匹配。这样就能够减少其他不必要的查找,从而增加了效率。
②、制作HashMap
步骤①、继承AbstratMap类,确定桶的个数,桶中装载哪种容器。
public class MyHashMap<K,V> extends AbstractMap<K, V>{
//桶的数量
private static final int BARREL_COUNT = 937;
//使用ArrayList作为容器
private ArrayList<Entry<K, V>> [] barrels;
//为返回Set做准备
private Set<java.util.Map.Entry<K, V>> mSet;
public MyHashMap(){
barrels = new ArrayList [BARREL_COUNT];
mSet = new HashSet<>();
}
}
MyHashMap
步骤②、创建HashEntry类继承Entry接口。用来存储key和value
class MyEntry implements Entry<K, V>{
private K key;
private V value;
public MyEntry(K key,V value){
this.key = key;
this.value = value;
}
@Override
public K getKey() {
// TODO Auto-generated method stub
return key;
}
@Override
public V getValue() {
// TODO Auto-generated method stub
return value;
}
@Override
public V setValue(V value) {
// TODO Auto-generated method stub
V oldValue = this.value;
this.value = value;
return oldValue;
}
}
MyEntry
步骤③、重写put方法 1、当放入数据的时候,先获key的hashCode转换为桶的下标 2、根据下标寻找桶对应 的容器,如果容器没被创建则创建,并将数据转换为Entry装入容器中(也装入Set这个容器中)。 3、如果容器被创建了,之后获取容器,并将容器中的Entry拿出来和放入数据的key进行判断,是否已经存在。如果已经存在则替换,如果不存在则将entry装入容器中(也装入Set这个容器中)。
@Override
public V put(K key, V value) {
//注意:常用获取索引的方法就是根据数组的大小取余 ①
int index = Math.abs(key.hashCode())%BARREL_COUNT;
ArrayList<Entry<K, V>> list = barrels[index];
MyEntry entry = new MyEntry(key, value);
//判断容器是否存在
if (list == null){
barrels[index] = new ArrayList<>();
barrels[index].add(entry);
mSet.add(entry);
return value;
}
else{
//查找是否key已经存在
boolean found = false;
for(Entry<K, V> myEntry: list){
if (myEntry.getKey().equals(key)){
V oldValue = myEntry.getValue();
myEntry.setValue(value);
found = true;
return oldValue;
}
}
//不存在则装入List中
if (!found){
list.add(entry);
}
return value;
}
}
MyHashMap
步骤④、重写get方法 1、获取key的hashCode转换为桶的下标 2、根据下标寻找容器,如果容器不存在返回false,如果容器存在,则拿出entry与key进行比较。如果成功则返回value,不成功返回null
public V get(Object key) {
int index =Math.abs(key.hashCode())%BARREL_COUNT;
ArrayList<Entry<K, V>> list = barrels[index];
if (list == null){
return null;
}
else {
boolean found = false;
for(Entry<K, V> myEntry: list){
if (myEntry.getKey().equals(key)){
return myEntry.getValue();
}
}
return null;
}
}
MyHashMap
步骤⑤、创建Set 将 所有的entry装入Set中
@Override
public Set<java.util.Map.Entry<K, V>> entrySet() {
// TODO Auto-generated method stub
return mSet;
}
MyHashMap
如何制作好的散列码:
判定方法:①、hashCode()应该产生分布均匀的散列码。
如何制作:①、根据对象内容生成散列码 ②、首先给int result 初始化一个非0值 例如:int result = 7; ③、对每个可修改的成员变量计算出本身的散列码 比如:人 有 姓名和性别 所以需要对姓名和性别分别制作散列码。
④、合并计算。
举例:
public class HashTest {
public String str;
public int data;
public HashTest(String str,int data){
this.str = str;
this.data = data;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof HashTest){
HashTest test = (HashTest)obj;
if (test.str == str && test.data == data){
return true;
}
else {
return false;
}
}
return false;
}
@Override
public int hashCode() {
//第一步,给result赋值
int result = 17;
//第二步给可改变的成员变量对应的值
int strHash = str.hashCode();
int dataHash = data;
//第三步:合并值
result = 10*result + strHash;
result = 10*result + dataHash;
return result;
}
}
HashTest
容器的深入研究(二)—Set与Map的更多相关文章
- Cocos2d-x3.0模版容器具体解释之二:cocos2d::Map<K,V>
1.概述: 版本号: v3.0 beta 语言: C++ 定义在 "COCOS2DX_ROOT/cocos/base" 路径下的 "CCMap.h" 的头文件里 ...
- Fiddler实战深入研究(二)
Fiddler实战深入研究(二) 阅读目录 Fiddler不能捕获chrome的session的设置 理解数据包统计 请求重定向(AutoResponder) Composer选项卡 Filters选 ...
- (转载)Fiddler实战深入研究(二)
原文来源于:http://www.cnblogs.com/tugenhua0707/p/4637771.html,作者:涂根华 !个人觉得文章写的特别好,故收藏于此,感谢原作者的分享 Fiddler实 ...
- Fiddler实战深入研究(二)[转载]
Fiddler实战深入研究(二) 阅读目录 Fiddler不能捕获chrome的session的设置 理解数据包统计 请求重定向(AutoResponder) Composer选项卡 Filters选 ...
- AspNetCore容器化(Docker)部署(二) —— 多容器通信
一.前言 着上一篇 AspNetCore容器化(Docker)部署(一) —— 入门,在单个容器helloworld的基础上引入nginx反向代理服务器组成多容器应用. 二.配置反向代理转接 配置转接 ...
- Java入门记(五):容器关系的梳理(下)——Map
注意:阅读本文及相关源码时,需要数据结构相关知识,包括:哈希表.链表.红黑树. Map是将键(key)映射到值(value)的对象.不同的映射不能包含相同的键:每个键最多只能映射到一个值.下图是常见M ...
- tomcat 组件研究二--请求过程
上一篇博客大概总结了tomcat 的组件以及其组织方式,对于tomcat 的启动过程也进行进行了简单的总结,下面这篇博客,继续研究tomcat 处理请求的相关组件,其实就是主要研究Connectors ...
- spring源码学习之容器的扩展(二)
六 BeanFactory的后处理BeanFactory作为spring容器的基础,用于存放所有已经加载的bean,为了保证程序上的高扩展性,spring针对BeanFactory做了大量的扩展,比如 ...
- Nginx源码研究二:NGINX的事件处理概论
NGINX作为服务端的应用程序,在客户端发出数据后,服务端在做着这样一些处理,数据先会经过网卡,网卡会和操作系统做交互,经过操作系统的协议栈处理,再和不同的应用程序交互. 在这里面涉及两个概念,一个是 ...
随机推荐
- maven项目依赖被改为文件夹时如何改回lib
如图
- Hdu1092
#include <stdio.h> int main() { ; while(scanf("%d",&n)){ ) {;} else{ int i,a; ;i ...
- 我和小美的撸码日记--基于MVC+Jqgrid的.Net快速开发框架
前言:以前的帐号没有发首页的权限,特此把这篇文章从另外一个博客移过来,这篇是<我和小美的撸码日记>的序 一转眼务农6年了,呆过大公司也去过小作坊,码农的人生除了抠腚还是抠腚.在所有呆过的公 ...
- linq学习三个实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 在UC浏览器上很炫的一个效果
效果简述: 这个效果将会强行去除UC浏览器的网址输入框和底部的菜单栏,这样网页将会占据整个手机界面.感觉看起来很像是一个APP. 浏览器界面右下角将会出现一个向上的标志,用来唤出简单的菜单栏. (界面 ...
- GetProcessMemoryInfo API取得进程所用的内存
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683219(v=vs.85).aspx 例子: https://msdn.mic ...
- 五子棋AI
下面是一些参考资料,打算看看: http://www.cnblogs.com/Blog_SivenZhang/archive/2010/06/13/1757677.html http://blog.c ...
- java设计模式--行为型模式--策略模式
策略模式: 策略模式 概述 定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法可独立于使用它的客户而变化. 适用性 1.许多相关的类仅仅是行为有异.“策略”提供了一种用多个行 ...
- 比较有用的log4j.properties
转自 http://walsh.iteye.com/blog/314941 log4j.properties log4j.rootLogger=DEBUG,CONSOLE,DATABASE,FIL ...
- 【转】android4.1.2 CTS测试总结
原文网址:http://blog.csdn.net/bboyliaoye/article/details/17299259 公司客户的产品要过google GMS认证,首先要过的就是兼容性测试(CTS ...