Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

functionality of consumer functional interface in java

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

functionality of consumer functional interface in java

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

functionality of consumer functional interface in java

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

functionality of consumer functional interface in java

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 :: countdown timer with seekbar 
Java :: enable GPS inside of application 
Java :: Copying value from one input field to another input field using checkbox 
Java :: textfield invisible java 
Java :: android stop audio playing by activity lifecycle 
Java :: java test coverage 
Java :: string to integer online 
Java :: guava-18.0.jar 
Java :: set maven goals in eclipse 
Java :: validate list of objects in javax validation 
Java :: Set value of input in android webview 
Java :: netbeans how to get string from jcombobox 
Java :: constraint layout not matching parent in netsted scrollview 
Java :: java platform runlater keeps running 
Java :: spring data rest id missing 
Java :: conexion a mysql java 
Java :: setting a hint on a text field in javafx 
Java :: skip list java 
Java :: jlabel text grösse 
Java :: resources/android/xml/network_security_config.xml 
Java :: BasicAWSCredentials 
Java :: java arraylist copy 
Java :: transpose array in java 
Java :: overwrite confguration value spring 
Java :: convert subarray to list java 
Java :: how to check that letter is not a number and is repeating or not in a sentence in java 
Java :: using ..replace() in jShell/java 
Java :: Merging 2 sorted arrays (edge cases2) 
Java :: number pattern in java 
Java :: kano magic wand 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =