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

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...

Easy way to remember Java keyword.
ACCESS MODIFIER ACCESS SPECIFIER abstract, assert, const, final, native, static, strictfp, super, synchronized, this, transient, void, volatile public, private, protected, default DATA...

Rohit Deshbhratar

1 0
0

Why we need to learn Programming languages?
Language is medium for communication. If two parties like to communicate or exchange the thoughts they must know a language. Language should be understandable by both the Parties. For example A wants to...

Demand of java in industry
Java is one of the widest use language in the industry all around the world As in software market more than 80% software, platform developed using java. Java comes in three flavours:- j2se for window based...

Prashant Tomer

0 0
0

GIT
In software engineering, software configuration management (SCM) is the task of tracking and controlling changes in the software, part of the larger cross-disciplinary field of configuration management....
R
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