Java哈希表和有序表如何实现

发布时间:2023-04-14 09:53:34 作者:iii
来源:亿速云 阅读:90

这篇文章主要介绍“Java哈希表和有序表如何实现”,在日常操作中,相信很多人在Java哈希表和有序表如何实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java哈希表和有序表如何实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

哈希表(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)

存放基础类型操作

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; 
    }
}

到此,关于“Java哈希表和有序表如何实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 哈希表实现源码
  2. 哈希表的实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:怎么用Python打造一个语音合成系统

下一篇:Windows11中小部件高CPU使用率怎么修复

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》