Differences between HashMap and Hashtable?

What are the differences between a HashMap and a Hashtable in Java? Which is more efficient for non-threaded applications? There are several differences between HashMap and Hashtable in Java: Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones. Hashtable does

HashMap和Hashtable的区别?

Java中的HashMap和Hashtable有什么区别? 哪种方法对非线程应用程序更有效? Java中的HashMap和Hashtable有几个区别: Hashtable是同步的,而HashMap不是。 这使得HashMap对于非线程应用程序更好,因为非同步对象通常比同步对象执行得更好。 Hashtable不允许null键或值。 HashMap允许一个null键和任意数量的null值。 其中一个HashMap的子类是LinkedHashMap ,所以如果您希望可预测的迭代顺序(默认为插入顺序),您

Proper use cases for Android UserManager.isUserAGoat()?

I was looking at the new APIs introduced in Android 4.2. While looking at the UserManager class I came across the following method: public boolean isUserAGoat() Used to determine whether the user making this call is subject to teleportations. Returns whether the user making this call is a goat. How and when should this be used? From their source , the method used to return false until i

适用于Android UserManager.isUserAGoat()的正确用例?

我正在研究Android 4.2中引入的新API。 在查看UserManager类时,我遇到了以下方法: public boolean isUserAGoat() 用于确定发起此呼叫的用户是否需要远程传送。 返回进行此调用的用户是否是山羊。 如何和何时应该使用? 从它们的源代码中 ,用于返回false的方法直到它在API 21中被更改。 /** * Used to determine whether the user making this call is subject to * teleportations. * @return whether the user

Why is (a*b != 0) faster than (a != 0 && b != 0) in Java?

I'm writing some code in Java where, at some point, the flow of the program is determined by whether two int variables, "a" and "b", are non-zero (note: a and b are never negative, and never within integer overflow range). I can evaluate it with if (a != 0 && b != 0) { /* Some code */ } Or alternatively if (a*b != 0) { /* Some code */ } Because I expect that

为什么(a * b!= 0)在Java中比(a!= 0 && b!= 0)更快?

我在Java中编写了一些代码,在某些时候,程序的流程由两个int变量“a”和“b”是否非零来确定(注意:a和b从不是负数,以及从不在整数溢出范围内)。 我可以用它评估它 if (a != 0 && b != 0) { /* Some code */ } 或者可选 if (a*b != 0) { /* Some code */ } 因为我希望每段代码运行数百万次,所以我想知道哪一个会更快。 我通过在一个巨大的随机生成的数组上进行比较来做了实验,我也很好奇看看数组的稀疏性(数据

Why is branch prediction faster than no branch at all?

Inspired by this question: Why is it faster to process a sorted array than an unsorted array? I wrote my own branch prediction experiment: public class BranchPrediction { public static void main(final String[] args) { long start; long sum = 0; /* No branch */ start = System.nanoTime(); sum = 0; for (long i = 0; i < 10000000000L; ++i)

为什么分支预测比没有分支更快?

受这个问题的启发:为什么处理一个有序数组比未经排序的数组更快? 我写了自己的分支预测实验: public class BranchPrediction { public static void main(final String[] args) { long start; long sum = 0; /* No branch */ start = System.nanoTime(); sum = 0; for (long i = 0; i < 10000000000L; ++i) sum += i; System.out.println(Syst

Stack vs Heap in C/Java

Here's my understanding. In C programming, if I do int a then that a is created on stack and thus the memory is taken from stack. Heap plays no part here. But if I do something like int *a; a=(int*)malloc(sizeof(int)); and dynamically allocate the memory, then the reference variable will be placed on stack, but the memory it points to will be on the heap. Am I correct with my underst

在C / Java中堆栈vs堆

这是我的理解。 在C编程中,如果我做int a那么在堆栈上创建a ,从而从栈中获取内存。 堆在这里不起作用。 但是,如果我做了类似的事情 int *a; a=(int*)malloc(sizeof(int)); 并动态分配内存,那么引用变量将被放置在堆栈上,但它指向的内存将位于堆上。 我的理解是否正确? 现在,我拿起了关于java的这本书 无论何时你需要一个对象,你只需编写代码来使用new来创建它,并且在代码执行时将存储分配到堆上。 所以

Java heap & stack

I want to study Java again, because I leave it some years ago. Reading a book I had a problem understanding how Java allocate memory in heap and in stack. This is what I've understood - I'll try to speak about it with examples. class TestA { int a; void methodA(int b) { a = b; } int getA() { return a; } } This is a sample class to show different

Java堆和堆栈

我想再次学习Java,因为我几年前就离开了它。 读一本书我有一个问题,就是理解Java如何在堆栈和堆栈中分配内存。 这就是我所理解的 - 我会试着用例子来谈论它。 class TestA { int a; void methodA(int b) { a = b; } int getA() { return a; } } 这是一个示例类,以显示不同的情况。 这是我的主要: int b = 3; TestA obj = new TestA(); obj.methodA(b); obj.getA(); 那么会发

Read/convert an InputStream to a String

If you have java.io.InputStream object, how should you process that object and produce a String ? Suppose I have an InputStream that contains text data, and I want to convert this to a String . For example, so I can write the contents of the stream to a log file. What is the easiest way to take the InputStream and convert it to a String ? public String convertStreamToString(InputStream is)

读取/转换InputStream为字符串

如果你有java.io.InputStream对象,你应该如何处理这个对象并产生一个String ? 假设我有一个包含文本数据的InputStream ,并且我想将它转换为一个String 。 例如,我可以将流的内容写入日志文件。 采用InputStream并将其转换为String的最简单方法是什么? public String convertStreamToString(InputStream is) { // ??? } 一个很好的方法是使用Apache commons IOUtils将InputStream复制到StringWriter ...类似于 S

Avoiding != null statements

I use object != null a lot to avoid NullPointerException . Is there a good alternative to this? For example: if (someobject != null) { someobject.doCalc(); } This avoids a NullPointerException , when it is unknown if the object is null or not. Note that the accepted answer may be out of date, see https://stackoverflow.com/a/2386013/12943 for a more recent approach. This to me sounds

避免!=空语句

我使用object != null来避免NullPointerException 。 有没有一个很好的选择呢? 例如: if (someobject != null) { someobject.doCalc(); } 这可以避免NullPointerException ,如果该对象为null或未知,则不会发生。 请注意,接受的答案可能已过时,请参阅https://stackoverflow.com/a/2386013/12943以获取更新的方法。 对我而言,这听起来像是一个相当普遍的问题,初级到中级开发人员往往会在某些时候面临这样的

=, *=, /= compound assignment operators require casting?

Until today, I thought that for example: i += j; is just a shortcut for: i = i + j; But what if we try this: int i = 5; long j = 8; Then i = i + j; will not compile but i += j; will compile fine. Does it mean that in fact i += j; is a shortcut for something like this i = (type of i) (i + j) ? As always with these questions, the JLS holds the answer. In this case §15.26.2 Compound As

=,* =,/ =复合赋值操作符需要转换吗?

直到今天,我认为,例如: i += j; 只是一个捷径: i = i + j; 但是,如果我们尝试这样做: int i = 5; long j = 8; 然后i = i + j; 不会编译,但i += j; 将编译好。 这是否意味着实际上i += j; 是这样的东西的捷径i = (type of i) (i + j) ? 像这些问题一样,JLS持有答案。 在这种情况下,§15.26.2复合赋值运算符。 摘录: E1 op= E2形式的复合赋值表达式等价于E1 = (T)((E1) op (E2)) ,其中T是E1的类型,只

Is Java "pass

I always thought Java was pass-by-reference . However, I've seen a couple of blog posts (for example, this blog) that claim it isn't. I don't think I understand the distinction they're making. What is the explanation? Java is always pass-by-value . Unfortunately, they decided to call the location of an object a "reference". When we pass the value of an object, we

是Java“通过

我一直认为Java是通过引用 。 不过,我见过一些博客文章(例如,这个博客),声称它不是。 我不认为我理解他们的区别。 什么是解释? Java始终是按值传递的 。 不幸的是,他们决定将对象的位置称为“参考”。 当我们传递一个对象的值时,我们将引用传递给它。 这对初学者来说很混乱。 它是这样的: public static void main(String[] args) { Dog aDog = new Dog("Max"); // we pass the object to foo foo