zoukankan      html  css  js  c++  java
  • JDK1.8遍历方式

    一、for循坏

    public class ForList {
    
    	public static void main(String[] args) {
    		List<Integer> list = new ArrayList<Integer>();
    		list.add(1);
    		list.add(2);
    		list.add(3);
    		
    		for (int i = 0, length = list.size(); i < length; i++) {
    			System.out.println(list.get(i));
    		}
    
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    查看控制台输出:

    1
    2
    3
    
    • 1
    • 2
    • 3

    二、forEach循坏

    与for循坏相比,forEach循坏更加简洁明了。

    public class ForEachList {
    
    	public static void main(String[] args) {
    		List<Integer> list = new ArrayList<Integer>();
    		list.add(1);
    		list.add(2);
    		list.add(3);
    		
    		for (Integer it : list) {
    			System.out.println(it);
    		}
    
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    查看控制台输出:

    1
    2
    3
    
    • 1
    • 2
    • 3

    三、迭代器

    1、Iterator

    public class IteratorTest {
    
    	public static void main(String[] args) {
    		List<Integer> list = new ArrayList<Integer>();
    		list.add(1);
    		list.add(2);
    		list.add(3);
    		
    		Iterator<Integer> it = list.iterator();
    		while(it.hasNext()) {
    			System.out.println(it.next());
    		}
    
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    查看控制台输出:

    1
    2
    3
    
    • 1
    • 2
    • 3

    2、ListIterator
    Iterator的子类,只能用于List集合。

    public class ListIteratorTest {
    
    	public static void main(String[] args) {
    		List<Integer> list = new ArrayList<Integer>();
    		list.add(1);
    		list.add(2);
    		list.add(3);
    		
    		ListIterator<Integer> it = list.listIterator();
    		while(it.hasNext()) {
    			System.out.println(it.next());
    		}
    
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    查看控制台输出:

    1
    2
    3
    
    • 1
    • 2
    • 3

    四、Lambda表达式

    public class LambdaTest {
    
    	public static void main(String[] args) {
    		List<Integer> list = new ArrayList<>();
    		list.add(1);
    		list.add(2);
    		list.add(3);
    		
    		list.forEach(item -> {
    			System.out.println(item);
    		});
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    查看控制台输出:

    1
    2
    3
    
    • 1
    • 2
    • 3

    五、Iterator和ListIterator的介绍和区别

    凡是实现了Collection接口的集合类,都有一个iterator()方法,用于返回一个实现了Iterator接口的对象,用于遍历集合:

    • 使用next()获得序列中的下一个元素
    • 使用hasNext()检查序列中是否还有元素
    • 使用remove()将迭代器新近返回的元素删除

    有了Iterator就不必为集合中元素的数量操心了,那是由hashNext()和next()关心的事情。
    如果你只是向前遍历List,并不打算修改List对象本身,那么你可以看到forEach语法会显得更加简洁。
    Iterator还可以移除由next()产生的最后一个元素,这意味着在调用remove()之前必须先调用next()。

    List和Set都有iterator方法获得Iterator,但是对于List来说,也可以通过listIterator()来获取迭代器,ListIterator是Iterator的子类型,功能更加强大。

    区别:
    1、iterator()方法在Set和List接口中都有定义,但是listIterator()仅存在于list接口中(或实现类中);
    2、ListIterator和Iterator都有hasNext()和next()方法,可以实现顺序向后遍历,但是ListIterator可以通过hasPrevious()和previous()方法实现顺序向前移动;
    3、iterator()方法和listIterator()方法都可以产生一个指向List集合开始出的Iterator对象,但是listIterator(n)方法可以创建一个一开始就指向列表索引为n的元素出的ListIterator;
    4、ListIterator有add()方法,可以向List中添加对象,而Iterator不能;
    5、ListIterator可以定位当前的索引位置,nextIndex()和previousIndex()可以实现。Iterator没有此功能;
    6、ListIterator可以使用set()方法实现对List集合中元素的修改。Iterator仅能遍历,不能修改。

     default void forEach(Consumer<? super T> action) {
    2     Objects.requireNonNull(action);
    3     for (T t : this) {
    4         action.accept(t);
    5     }
    6 }

    实例:

    复制代码
    1 public static void main(String[] args) {
    2     List<String> list = new ArrayList<String>();
    3     list.add("张三");
    4     list.add("李四");
    5     list.add("王五");
    6     list.add("赵六");
    7     list.forEach(e -> System.out.println(e));
    8 }
    复制代码

    遍历Set

    方法:

    1 default void forEach(Consumer<? super T> action) {
    2     Objects.requireNonNull(action);
    3     for (T t : this) {
    4         action.accept(t);
    5     }
    6 }

    实例:

    复制代码
    1 public static void main(String[] args) {
    2     Set<String> set = new HashSet<String>();
    3     set.add("张三");
    4     set.add("李四");
    5     set.add("王五");
    6     set.add("赵六");
    7     set.forEach(e -> System.out.println(e));
    8 }
    复制代码

    遍历Map

    方法:

    复制代码
     1 default void forEach(BiConsumer<? super K, ? super V> action) {
     2     Objects.requireNonNull(action);
     3     for (Map.Entry<K, V> entry : entrySet()) {
     4         K k;
     5         V v;
     6         try {
     7             k = entry.getKey();
     8             v = entry.getValue();
     9         } catch(IllegalStateException ise) {
    10             // this usually means the entry is no longer in the map.
    11             throw new ConcurrentModificationException(ise);
    12         }
    13         action.accept(k, v);
    14     }
    15 }
    复制代码

    实例:

    复制代码
    1 public static void main(String[] args) {
    2     Map<Integer, String> map = new HashMap<Integer, String>();
    3     map.put(101, "张三");
    4     map.put(102, "李四");
    5     map.put(103, "王五");
    6     map.put(104, "赵六");
    7     map.forEach((key, value) -> System.out.println(key+"->"+value));
    8 }
    复制代码
     
  • 相关阅读:
    flex
    IOCP三:多个接收
    IOCP三:多个接收
    IOCP二:同时发送和接收
    IOCP二:同时发送和接收
    IOCP一:AcceptEx
    IOCP一:AcceptEx
    字符串和数字相互转换
    字符串和数字相互转换
    QThread应用详解
  • 原文地址:https://www.cnblogs.com/ketoli/p/13494658.html
Copyright © 2011-2022 走看看