我如何迭代一个集/HashSet没有以下?
Iterator iter = set.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
我如何迭代一个集/HashSet没有以下?
Iterator iter = set.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
当前回答
枚举(?):
Enumeration e = new Vector(set).elements();
while (e.hasMoreElements())
{
System.out.println(e.nextElement());
}
另一种方法(java.util.Collections.enumeration()):
for (Enumeration e1 = Collections.enumeration(set); e1.hasMoreElements();)
{
System.out.println(e1.nextElement());
}
Java 8:
set.forEach(element -> System.out.println(element));
or
set.stream().forEach((elem) -> {
System.out.println(elem);
});
其他回答
枚举(?):
Enumeration e = new Vector(set).elements();
while (e.hasMoreElements())
{
System.out.println(e.nextElement());
}
另一种方法(java.util.Collections.enumeration()):
for (Enumeration e1 = Collections.enumeration(set); e1.hasMoreElements();)
{
System.out.println(e1.nextElement());
}
Java 8:
set.forEach(element -> System.out.println(element));
or
set.stream().forEach((elem) -> {
System.out.println(elem);
});
您可以使用函数式操作来获得更简洁的代码
Set<String> set = new HashSet<String>();
set.forEach((s) -> {
System.out.println(s);
});
至少有六种方法可以遍历一个集合。我了解到以下情况:
方法1
// Obsolete Collection
Enumeration e = new Vector(movies).elements();
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}
方法2
for (String movie : movies) {
System.out.println(movie);
}
方法3
String[] movieArray = movies.toArray(new String[movies.size()]);
for (int i = 0; i < movieArray.length; i++) {
System.out.println(movieArray[i]);
}
方法4
// Supported in Java 8 and above
movies.stream().forEach((movie) -> {
System.out.println(movie);
});
方法5
// Supported in Java 8 and above
movies.stream().forEach(movie -> System.out.println(movie));
方法6
// Supported in Java 8 and above
movies.stream().forEach(System.out::println);
这是我在例子中使用的HashSet:
Set<String> movies = new HashSet<>();
movies.add("Avatar");
movies.add("The Lord of the Rings");
movies.add("Titanic");
为了演示,考虑下面的集合,它包含不同的Person对象:
Set<Person> people = new HashSet<Person>();
people.add(new Person("Tharindu", 10));
people.add(new Person("Martin", 20));
people.add(new Person("Fowler", 30));
人物模型类
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//TODO - getters,setters ,overridden toString & compareTo methods
}
for语句有一种设计用于通过集合和数组进行迭代的形式,这种形式有时被称为增强的for语句,可用于使循环更加紧凑和易于阅读。
(人p:人){ System.out.println (p.getName ()); }
Java 8 - Java .lang. iterable . foreach(消费者)
人。forEach(p -> System.out.println(p. getname ()));
default void forEach(Consumer<? super T> action)
Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of iteration (if an iteration order is specified). Exceptions thrown by the action are relayed to the caller. Implementation Requirements:
The default implementation behaves as if:
for (T t : this)
action.accept(t);
Parameters: action - The action to be performed for each element
Throws: NullPointerException - if the specified action is null
Since: 1.8
你可以使用一个增强的for循环:
Set<String> set = new HashSet<String>();
//populate set
for (String s : set) {
System.out.println(s);
}
或者使用Java 8:
set.forEach(System.out::println);