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

ClassNotFoundException vs NoClassDefFoundError
ClassNotFoundException NoClassDefFoundError It is an exception and happens due to programmer’s mistake and can be recovered by updating the code. Thrown when an application tries...

How to create Rest web services in Java
Web services are web application components that lets two different applications to communicate over the network.Let if an application which in written java provides web services can be communicated through...

Design Pattern
Prototype Design Pattern: Ø Prototype pattern refers to creating duplicate object while keeping performance in mind. Ø This pattern involves implementing a prototype interface which tells...

Java :: Inner Class Concepts
Java:: Inner Class Concepts--------------------------------- Inner class concepts introduced in Java v1.1 - as a part of AWT Event Handling improvements.When to use Inner class: - Without existing One...
M

Marimuthu P.

2 0
0

Understanding concept of Inheritance in Java
Inheritance: Acquiring a properties and methods from Parent class to Child class. For example, In some cases we can hear from any one of your relative or your friends will say like that "You are bahaving...
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