/*
Map集合:该集合存储键值对。一对一对往里存。而且要保证键的唯一性。
Map
|--Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。效率低。基本已废弃
|--HashMap:底层是哈希表数据结构,允许使用 null 值和 null 键,该集合是不同步的。将hashtable替代,.效率高,不保证顺序。
|--TreeMap:底层是二叉树数据结构。线程不同步。可以用于给map集合中的键进行排序。保证顺序
*/ import java.util.*; /**
* 学生类实现Comparable可比较接口
*/
class Student implements Comparable<Student> {
private String name;
private int age; Student(String name, int age) {
this.name = name;
this.age = age;
} /**
* 覆写接口中的compareTo方法跟equals覆写组合判断对象是否相同
*
* @param s
* @return
*/
@Override
public int compareTo(Student s) {
int num = Integer.valueOf(this.age).compareTo(s.age);
if (num == 0)
return this.name.compareTo(s.name);
return num;
} /**
* 覆写hashcode方法用于底层采用hash算法的容器
*
* @return
*/
@Override
public int hashCode() {
//用姓名和年龄组合值作为hashcode
return name.hashCode() + age * 34;
} /**
* 覆写equals方法实现自定义比较
*
* @param obj
* @return
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Student))
throw new ClassCastException("类型不匹配");
//强制转换类
Student s = (Student) obj;
//比较类中成员变量是否相同
return this.name.equals(s.name) && this.age == s.age;
} public String getName() {
return name;
} public int getAge() {
return age;
} public String toString() {
return name + "," + age;
}
} /**
* 比较器
*/
class StuNameComparator implements Comparator<Student> {
public int compare(Student s1, Student s2) {
int num = s1.getName().compareTo(s2.getName());
if (num == 0)
return Integer.valueOf(s1.getAge()).compareTo(s2.getAge());
return num;
}
} class Test2 {
public static void main(String[] args) {
HashMap<Student, String> hm = new HashMap<Student, String>(); hm.put(new Student("lisi1", 21), "beijing");
hm.put(new Student("lisi1", 21), "beijing"); //与上面相同不存
hm.put(new Student("lisi1", 21), "tianjin");
hm.put(new Student("lisi2", 22), "shanghai");
hm.put(new Student("lisi3", 23), "nanjing");
hm.put(new Student("lisi4", 24), "wuhan");
hm.put(new Student("lisi4", 24), "wuhan"); //与上面相同不存 System.out.println("-----------------第第一种取出方式 keySet, 迭代器-------------------------");
//第一种取出方式 keySet, 迭代器 Set<Student> keySet = hm.keySet(); Iterator<Student> it = keySet.iterator(); while (it.hasNext()) {
Student stu = it.next();
String addr = hm.get(stu);
System.out.println(stu + "___" + addr);
} System.out.println("-----------------第二种取出方式 entrySet, 迭代器-------------------------"); //第二种取出方式 entrySet, 迭代器
Set<Map.Entry<Student, String>> entrySet = hm.entrySet(); Iterator<Map.Entry<Student, String>> iter = entrySet.iterator(); while (iter.hasNext()) {
Map.Entry<Student, String> me = iter.next();
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu + "___" + addr);
} System.out.println("---------------第三种取出方式 for循环方式---------------------------"); //第三种取出方式 for循环方式
for (Map.Entry<Student, String> entry : hm.entrySet()) {
System.out.println(entry.getKey() + "___" + entry.getValue());
} System.out.println("----------------TreeMap自定义排序,使用比较器--------------------------");
//TreeMap自定义排序,使用比较器
TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());
tm.put(new Student("lisi1", 21), "beijing");
tm.put(new Student("lisi1", 21), "beijing"); //与上面相同不存
tm.put(new Student("lisi1", 21), "tianjin");
tm.put(new Student("lisi2", 22), "shanghai");
tm.put(new Student("lisi3", 23), "nanjing");
tm.put(new Student("lisi4", 24), "wuhan");
tm.put(new Student("lisi4", 24), "wuhan"); //与上面相同不存
for (Map.Entry<Student, String> entry : tm.entrySet()) {
System.out.println(entry.getKey() + "___" + entry.getValue());
} /**
* -----------------第第一种取出方式 keySet, 迭代器-------------------------
* lisi4,24___wuhan
* lisi2,22___shanghai
* lisi1,21___tianjin
* lisi3,23___nanjing
* -----------------第二种取出方式 entrySet, 迭代器-------------------------
* lisi4,24___wuhan
* lisi2,22___shanghai
* lisi1,21___tianjin
* lisi3,23___nanjing
* ---------------第三种取出方式 for循环方式---------------------------
* lisi4,24___wuhan
* lisi2,22___shanghai
* lisi1,21___tianjin
* lisi3,23___nanjing
* ----------------TreeMap自定义排序,使用比较器--------------------------
* lisi1,21___tianjin
* lisi2,22___shanghai
* lisi3,23___nanjing
* lisi4,24___wuhan
*/
}
}

  

Java 中HashTable、HashMap、TreeMap三者区别,以及自定义对象是否相同比较,自定义排序等的更多相关文章

  1. Hashtable,HashMap,TreeMap有什么区别?Vector,ArrayList,LinkedList有什么区别?int和Integer有什么区别?

    接着上篇继续更新. /*请尊重作者劳动成果,转载请标明原文链接:*/ /*https://www.cnblogs.com/jpcflyer/p/10759447.html* / 题目一:Hashtab ...

  2. 沉淀再出发:java中的HashMap、ConcurrentHashMap和Hashtable的认识

    沉淀再出发:java中的HashMap.ConcurrentHashMap和Hashtable的认识 一.前言 很多知识在学习或者使用了之后总是会忘记的,但是如果把这些只是背后的原理理解了,并且记忆下 ...

  3. Java中Set Map List 的区别

    java中set map list的区别: 都是集合接口 简要说明 set --其中的值不允许重复,无序的数据结构 list   --其中的值允许重复,因为其为有序的数据结构 map--成对的数据结构 ...

  4. Java中Comparable和Comparator接口区别分析

    Java中Comparable和Comparator接口区别分析 来源:码农网 | 时间:2015-03-16 10:25:20 | 阅读数:8902 [导读] 本文要来详细分析一下Java中Comp ...

  5. Java中关于HashMap的元素遍历的顺序问题

    Java中关于HashMap的元素遍历的顺序问题 今天在使用如下的方式遍历HashMap里面的元素时 1 for (Entry<String, String> entry : hashMa ...

  6. java中fail-fast 和 fail-safe的区别

    java中fail-fast 和 fail-safe的区别   原文地址:http://javahungry.blogspot.com/2014/04/fail-fast-iterator-vs-fa ...

  7. [转]为什么Java中的HashMap默认加载因子是0.75

    前几天在一个群里看到有人讨论hashmap中的加载因子为什么是默认0.75. HashMap源码中的加载因子 static final float DEFAULT_LOAD_FACTOR = 0.75 ...

  8. Java中的HashMap的工作原理是什么?

    问答题23 /120 Java中的HashMap的工作原理是什么? 参考答案 Java中的HashMap是以键值对(key-value)的形式存储元素的.HashMap需要一个hash函数,它使用ha ...

  9. 关于Java中的HashMap的深浅拷贝的测试与几点思考

    0.前言 工作忙起来后,许久不看算法,竟然DFA敏感词算法都要看好一阵才能理解...真是和三阶魔方还原手法一样,田园将芜,非常可惜啊. 在DFA算法中,第一步是需要理解它的数据结构,在此基础上,涉及到 ...

  10. 转:Java中abstract和interface的区别

    转自:Java中abstract和interface的区别 abstract class和interface是Java语言中对于抽象类定义进行支持的两种机制,正是由于这两种机制的存在,才赋予了Java ...

随机推荐

  1. python3-使用requests模拟登录网易云音乐

    # -*- coding: utf-8 -*- from Crypto.Cipher import AES import base64 import random import codecs impo ...

  2. node+mysql+vue+express项目搭建

    第一步:项目搭建之前首先需要安装node环境和MySQL数据库. 在已经完成上述的条件下开始进行以下操作: npm install @vue/cli -g   (-g 代表全局安装) 初始化项目  v ...

  3. Microsoft Compiled HTML Help / Uncompiled .chm File XML External Entity

    [+] Credits: John Page (aka hyp3rlinx) [+] Website: hyp3rlinx.altervista.org[+] Source:  http://hyp3 ...

  4. Centos6.10编译安装php-7.1.12并安装redis模块

    1.服务器初始化 yum update -yyum install epel-release -yyum install gcc gcc-c++ wget lsof lrzsz telnet -y 2 ...

  5. Android ProGuard:代码混淆压缩

    写这篇文章的目的 一直以来,在项目中需要进行代码混淆时每次都要去翻文档,很麻烦.也没有像写代码那样记得那么多.既然要查来查去,就不如自己捋一捋这个知识点了,被人写的终究还是别人的.所以自己去翻看了很多 ...

  6. Linux下MySQL的数据文件存放在哪里的??

    http://bbs.csdn.net/topics/390620630 mysql> show variables like '%dir%';+------------------------ ...

  7. 【JUC】5.线程池—Executor

    创建线程池可以分为三种方式: 1. 通过ThreadPoolExecutor的构造方法,创建ThreadPoolExecutor的对象,即一个线程池对象: 此构造方法,一共7个参数,5个必须参数,2个 ...

  8. 【Bug】MQ消息与事务提交

    项目联调期间,遇到个bug,涉及MQ消息传递和事务提交时间问题,简单记录下. 背景 审核服务(审核创建项目),点击审核通过,后台代码会在提交事务前发送MQ消息(该消息由项目管理服务消费),发送成功后, ...

  9. HTML&CSS基础-内边框

    HTML&CSS基础-内边框  作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML源代码 <!DOCTYPE html> <html> &l ...

  10. ET·ci — 全自动软件测试调度(持续集成)平台

            ET·ci 提供了编译-测试-发布解决方案,包括:自动提取配置库代码进行自动构建, 自动调度静态测试工具(如QAC)进行静态测试,自动调度单元测试工具(如Tessy)开展动态测试,自动 ...