lambda - foreach

lambda - foreach

lambda 语法格式:

(parameters) -> expression

(parameters) -> {statements;}

    /**
     * Map
     */
    public static void testMap() {
        Map map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 2);
        map.put("c", 3);

        Date now = new Date();
        // foreach 遍历
        for (Map.Entry entry : map.entrySet()) {
            System.out.println("key -> " + entry.getKey() + " value -> " + entry.getValue());
        }
        Date end = new Date();
        System.out.println(end.getTime() - now.getTime());
        System.out.println("--------------------");
        Date now2 = new Date();
        // lambda foreach 遍历
        map.forEach((k, v) -> System.out.println("key -> " + k + " value -> " + v));
        Date end2 = new Date();
        System.out.println(end2.getTime() - now2.getTime());
    }

 

    /**
     * List
     */
    public static void testList() {
        List list = new ArrayList<>();
        list.add("a");
        list.add("v");
        list.add("b");
        // foreach 遍历
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("--------------------");
        // lambda foreach 遍历
        list.forEach(iem -> System.out.println(iem));
        list.forEach(System.out::println);
    }

jdk 1.8  Map

   /**
     * Performs the given action for each entry in this map until all entries
     * have been processed or the action throws an exception.   Unless
     * otherwise specified by the implementing class, actions are performed in
     * the order of entry set iteration (if an iteration order is specified.)
     * Exceptions thrown by the action are relayed to the caller.
     *
     * @implSpec
     * The default implementation is equivalent to, for this {@code map}:
     * 
 {@code
     * for (Map.Entry entry : map.entrySet())
     *     action.accept(entry.getKey(), entry.getValue());
     * }
* * The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. * * @param action The action to be performed for each entry * @throws NullPointerException if the specified action is null * @throws ConcurrentModificationException if an entry is found to be * removed during iteration * @since 1.8 */ default void forEach(BiConsumer action) { Objects.requireNonNull(action); for (Map.Entry entry : entrySet()) { K k; V v; try { k = entry.getKey(); v = entry.getValue(); } catch(IllegalStateException ise) { // this usually means the entry is no longer in the map. throw new ConcurrentModificationException(ise); } action.accept(k, v); } }

jdk 1.8  List

    /**
     * Performs the given action for each element of the {@code 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.
     *
     * @implSpec
     * 

The default implementation behaves as if: *

{@code
     *     for (T t : this)
     *         action.accept(t);
     * }
* * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */ default void forEach(Consumer action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } }

 

 

 

 

 

 

你可能感兴趣的:(java,java,lambda)