Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

consumer

package com.zetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class ConsumerEx4 {

    public static void main(String[] args) {

        var vals = new ArrayList<Integer>();
        vals.add(2);
        vals.add(4);
        vals.add(6);
        vals.add(8);

        Consumer<List<Integer>> addTwo = list -> {

            for (int i = 0; i < list.size(); i++) {

                list.set(i, 2 + list.get(i));
            }
        };

        Consumer<List<Integer>> showList = list -> 
            list.forEach(System.out::println);

        addTwo.andThen(showList).accept(vals);
    }
}
Comment

consumer

@FunctionalInterface
public interface Consumer<T> {
    void accept(T t);
}
Comment

consumer

package com.zetcode;

import java.util.function.Consumer;
import java.util.function.IntConsumer;

public class ConsumerEx2 {

    public static void main(String[] args) {

        Consumer<Integer> printMultiplyBy100 = (val) -> System.out.println(val * 100);

        printMultiplyBy100.accept(3);
        printMultiplyBy100.accept(4);
        printMultiplyBy100.accept(5);

        IntConsumer printMultiplyBy500 = a -> System.out.println(a * 50);
        printMultiplyBy500.accept(1);
        printMultiplyBy500.accept(2);
        printMultiplyBy500.accept(3);
    }
}
Comment

consumer

package com.zetcode;

import java.util.List;
import java.util.function.Consumer;

public class ConsumerEx3 {

    public static void main(String[] args) {

        var words = List.of("falcon", "wood", "rock", "forest",
                "river", "water");

        words.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {

                System.out.println(s);
            }
        });
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: reversing an integer with overflow handled 
Java :: Check if service running in the background 
Java :: @expose(serialize = false) not working 
Java :: java data structure interview questions 
Java :: java create a random number 
Java :: Java create an object of the static class Mammal 
Java :: how do you create a txt file in java 
Java :: a java program must have at least one of these: 
Java :: super class and concrete class in java 
Java :: retrofit gradle 
Java :: Fomating .json 
Java :: java get the closest pair to a given sum in two arrays 
Java :: adding matrix 
Java :: How tomake teris in Java 
Java :: how to use advanced math in java 
Java :: read properties file outside jar java 
Java :: java opengl get fps 
Java :: org.springframework.data.mapping.model.mappinginstantiationexception: failed to instantiate java.util.list using constructor no_constructor with arguments 
Java :: int a[ ]={4,8,3,2}; a[0] = 23; a[3]= a[1]; a[2]=12; for(int i=0; i<a.length; i++) System.out.println(a[i]); 
Java :: timer tick java 
Java :: longadder 
Java :: function compose method java 8 
Java :: spigot scoreboard objective 
Java :: string split to list java 
Java :: primefaces p:dataexporter encoding for pdf 
Java :: how to get the url after loading page in webview in andorid 
Java :: calling a method after the build method is run 
Java :: how does minus works in Java 
Java :: java jbutton trigger action 
Java :: java consonant regex 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =