Ways to iterate over a list in Java

Being somewhat new to the Java language I'm trying to familiarize myself with all the ways (or at least the non-pathological ones) that one might iterate through a list (or perhaps other collections) and the advantages or disadvantages of each. Given a List<E> list object, I know of the following ways to loop through all elements: Basic for loop (of course, there're equivalent w

在Java中迭代列表的方法

对于Java语言而言,我尝试熟悉所有可能通过列表(或者其他集合)迭代的方式(或者至少是非病态方法)以及每种方式的优缺点。 给定一个List<E> list对象,我知道以下方法循环遍历所有元素: 基本的for循环(当然,还有就是相当于while / do while循环以及) // Not recommended (see below)! for (int i = 0; i < list.size(); i++) { E element = list.get(i); // 1 - can call methods of element //

How to directly initialize a HashMap (in a literal way)?

This question already has an answer here: How can I initialise a static Map? 40 answers No, you will have to add all the elements manually. You can use a static initializer though: public class Demo { private static final Map<String, String> myMap; static { myMap = new HashMap<String, String>(); myMap.put("a", "b"); myMap.put("c", "d"); }

如何直接初始化一个HashMap(以字面的方式)?

这个问题在这里已经有了答案: 我怎样才能初始化一个静态地图? 40个答案 不,您将不得不手动添加所有元素。 你可以使用一个静态初始化器: public class Demo { private static final Map<String, String> myMap; static { myMap = new HashMap<String, String>(); myMap.put("a", "b"); myMap.put("c", "d"); } } 请注意,使用函数进行初始化将执行相同的操作,

How do I call one constructor from another in Java?

Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? And what could be the best way to call another constructor (if there are several ways to do it)? Yes, it is possible: public class Foo { private int x; public Foo() { this(1); } public Foo(int x) { this.x = x; } } To chain to a particular supercla

如何在Java中调用另一个构造函数?

是否有可能从另一个(在同一个类中,而不是从一个子类)调用构造函数? 如果是的话如何? 调用另一个构造函数的最佳方法是什么(如果有几种方法可以这样做)? 对的,这是可能的: public class Foo { private int x; public Foo() { this(1); } public Foo(int x) { this.x = x; } } 要链接到特定的超类构造函数而不是同一个类中的构造函数,请使用super而不是this 。 请注意,

Performance considerations for keySet() and entrySet() of Map

All, Can anyone please let me know exactly what are the performance issues between the 2? The site : CodeRanch provides a brief overview of the internal calls that would be needed when using keySet() and get(). But it would be great if anyone can provide exact details about the flow when keySet() and get() methods are used. This would help me understand the performance issues better. First

Map的keySet()和entrySet()的性能注意事项

所有, 任何人都可以让我知道2之间的性能问题究竟是什么? 该站点:CodeRanch提供了使用keySet()和get()时需要的内部调用的简要概述。 但是,如果任何人都可以在使用keySet()和get()方法时提供有关流程的确切细节,那将是非常好的。 这可以帮助我更好地理解性能问题。 首先,这完全取决于你正在使用哪种类型的地图。 但是,由于JavaRanch线程讨论HashMap,我假定这就是你所指的实现。 并且让我们假设您正在讨论

String Deduplication feature of Java 8

Since String in Java (like other languages) consumes a lot of memory because each character consumes two bytes, Java 8 has introduced a new feature called String Deduplication which takes advantage of the fact that the char arrays are internal to strings and final, so the JVM can mess around with them. I have read this example so far but since I am not a pro java coder, I am having a hard time

Java 8的字符串重复数据删除功能

由于Java中的String (与其他语言一样)会消耗大量内存,因为每个字符都消耗两个字节,所以Java 8引入了一个名为String Deduplication的新特性,它利用char数组在字符串和final内部这一事实,所以JVM可能会混淆它们。 到目前为止,我已经阅读过这个例子,但由于我不是专业的java编码人员,所以我很难理解这个概念。 这就是它所说的, 已经考虑了字符串复制的各种策略,但是现在实现的策略遵循以下方法:无论何时垃圾收集器

How can I hash a password in Java?

I need to hash passwords for storage in a database. How can I do this in Java? I was hoping to take the plain text password, add a random salt, then store the salt and the hashed password in the database. Then when a user wanted to log in, I could take their submitted password, add the random salt from their account information, hash it and see if it equates to the stored hash password with

我怎样才能在Java中散列密码?

我需要散列密码以存储在数据库中。 我怎样才能在Java中做到这一点? 我希望获取纯文本密码,添加一个随机盐,然后将salt和哈希密码存储在数据库中。 然后,当用户想要登录时,我可以提取他们提交的密码,从他们的账户信息中添加随机盐,对其进行散列,并查看它是否与存储的散列密码等同于他们的账户信息。 实际上,您可以使用内置于Java运行时的设施来执行此操作。 Java 6中的SunJCE支持PBKDF2,这是一种用于密码散列的

How can I generate random number in specific range in Android?

Possible Duplicate: Java: generating random number in a range I want to generate random number in a specific range. (Ex. Range Between 65 to 80) I try as per below code, but it is not very use full. It also returns the value greater then max. value(greater then 80). Random r = new Random(); int i1 = (r.nextInt(80) + 65); How can I generate random number in between range? Random r = ne

如何在Android中的特定范围内生成随机数?

可能重复: Java:在范围内生成随机数 我想要在特定范围内生成随机数。 (例如65至80之间的范围) 我尝试按照下面的代码,但它不是很充分的使用。 它也返回大于max的值。 值(大于80)。 Random r = new Random(); int i1 = (r.nextInt(80) + 65); 我如何在范围之间生成随机数? Random r = new Random(); int i1 = r.nextInt(80 - 65) + 65; 这给出了一个在65(含)和80(不含)之间的随机整数, 65,66,...,78,79 。

Getting random numbers in Java

Possible Duplicate: Java: generating random number in a range I would like to get a random value between 1 to 50 in Java. How may I do that with the help of Math.random(); ? How do I bound the values that Math.random() returns? import java.util.Random; Random rand = new Random(); int n = rand.nextInt(50) + 1; //50 is the maximum and the 1 is our minimum int max = 50; int min = 1; 1

用Java获取随机数

可能重复: Java:在范围内生成随机数 我想在Java中获得1到50之间的随机值。 我怎样才能在Math.random();的帮助下做到这一点Math.random(); ? 如何限制Math.random()返回的值? import java.util.Random; Random rand = new Random(); int n = rand.nextInt(50) + 1; //50 is the maximum and the 1 is our minimum int max = 50; int min = 1; 1.使用Math.random() double random = Math.random() * 49 + 1

How to generate a random alpha

I've been looking for a simple Java algorithm to generate a pseudo-random alpha-numeric string. In my situation it would be used as a unique session/key identifier that would "likely" be unique over 500K+ generation (my needs don't really require anything much more sophisticated). Ideally, I would be able to specify a length depending on my uniqueness needs. For example, a ge

如何生成一个随机的阿尔法

我一直在寻找一种简单的Java算法来生成一个伪随机字母数字字符串。 在我的情况下,它将被用作一个独特的会话/密钥标识符,“很可能”在500K +一代中是唯一的(我的需求并不需要任何更复杂的东西)。 理想情况下,我可以根据我的独特需要指定长度。 例如,生成的长度为12的字符串可能看起来像"AEYGF7K0DM1X" 。 算法 要生成一个随机字符串,将从可接受符号集合中随机抽取的字符连接起来,直到字符串达到所需的长度

Benefits of creating a List using Arrays.asList()

This question already has an answer here: Why does Arrays.asList() return its own ArrayList implementation 6 answers Not being able to call add, remove, etc is the exact difference. If you don't need those methods, Arrays.asList gives you a perfectly fine view of the array as a List (for APIs that take collections rather than arrays). If you need to change the "shape" of the l

使用Arrays.asList()创建列表的好处

这个问题在这里已经有了答案: 为什么Arrays.asList()返回自己的ArrayList实现6个答案 不能调用添加,删除等是完全不同的。 如果你不需要这些方法,Arrays.asList可以让你完美地将数组视为一个List(对于采用集合而不是数组的API)。 如果你需要改变列表的“形状”,那么新的ArrayList <>(Arrays.asList(myArray))是要走的路。