Difference between Hashtable and Collections.synchronizedMap(HashMap)

As far as I know, java.util.Hashtable synchronizes each and every method in the java.util.Map interface, while Collections.synchronizedMap(hash_map) returns a wrapper object containing synchronized methods delegating calls to the actual hash_map (correct me if I am wrong). I have two questions : What difference does it make to synchronize each and every method and to have a wrapper class? Wh

Hashtable和Collections.synchronizedMap(HashMap)之间的区别

据我所知, java.util.Hashtable同步java.util.Map接口中的每个方法,而Collections.synchronizedMap(hash_map)返回一个包装器对象,其中包含将调用委托给实际hash_map同步方法(如果我我错了)。 我有两个问题: 它使同步每一种方法和有一个包装类有什么不同? 有什么情况可以选择一种吗? 当我们做Collections.synchronizedMap(hash_table)时会发生什么? 这是否等于简单地使用普通的java.util.Hashtable ? 以下是

ConcurrentHashMap and Hashtable in Java

This question already has an answer here: What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)? 18 answers ConcurrentHashMap uses multiple buckets to store data. This avoids read locks and greatly improves performance over a HashTable . Both are thread safe, but there are obvious performance wins with ConcurrentHashMap . When you read from a Concurrent

Java中的ConcurrentHashMap和Hashtable

这个问题在这里已经有了答案: ConcurrentHashMap和Collections.synchronizedMap(Map)有什么区别? 18个答案 ConcurrentHashMap使用多个桶来存储数据。 这样可以避免读锁,并大大提高了HashTable性能。 两者都是线程安全的,但ConcurrentHashMap有明显的性能优势。 当您使用get()从ConcurrentHashMap读取数据时,没有锁定,与所有操作都简单同步的HashTable相反。 HashTable是在老版本的Java中发布的,而ConcurrentH

Simplest and understandable example of volatile keyword in java

I'm reading about volatile keyword in Java and completely understand the theory part of it. But, what I'm searching for is, a good case example, which shows what would happen if variable wasn't volatile and if it were. Below code snippet doesn't work as expected ( from aioobe) class Test extends Thread { boolean keepRunning = true; public void run() { while

java中volatile关键字最简单易懂的例子

我正在阅读Java中的volatile关键字,并完全理解它的理论部分。 但是,我正在寻找的是一个很好的案例,它显示了如果变量不是不稳定的,如果是变化的话会发生什么。 下面的代码片段不能按预期工作(来自aioobe) class Test extends Thread { boolean keepRunning = true; public void run() { while (keepRunning) { } System.out.println("Thread terminated."); } public stat

HashMap Java 8 implementation

As per the following link document: Java HashMap Implementation I'm confused with the implementation of HashMap (or rather, an enhancement in HashMap ). My queries are: Firstly static final int TREEIFY_THRESHOLD = 8; static final int UNTREEIFY_THRESHOLD = 6; static final int MIN_TREEIFY_CAPACITY = 64; Why and how are these constants used? I want some clear examples for this. How they

HashMap Java 8实现

根据以下链接文档:Java HashMap实现 我对HashMap的实现感到困惑(或者说, HashMap的增强)。 我的查询是: 首先 static final int TREEIFY_THRESHOLD = 8; static final int UNTREEIFY_THRESHOLD = 6; static final int MIN_TREEIFY_CAPACITY = 64; 为什么以及如何使用这些常量? 我想要一些清晰的例子。 他们如何通过这种方式实现业绩增长? 其次 如果您在JDK中看到HashMap的源代码,您会发现以下静态内部类: s

Java: how to convert HashMap<String, Object> to array

I need to convert a HashMap<String, Object> to an array; could anyone show me how it's done? hashMap.keySet().toArray(); // returns an array of keys hashMap.values().toArray(); // returns an array of values Edit Should be noted that the ordering of both arrays may not be the same, See oxbow_lakes answer for a better approach for iteration when the pair key/values are needed. If

Java:如何将HashMap <String,Object>转换为数组

我需要将HashMap<String, Object>转换为数组; 任何人都可以告诉我它是如何完成的? hashMap.keySet().toArray(); // returns an array of keys hashMap.values().toArray(); // returns an array of values 编辑 需要注意的是,两个数组的排序可能不一样,请参阅oxbow_lakes答案,以便在需要配对键/值时更好地进行迭代。 如果你想要的键和值,你总是可以通过entrySet来做到这一点: hashMap.entrySet().toArray()

When to use Comparable and Comparator

I have a list of objects I need to sort on a field, say Score. Without giving much thought I wrote a new class that implements Comparator, that does the task and it works. Now looking back at this, I am wondering if I should have instead have the my class implement Comparable instead of creating a new class that implements Comparator. The score is the only field that the objects will be order

何时使用Comparable和Comparator

我有一个需要在字段上排序的对象列表,比如Score。 没有太多的想法,我写了一个实现比较器的新类,它完成任务并且工作。 现在回顾一下,我想知道是否应该让我的类实现Comparable,而不是创建一个实现Comparator的新类。 分数是对象将被排序的唯一字段。 我做了什么可以接受的做法? 是正确的方法“首先将类实现为Comparable(用于自然顺序),如果需要替代字段比较,则创建一个实现Comparator”的新类? 如果上面的(2)

ConcurrentHashMap and Hash table difference

This question already has an answer here: Differences between HashMap and Hashtable? 38 answers concurrentHashMap - Lock free algorithm. There is no synchronization between read or write operation. As per java Doc A hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates. This class obeys the same functional specification as Hashtable, and in

ConcurrentHashMap和Hash表的区别

这个问题在这里已经有了答案: HashMap和Hashtable的区别? 38个答案 concurrentHashMap - 锁定自由算法。 读或写操作之间没有同步。 按照java Doc 哈希表支持检索的完全并发和可更改的预期并发更新。 该类遵循与Hashtable相同的功能规范,并且包含与Hashtable的每种方法相对应的方法版本。 但是,即使所有操作都是线程安全的,检索操作也不需要锁定,并且也不支持以阻止所有访问的方式锁定整个表。 这个类可以在依

Java hashtable or hashmap?

This question already has an answer here: Differences between HashMap and Hashtable? 38 answers HashMap (or, more likely, HashSet ) is probably a good place to start, at your point. It's not perfect, and it does consume more memory than eg a list, but it should be your default when you need fast add, remove, and contains operations. The String.hashCode() implementation is not the best

Java哈希表或哈希表?

这个问题在这里已经有了答案: HashMap和Hashtable的区别? 38个答案 HashMap (或者更可能是HashSet )可能是一个很好的开始的地方。 这并不完美,它比列表消耗更多的内存,但是当您需要快速添加,删除和包含操作时,它应该是您的默认值。 String.hashCode()实现并不是最好的散列函数,尽管它很快,而且对于大多数目的来说足够好。 HashMap(&HashTable也是我认为的)的访问时间为O(1),因为Put()期间给定值的内

difference between hashmap and hashtable

Possible Duplicate: Differences between HashMap and Hashtable? I went to an interview the other day interviewer asked me under which situation will there be a problem to use hashmap rather then hashtable? Meaning give a eg where hashtmap use will result in problem but using hashtable will resolve the problem. He told me that the machine in which the code is run is single core!! I gave a

散列表和散列表之间的区别

可能重复: HashMap和Hashtable的区别? 前几天我采访了面试官,问我在哪种情况下会有问题使用hashmap而不是hashtable? 含义给出一个例如hashtmap的使用会导致问题,但使用散列表将解决问题。 他告诉我,代码运行的机器是单核心的! 我给了一个例如 Time Thread1 Thread 2 t0 tb.put("a",1) t1 tb.put("a",2) int a = tb.get("a"); 我告诉过,如果在t1时t1和t2同时

How is Hashtable different to Hashmap

Possible Duplicate: Differences between HashMap and Hashtable? I've seen hash tables and hash maps used in different code but they look like they do the same thing. Is there a difference between them? Which one should I use in my code? java.util.Hashtable methods are synchronized , java.util.Hashmap methods are not. If you use Hashtable there will be a performance hit as no two threa

哈希表如何与哈希表不同?

可能重复: HashMap和Hashtable的区别? 我看到了用于不同代码的哈希表和哈希映射,但它们看起来像是做同样的事情。 他们之间有区别吗? 我应该在我的代码中使用哪一个? java.util.Hashtable方法是同步的 , java.util.Hashmap方法不是。 如果你使用Hashtable ,会有性能问题,因为没有两个线程可以同时访问它的方法。 如果你关心应用程序中的线程安全性 ,那么Hashtable就是要走的路。 如果你不关心线程安全的Hashm