1.forEach方法##
public class CollectionForEach {
public static void main(String[] args){
List lists=new LinkedList();
lists.add("a");
lists.add("b");
lists.forEach(obj->System.out.println("显示: "+obj));
}
}
2.iterator遍历
public class IteratorTest {
public static void main(String[] args){
List lists=new LinkedList();
lists.add("a");
lists.add("b");
Iterator it=lists.iterator();
while(it.hasNext()){
String list=(String)it.next();
System.out.println(list);
}
}
}
3.Iterator的forEachRemaining方法
public static void main(String[] args){
List lists=new LinkedList();
lists.add("a");
lists.add("b");
Iterator it =lists.iterator();
it.forEachRemaining(obj->System.out.println("显示: "+obj));
}
4.for(Object obj : lists), 结果和上面其他方法不一样结果有大括号[a,b]
public class ForEachTest {
public static void main(String[] args){
List lists=new LinkedList();
lists.add("a");
lists.add("b");
for(Object obj : lists){
String list=(String)obj;
}
System.out.println(lists);
}
}
Iterator不可删除内容、也不能添加内容,否则报异常ConcurrentModificationException
网友评论