DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Introduction to Java 8 Streams

Introduction to Java 8 Streams

Use Java 8's Streams to handle multi-core processors and use multiple threads to process your data.

Siva Prasad Rao Janapati user avatar by
Siva Prasad Rao Janapati
·
Apr. 10, 16 · Java Zone · Tutorial
Like (8)
Save
Tweet
9.98K Views

Join the DZone community and get the full member experience.

Join For Free

In Java 8, we have a new feature called “Streams.” These “Streams” are similar to collections, but there are some differences. Primarily the collections are the data structures which will hold the data. To process the data, we need to get the data from the collections and execute some logic. For example, we have a list of students. We need to sort the students based on the marks secured, in descending order. To perform the above-said logic, we have to follow these steps.

Create a Comparator

package org.smarttechie;

import java.util.Comparator;

public class MarksComparator implements Comparator<Student>{

@Override
public int compare(Student student1, Student student2) {

   if (student1.getTotalMarks() > student2.getTotalMarks()) {
       return -11;
    } else if (student1.getTotalMarks() < student2.getTotalMarks()) {
      return 1;
    } else {
      return 0;
   }
  }
}

Call the Method

 Collections.sort(students, new MarksComparator());

With Streams we can do this in a simple way. The code snippet is given below.

List<String> studentRollNumbers = students.parallelStream().sorted()
                                  .map(Student::getRollNumber)
                                  .collect(Collectors.toList());

Another use case is where we want to get the student whose name is “ABC_4”. If you use the collections approach, we need to iterate the list and compare each student name with “ABC_4”. With the Streams can do it very simply:

boolean studentFound = students.parallelStream()
                                   .anyMatch(t -> t.getStudentName()
                                   .equalsIgnoreCase("ABC_4"));

If you look at the above code, the stream has the flow of data and processes the data on the fly to generate the result. Here, each method acting on a stream is a pipeline. The output of the one pipeline becomes input to the other pipeline. You can get the stream by calling stream() or parallelStream(). If you have a multi-core processor system and want to utilize the CPU, then you can use parallelStream(), which will use multiple threads to process the data. As a developer, you don't need to write multi-thread programming.

The sample code used in this article is available here.

Stream (computing) Java (programming language)

Published at DZone with permission of Siva Prasad Rao Janapati, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Comprehensive Guide to Jenkins Declarative Pipeline [With Examples]
  • Ultra-Fast Microservices: When Microstream Meets Wildfly
  • Create a Self-Service Customer Support Chatbot Without Code
  • Types of UI Design Patterns Depending on Your Idea

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo