Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Java Stream

// Creates a list of ProgrammingLanguage objects,
// maps it with a method of ProgrammingLanguage to get the name
// and filters them for programming languages, which are not starting with C.


import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;


class ProgrammingLanguage {

    private String name = "";

    public static void main(final String args[]) {
        List<ProgrammingLanguage> programmingLanguages = Arrays.asList(
                new ProgrammingLanguage("Java"),
                new ProgrammingLanguage("Python"),
                new ProgrammingLanguage("C#"),
                new ProgrammingLanguage("JS"),
                new ProgrammingLanguage("C++"),
                new ProgrammingLanguage("C"));
        List<String> nonCProgrammingLanguages = programmingLanguages.stream()  // creates stream from list
                .map(ProgrammingLanguage::getName)
                .filter(name -> !name.startsWith("C"))
                .collect(Collectors.toList());  // convert stream back to list
        System.out.println("Names not starting with C: " + nonCProgrammingLanguages);
    }


    public ProgrammingLanguage(final String name) {
        setName(name);
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }
}
Source by howtodoinjava.com #
 
PREVIOUS NEXT
Tagged: #Java #Stream
ADD COMMENT
Topic
Name
2+9 =