lambda - foreach

邹浩
2023-12-01

lambda - foreach

lambda 语法格式:

(parameters) -> expression

(parameters) -> {statements;}

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

        Date now = new Date();
        // foreach 遍历
        for (Map.Entry<String, Integer> 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<String></>
     */
    public static void testList() {
        List<String> 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<K,V>

   /**
     * 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}:
     * <pre> {@code
     * for (Map.Entry<K, V> entry : map.entrySet())
     *     action.accept(entry.getKey(), entry.getValue());
     * }</pre>
     *
     * 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<? super K, ? super V> action) {
        Objects.requireNonNull(action);
        for (Map.Entry<K, V> 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<T>

    /**
     * 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
     * <p>The default implementation behaves as if:
     * <pre>{@code
     *     for (T t : this)
     *         action.accept(t);
     * }</pre>
     *
     * @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<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }

 

 

 

 

 

 

 类似资料: