UrbanPro
true

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Java8 Filters and collectors

Shiva Kumar
10/10/2018 0 0

Lets say we have collection of strings and we would like to filter  (remove) out certain strings from collection. We could achive the same in java 7 and earlier versions

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BeforeJava8 {

    public static void main(String[] args) {

        List lines = Arrays.asList("abc","shiva", "xyz");
        List result = getFilterOutput(lines, "shiva");
        for (String temp : result) {
            System.out.println(temp);    //output : abc, xyz
        }

    }

    private static List getFilterOutput(List lines, String filter) {
        List result = new ArrayList<>();
        for (String line : lines) {
            if (!"shiva".equals(line)) { 
                result.add(line);
            }
        }
        return result;
    }

}

The same can be achieved in java 8.

import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class NowJava8 { public static void main(String[] args) { List lines = Arrays.asList("abc", "xyz", "shiva"); List result = lines.stream() .filter(line -> !"shiva".equals(line)) .collect(Collectors.toList()); result.forEach(System.out::println); //output : abc,xyz } }
So, what have we done here, we have used java 8 for filtering the collections and using lambda and reduced the number of lines to do the same.

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Java: A Quick Overview
Not purely Object Oriented: It doesn't support multiple inheritence, it supports primitive data types and static members. Doesn’t support multiple inheritance: Reason is diamond problem i.e.,...

JAVA Online Training
What is Java? Java is a technology developed by James Gosling at Sun Microsystems, Sun Microsystems is now a part of Oracle Corporation. This is one of the most powerful & securable language used to...

How can everyone prepare to clear any Java interview?
Java interview your java should be much strong then J2EE. core java and Advance java is the basic foundation for Interview. Some of the topic about which you should know before going for a java interview...

Comparable vs Comparator
java.lang.Comparable java.util.Comparator For comparing some other object with its own object. Ex. I am comparing myself to some other employee. Method signature is: int compareTo (T object). For...

Java: A Quick Overview
Not purely Object Oriented: It doesn't support multiple inheritence, it supports primitive data types and static members. Doesn’t support multiple inheritance: Reason is diamond problem i.e.,...
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more