How do I use sorted method in Java Stream API?

In the Java Stream API, the sorted() method is used to sort elements in a stream. The sorted() method returns a stream consisting of the elements of the original stream, sorted according to natural order. If the elements of the stream are not Comparable, a java.lang.ClassCastException may be thrown when the terminal operation is executed.

Take a look at this example:

package org.kodejava.stream;

import java.util.stream.Stream;

public class SortedString {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("d", "a", "b", "c", "e");
        stream.sorted().forEach(System.out::println);
    }
}

In this code, we create a stream of String objects and sort it using the sorted() operation. The forEach method is a terminal operation that processes the sorted stream.

If you would like to sort objects of a custom class, you may need to supply your own comparator:

package org.kodejava.stream;

import java.util.Comparator;
import java.util.stream.Stream;

public class SortedCustomComparator {

    public static void main(String[] args) {
        Stream<User> usersStream = Stream.of(
                new User("John", 30),
                new User("Rosa", 25),
                new User("Adam", 23));

        usersStream
                .sorted(Comparator.comparing(User::getAge))
                .forEach(System.out::println);
    }

    static class User {
        String name;
        int age;

        User(String name, int age) {
            this.name = name;
            this.age = age;
        }

        String getName() {
            return name;
        }

        int getAge() {
            return age;
        }

        @Override
        public String toString() {
            return "User{" + "name='" + name + '\'' + ", age=" + age + '}';
        }
    }
}

In this case, the sorted() method takes a Comparator argument, which is created using a lambda function. This comparator compares User objects by their ages.

In Java Stream API, you can use the sorted() and limit() methods together, but their ordering impacts performance. The sorted() method sorts all the elements in the stream, whereas limit(n) shortens the stream to be no longer than n elements in length.

If you call limit() before sorted(), like:

stream.limit(10).sorted()

The operation will only sort the first 10 elements from the stream.

But if you call sorted() before limit(), like:

stream.sorted().limit(10)

The operation will sort the entire stream, which may be much larger and more time-consuming, and then cut down the result to only keep the first 10 items.

So, if your task is to ‘find the smallest (or largest) n elements’, it is more efficient to first sort the stream and then limit it. If you want to ‘sort the first n elements’, you should limit the stream first and then sort it.

How do I use limit method in Java Stream API?

The limit(long maxSize) method in Java’s Stream API is used for reducing the size of the stream. It takes a single parameter, maxSize, which is a long value that represents the maximum number of elements that the stream should be limited to.

The primary purpose and usefulness of the limit() method can be summarized as follows:

  1. Short-circuit Operation: It provides a way to work with infinite streams. Even if your stream is infinite, using limit() allows you to get a finite number of elements.

  2. Performance Enhancement: Since limit() short-circuits the stream, it can significantly improve performance by reducing the number of operations performed, especially in large streams.

  3. Control Stream Size: The limit() method allows you to reduce the number of elements in the stream according to your needs without changing the original data source.

Here is a simple example of how to use it:

package org.kodejava.stream;

import java.util.stream.*;

public class StreamLimit {
    public static void main(String[] args) {
        Stream<Integer> numbersStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
        numbersStream
                .limit(4)
                .forEach(System.out::println);
    }
}

Output:

1
2
3
4

In this code, we have a stream of nine numbers, but we are limiting this stream to just the first four elements, so only the numbers 1 to 4 are displayed on the console.

Please note that if the size of this stream is smaller than the maxSize then the same amount of stream will be returned. If the size of the stream is greater than the maxSize then the size of the stream will be maxSize.

How do I use Stream.generate() method?

The Stream.generate() method in Java is used to create an infinite stream of data, typically used when the programmer needs a limitless supply of data to be processed.

Here’s an example of how you might use Stream.generate():

package org.kodejava.util;

import java.util.stream.Stream;

public class StreamGenerate {
    public static void main(String[] args) {
        Stream<String> stringStream = Stream.generate(() -> "Hello, World!");

        stringStream
                .limit(5)
                .forEach(System.out::println);
    }
}

The output of this code snippet is:

Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!

In this example, Stream.generate() is used to create an infinite stream of the String "Hello, World!". The limit(5) method is used to limit the infinite stream to just the first five elements, and forEach(System.out::println) is used to print each of the first five elements to the console.

However, be careful while using Stream.generate() without limit() as it can lead to infinite loop. Generally, limit() is used with Stream.generate() to avoid this.

Here’s how you would use it with the Random class to generate an infinite stream of random numbers:

package org.kodejava.util;

import java.util.Random;
import java.util.stream.Stream;

public class StreamGenerateRandomNumber {
    public static void main(String[] args) {
        Stream<Integer> randomNumbers = Stream.generate(new Random()::nextInt);

        randomNumbers
                .limit(10)
                .forEach(System.out::println);
    }
}

This will output something like:

-2134800739
730041861
357210260
1964364949
-1083197494
-1988345642
-1851656161
-562751737
-1777126198
-1030758565

In this case, new Random()::nextInt is a supplier function that provides an infinite stream of random integers. The limit(10) method is used to limit the stream to the first 10 random integers.

How do I limit MySQL query result?

package org.kodejava.jdbc;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SqlLimitExample {
    private static final String URL = "jdbc:mysql://localhost/kodejava";
    private static final String USERNAME = "kodejava";
    private static final String PASSWORD = "s3cr*t";

    public static void main(String[] args) {
        try (Connection connection =
                     DriverManager.getConnection(URL, USERNAME, PASSWORD)) {

            // Create PreparedStatement to get all data from a database.
            String query = "select count(*) from product";
            PreparedStatement ps = connection.prepareStatement(query);
            ResultSet result = ps.executeQuery();

            int total = 0;
            while (result.next()) {
                total = result.getInt(1);
            }

            System.out.println("Total number of data in database: " +
                               total + "\n");

            // Create PreparedStatement to the first 5 records only.
            query = "select * from product limit 5";
            ps = connection.prepareStatement(query);
            result = ps.executeQuery();

            System.out.println("Result fetched with specified limit 5");
            System.out.println("====================================");
            while (result.next()) {
                System.out.println("id:" + result.getInt("id") +
                                   ", code:" + result.getString("code") +
                                   ", name:" + result.getString("name") +
                                   ", price:" + result.getString("price"));
            }

            // Create PreparedStatement to get data from the 4th
            // record (remember the first record is 0) and limited
            // to 3 records only.
            query = "select * from product limit 3, 3";
            ps = connection.prepareStatement(query);
            result = ps.executeQuery();

            System.out.println("\nResult fetched with specified limit 3, 3");
            System.out.println("====================================");
            while (result.next()) {
                System.out.println("id:" + result.getInt("id") +
                                   ", code:" + result.getString("code") +
                                   ", name:" + result.getString("name") +
                                   ", price:" + result.getString("price"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

An example result of our program is:

Total number of data in database: 9

Result fetched with specified limit 5
====================================
id:1, code:P0000001, name:UML Distilled 3rd Edition, price:25.00
id:3, code:P0000003, name:PHP Programming, price:20.00
id:4, code:P0000004, name:Longman Active Study Dictionary, price:40.00
id:5, code:P0000005, name:Ruby on Rails, price:24.00
id:6, code:P0000006, name:Championship Manager, price:0.00

Result fetched with specified limit 3, 3
====================================
id:5, code:P0000005, name:Ruby on Rails, price:24.00
id:6, code:P0000006, name:Championship Manager, price:0.00
id:7, code:P0000007, name:Transport Tycoon Deluxe, price:0.00

Maven Dependencies

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.1.0</version>
</dependency>

Maven Central

How do I limit Hibernate query result?

In the example below you’ll see how to limit the number of records returned by hibernate queries. Limiting the query result is usually use for creating pagination, where we can navigate from page to page of data in our application but only a few of them are read from the database.

In hibernate Query object we need to specify the first result and max results by calling the setFirstResult() and setMaxResults() methods to limit the query results.

package org.kodejava.hibernate.service;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.kodejava.hibernate.SessionFactoryHelper;
import org.kodejava.hibernate.model.Label;

import java.util.List;

public class LabelService {
    public List<Label> getLabels(int pageNumber, int pageSize) {
        Session session =
                SessionFactoryHelper.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Query<Label> query = session.createQuery("from Label", Label.class);

        // Set the first record position and the max number of record to be
        // read. The setFirstResult() tell hibernate from which row the data
        // should be read. In the example if we have pages of 10 records,
        // passing the page number 2 will read 10 records from the 20th row
        // in the selected records.
        query.setFirstResult((pageNumber - 1) * pageSize);
        query.setMaxResults(pageSize);

        List<Label> labels = query.list();
        session.getTransaction().commit();
        return labels;
    }
}
package org.kodejava.hibernate;

import org.kodejava.hibernate.model.Label;
import org.kodejava.hibernate.service.LabelService;

import java.util.List;

public class LimitDemo {
    public static void main(String[] args) {
        LabelService service = new LabelService();

        List<Label> labels = service.getLabels(1, 10);
        for (Label label : labels) {
            System.out.println("Label = " + label);
        }
    }
}

Maven Dependencies

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.6.9.Final</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.0.33</version>
    </dependency>
</dependencies>

Maven Central Maven Central