Java Stream API : Finding the Longest Consecutive Sequence of Integers in a List

Finding the Longest Consecutive Sequence of Integers in a List Using Java Stream API


When working with Java, the Stream API provides a powerful and concise way to perform operations on collections. In this blog post, we'll explore how to use the Java Stream API to find the longest consecutive sequence of integers in a list.

Java Stream API : Finding the Longest Consecutive Sequence of Integers in a List
Java Stream API : Finding the Longest Consecutive Sequence of Integers in a List

Problem Statement

Given a list of integers, we want to find the longest consecutive sequence of integers. For example, given the input [100, 4, 200, 1, 3, 2], the longest consecutive sequence is [1, 2, 3, 4].

Approach

To solve this problem using the Stream API, we can follow these steps:

1. Convert the list of integers into a set to remove duplicates and enable fast lookup.
2. Iterate over each element in the set.
3. For each element, check if the previous integer (element - 1) exists in the set. If it does not exist, it means the current element is the start of a new consecutive sequence.
4. Continue checking consecutive integers until there are no more consecutive integers.
5. Keep track of the length of each consecutive sequence and return the length of the longest sequence found.

Implementation

Here's the Java code implementing the above approach:

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class LongestConsecutiveSequence {

    public static int findLongestConsecutiveSequence(List<Integer> nums) {
        Set<Integer> numSet = new HashSet<>(nums);
        int longestSeq = 0;

        for (int num : numSet) {
            if (!numSet.contains(num - 1)) {
                int currentNum = num;
                int currentSeq = 1;

                while (numSet.contains(currentNum + 1)) {
                    currentNum++;
                    currentSeq++;
                }

                longestSeq = Math.max(longestSeq, currentSeq);
            }
        }

        return longestSeq;
    }

    public static void main(String[] args) {
        List<Integer> nums = List.of(100, 4, 200, 1, 3, 2);
        int longestSeq = findLongestConsecutiveSequence(nums);
        System.out.println("Longest consecutive sequence length: " + longestSeq);
    }
}


In this implementation, we first convert the list of integers nums into a set numSet. We then iterate over each element in numSet. For each element, we check if the previous integer exists in numSet. If it does not, we start counting the consecutive sequence from that element. We continue counting until there are no more consecutive integers, updating longestSeq with the maximum sequence length found.

Conclusion

Using the Java Stream API, we can efficiently find the longest consecutive sequence of integers in a list. This approach demonstrates the power and simplicity of the Stream API for handling complex operations on collections in Java.

Post a Comment

Previous Post Next Post