treemap

面试题:HashSet、TreeSet 和HashMap 的实现与原理

走远了吗. 提交于 2020-02-27 04:21:47
说下 TreeSet 和 HashSet 什么区别呢? 它们的区别点主要在他们的底层数据结构不同,HashSet 使用的是 HashMap 来实现,而 TreeSet 使用的是 TreeMap 来实现的。 哦?那你了解 HashMap 和 TreeMap 的区别吗? HashMap 是一个最常用的数据结构,它主要用于我们有通过固定值(key)获取内容的场景,时间复杂度可以最快优化到 O(1) 哈,当然效果不好的时候时间复杂度是 O(logN) 或者O(n)。虽然固定值查找提高了速度,但是 HashMap 不能保证固定值,也就是 key 的顺序,所以这个时候 TreeMap 就出现了,虽然它的查找、删除、更新的时间复杂度都是 O(logN),但是他可以保证 key 的有序性。 恩恩,掌握的还不错,那你和我说一下 HashMap 和 TreeMap 的底层实现有什么不同,才导致的他们有这么大的差异? 这个原因主要是它们底层用的实现不同,HashMap 使用的是数组(桶)和哈希的方式实现,巧妙通过 key 的哈希路由到每一个数组用于存放内容,这时候通过 key 获取 value 的时间复杂度就是 O(1),当然因为 key 的哈希可能碰撞,所以就需要针对碰撞的时候做处理,HashMap 里面每一个数组(桶)里面存的其实是一个链表,key 的哈希冲突以后会追加到链表上面,这时候再通过

Lucene索引存储结构

女生的网名这么多〃 提交于 2020-02-26 01:15:56
字典数据结构: 字典树(Trie tree) 、FST 字典树(Trie tree) 典型应用是用于统计和排序大量的 字符串 (但不仅限于字符串), 所以经常被搜索引擎系统用于文本词频统计。 它的优点是:最大限度地减少无谓的字符串比较,查询效率比 哈希表 高。 基本的实现有array与linked-list两种: array实现: linked-list实现: 双数组Trie树 在Trie数实现过程中,我们发现了每个节点均需要 一个数组来存储next节点,非常占用存储空间,空间复杂度大, 双数组Trie树正是解决这个问题的。 双数组Trie树(DoubleArrayTrie)是一种空间复杂度低的Trie树, 应用于字符区间大的语言(如中文、日文等)分词领域。 双数组的原理是,将原来需要多个数组才能表示的Trie树,使用两个数组就可以存储下来,可以极大的减小空间复杂度 使用两个数组base和check来维护Trie树, base负责记录状态, check负责检查各个字符串是否是从同一个状态转移而来, 当check[i]为负值时,表示此状态为字符串的结束。 check[i] 代表从什么状态转换过来,如下,都是从base[t] 转换过来,所以 check[ta] = check[tb] base[t] + a.code = base[ta] base[t] + b.code = base

数据结构-树

泪湿孤枕 提交于 2020-02-25 15:27:45
一、红黑树 红黑树是一种自平衡的二叉查找树,是一种高效的查找树。它是由 Rudolf Bayer 于1972年发明,在当时被称为 对称二叉 B 树(symmetric binary B-trees) 。后来,在1978年被 Leo J. Guibas 和 Robert Sedgewick 修改为如今的 红黑树 。红黑树具有良好的效率,它可在 O(logN) 时间内完成查找、增加、删除等操作。因此,红黑树在业界应用很广泛,比如 Java 中的 TreeMap,JDK 1.8 中的 HashMap、C++ STL 中的 map 均是基于红黑树结构实现的。考虑到红黑树是一种被广泛应用的数据结构,所以我们很有必要去弄懂它。 树根:必为黑色 外部节点:必为黑色 其余节点:若为红,则只能有黑孩子(红节点,之父必黑) 外部节点到根节点:途中黑节点数目相等(黑深度) https://www.ixigua.com/i6783852720995435011/ https://www.ixigua.com/home/874852209407496/video/ 来源: oschina 链接: https://my.oschina.net/u/3727895/blog/3167684

TreeMap - Search Time Complexity

↘锁芯ラ 提交于 2020-02-21 09:46:31
问题 What is the time complexity of a get() and put() in a TreeMap? Is the implementation same as a Red-Black Tree? 回答1: From here: http://java.sun.com/javase/6/docs/api/java/util/TreeMap.html This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations 回答2: TreeMap is: A Red-Black tree based NavigableMap implementation. This implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations. Algorithms are

TreeMap<String, Integer> object's get method return null value

╄→尐↘猪︶ㄣ 提交于 2020-01-23 13:55:28
问题 import java.util.*; public class Sort { static class ValueComparator implements Comparator<String> { Map<String, Integer> base; ValueComparator(Map<String, Integer> base) { this.base = base; } @Override public int compare(String a, String b) { if (base.get(a) >= base.get(b)) { return 1; } else { return -1; } } } public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<String, Integer>(); ValueComparator vc = new ValueComparator(map); TreeMap<String, Integer> sorted

TreeMap<String, Integer> object's get method return null value

こ雲淡風輕ζ 提交于 2020-01-23 13:53:57
问题 import java.util.*; public class Sort { static class ValueComparator implements Comparator<String> { Map<String, Integer> base; ValueComparator(Map<String, Integer> base) { this.base = base; } @Override public int compare(String a, String b) { if (base.get(a) >= base.get(b)) { return 1; } else { return -1; } } } public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<String, Integer>(); ValueComparator vc = new ValueComparator(map); TreeMap<String, Integer> sorted

TreeMap<String, Integer> object's get method return null value

南楼画角 提交于 2020-01-23 13:53:10
问题 import java.util.*; public class Sort { static class ValueComparator implements Comparator<String> { Map<String, Integer> base; ValueComparator(Map<String, Integer> base) { this.base = base; } @Override public int compare(String a, String b) { if (base.get(a) >= base.get(b)) { return 1; } else { return -1; } } } public static void main(String[] args) { HashMap<String, Integer> map = new HashMap<String, Integer>(); ValueComparator vc = new ValueComparator(map); TreeMap<String, Integer> sorted

Java: SortedMap, TreeMap, Comparable? How to use?

帅比萌擦擦* 提交于 2020-01-19 05:36:27
问题 I have a list of objects I need to sort according to properties of one of their fields. I've heard that SortedMap and Comparators are the best way to do this. Do I implement Comparable with the class I'm sorting, or do I create a new class? How do I instantiate the SortedMap and pass in the Comparator? How does the sorting work? Will it automatically sort everything as new objects are inserted? EDIT: This code is giving me an error: private TreeMap<Ktr> collection = new TreeMap<Ktr>(); (Ktr

Java: SortedMap, TreeMap, Comparable? How to use?

▼魔方 西西 提交于 2020-01-19 05:36:09
问题 I have a list of objects I need to sort according to properties of one of their fields. I've heard that SortedMap and Comparators are the best way to do this. Do I implement Comparable with the class I'm sorting, or do I create a new class? How do I instantiate the SortedMap and pass in the Comparator? How does the sorting work? Will it automatically sort everything as new objects are inserted? EDIT: This code is giving me an error: private TreeMap<Ktr> collection = new TreeMap<Ktr>(); (Ktr

sorting treemap based on key, where key is variable

不羁岁月 提交于 2020-01-13 08:32:08
问题 I want to sort the tree map based on the key where key is a variable,so sorting should be based on variable value, How can we achieve this? I want use in built sort method rathar implementing it through code, any reply with example is of great help. 回答1: TreeMap (which implements SortedMap) stores automatically the keys in the correct order: Map<Integer, String> map = new TreeMap<Integer, String>(); map.put(1, "one"); map.put(3, "three"); map.put(2, "two"); // prints one two three for(Integer