Java哈希表和有序表实例代码讲解

作者:ziop-三月 时间:2023-05-28 11:29:29 

哈希表(HashMap)

hash查询的时间复杂度是O(1)

按值传递

Character,Short,Integer,Long, Float,Double,String,Boolean,在java当中哈希表内部以值的形式传递,而不是一地址的形式传递。

例如:

HashMap<Integer, String> intMap = new HashMap<>();
intMap.put(1234567, "111");
Integer a = 1234567;
Integer b = 1234567;
System.out.println("a==b = " + (a == b));
System.out.println("a.equals(b) = " + a.equals(b));
System.out.println("intMap.get(a) = " + intMap.get(a));
System.out.println("intMap.get(b) = " + intMap.get(b));

// 输出结果
// a==b = false
// a.equals(b) = true
// intMap.get(a) = 111
// intMap.get(b) = 111

由上边的案例中 a!= b,但是intMap.get(a) == intMap.get(b).我们可以看出,在我们从hashmap里面查询或者操作某些值的话,是以值的形式去传递和匹配的,而不是以内存地址的形式去匹配。

按址传递

如果是非原生的类型的话,以内存地址的形式传递。例如:

public static class Node {
   private int value;
   public Node(int value) {
       this.value = value;
   }
}
HashMap<Node, String> map = new HashMap<>();
Node node1 = new Node(1);
Node node2 = new Node(1);
map.put(node1, "ziop");
System.out.println("map.containsKey(node1) = " + map.containsKey(node1));
System.out.println("map.containsKey(node2) = " + map.containsKey(node2));

//输出结果
//map.containsKey(node1) = true
//map.containsKey(node2) = false

内存大小比较

基础类型,一条记录的内存大小是Key的大小加上Value的大小。

非基础类型, 一条记录的内存大小是 两个地址的大小, 一个地址8字节,key和value 共16字节

如果是 基础类型和非基础类型的混合类型的话,就是各自按照各自的方式计算

有序表(TreeMap)

  • 有序表会根据key的大小进行 升序排列 ,我们可以用他来做hashmap中的所有操作,并且扩展出了,查找第一个key或者最后一个key的操作,也扩展出了查找小于某个区间的最大值和大于某个区间的最小值

  • 所有操作时间复杂度都是O(logn)级别。

  • 但是如果key是非基础类型的话,并不能直接排序,需要该类型实现了排序接口,有可排序功能。或者在new treeMap的时候传入比较方法

存放基础类型操作

TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(3,"我是3 ");
treeMap.put(0,"我是3 ");
treeMap.put(7,"我是3 ");
treeMap.put(2,"我是3 ");
treeMap.put(5,"我是3 ");
treeMap.put(9,"我是3 ");
treeMap.put(1,"我是3 ");
System.out.println("treeMap.containsKey(3) = "+treeMap.containsKey(3));
System.out.println("treeMap.containsKey(6) = "+treeMap.containsKey(6));
System.out.println("treeMap.get(3) = "+treeMap.get(3));
treeMap.put(3,"他是3");
System.out.println("treeMap.get(3) = "+treeMap.get(3));
treeMap.remove(3);
System.out.println("treeMap.get(3) = "+treeMap.get(3));
treeMap.remove(3);
System.out.println("treeMap.firstKey() = "+treeMap.firstKey());
System.out.println("treeMap.lastKey() = "+treeMap.lastKey());
//        返回 小于等于五 并且最近的 key
System.out.println("treeMap.floorKey(5) = "+treeMap.floorKey(5));
System.out.println("treeMap.floorKey(6) = "+treeMap.floorKey(6));
//        返回 大于等于 4 并且最靠近的值
System.out.println("treeMap.ceilingKey(4) = "+treeMap.ceilingKey(4));

//输出结果如下
//treeMap.containsKey(3) = true 
//treeMap.containsKey(6) = false 
//treeMap.get(3) = 我是3 
//treeMap.get(3) = 他是3
//treeMap.get(3) = null
//treeMap.firstKey() = 0
//treeMap.lastKey() = 9
//treeMap.floorKey(5) = 5
//treeMap.floorKey(6) = 5
//treeMap.ceilingKey(4) = 5

存放非基础类型进行操作

//        存放非基础类型
public static void main(String[] args) {
TreeMap<Node, Integer> treeMap1 = new TreeMap<>();
Node node3 = new Node(3);
Node node4 = new Node(4);
treeMap1.put(node3, 3);
treeMap1.put(node4, 4);
System.out.println("treeMap1.firstEntry().getValue() = " + treeMap1.firstEntry().getValue());
System.out.println("treeMap1.lastEntry().getValue() = " + treeMap1.lastEntry().getValue());
}
public static class Node implements Comparable<Node> {
private int value;
   public Node(int value) {
   this.value = value;
}
       @Override
   public int compareTo(Node node) {
       return this.value - node.value;
   }
}

来源:https://blog.csdn.net/weixin_46178937/article/details/127930567

标签:Java,哈希表,有序表
0
投稿

猜你喜欢

  • WCF和Remoting之间的消息传输

    2023-04-15 01:01:20
  • myeclipse安装Spring Tool Suite(STS)插件的方法步骤

    2023-02-22 00:56:02
  • springboot bean循环依赖实现以及源码分析

    2022-06-05 11:50:15
  • 详解使用Spring的BeanPostProcessor优雅的实现工厂模式

    2023-01-14 02:15:53
  • 图解JVM垃圾内存回收算法

    2023-10-13 17:24:35
  • 单点登录的三种方式和JWT的介绍与使用

    2023-05-19 22:10:59
  • while和for可以相互转换的例子分享

    2023-08-23 02:17:46
  • 深入理解Spring中bean的生命周期介绍

    2023-02-08 17:21:37
  • C#多线程学习之Thread、ThreadPool、Task、Parallel四者区别

    2023-08-27 05:32:14
  • C#使用SharpZipLib压缩解压文件

    2021-10-27 18:56:21
  • android安装后启动出错解决

    2021-10-31 12:13:26
  • SpringBoot使用Kaptcha实现验证码的生成与验证功能

    2022-11-30 15:57:44
  • MapTask工作机制图文详解

    2021-12-13 11:03:27
  • 新手入门Jvm-- JVM对象创建与内存分配机制

    2021-11-04 19:28:10
  • Java RabbitMQ高级特性详细分析

    2021-12-26 00:31:36
  • Android实现简单水波纹效果

    2021-11-09 12:40:45
  • springboot static关键字真能提高Bean的优先级(厉害了)

    2023-03-10 07:39:12
  • Java完美实现2048小游戏

    2023-06-14 12:59:25
  • Android开发笔记之如何正确获取WebView的网页Title

    2022-06-04 07:02:41
  • spring项目中切面及AOP的使用方法

    2021-12-01 21:11:29
  • asp之家 软件编程 m.aspxhome.com