Understanding Data Streams

Warm greetings! This lesson introduces data streams, which are essentially continuous datasets. Imagine a weather station or a gaming application gathering data every second — both generate data streams! We will learn how to handle these data streams using Java by accessing individual elements, slicing segments, and converting these streams into strings for easier handling.

Representing Data Streams in Java

In Java, data streams can be represented using collections such as ArrayList and Map. We will use a class to encapsulate operations related to these data streams in our Java application. Let's consider a simple Java class named DataStream:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

class DataStream {
    private ArrayList<Map<String, Integer>> data;

    public DataStream(ArrayList<Map<String, Integer>> data) {
        this.data = data;
    }
}

To use this, we create a sample data stream as an instance of our DataStream class, where each element is a map with key-value pairs:

class Program {
    public static void main(String[] args) {
        ArrayList<Map<String, Integer>> sampleData = new ArrayList<>();

        sampleData.add(new HashMap<>(Map.of("id", 1, "value", 100)));
        sampleData.add(new HashMap<>(Map.of("id", 2, "value", 200)));
        sampleData.add(new HashMap<>(Map.of("id", 3, "value", 300)));
        sampleData.add(new HashMap<>(Map.of("id", 4, "value", 400)));

        DataStream stream = new DataStream(sampleData);
    }
}
Accessing Elements — Key Operation

To look into individual elements of a data stream, we use zero-based indexing. The get method we introduce below fetches the i-th element from the data stream:

import java.util.ArrayList;
import java.util.Map;

class DataStream {
    private ArrayList<Map<String, Integer>> data;

    public DataStream(ArrayList<Map<String, Integer>> data) {
        this.data = data;
    }

    public Map<String, Integer> get(int i) {
        if (i >= 0 && i < data.size())
            return data.get(i);

        return null; // Returning null if index is out of bounds
    }
}

Here's how we can use the get method:

class Program {
    public static void main(String[] args) {
        ArrayList<Map<String, Integer>> sampleData = new ArrayList<>();

        sampleData.add(new HashMap<>(Map.of("id", 1, "value", 100)));
        sampleData.add(new HashMap<>(Map.of("id", 2, "value", 200)));
        sampleData.add(new HashMap<>(Map.of("id", 3, "value", 300)));
        sampleData.add(new HashMap<>(Map.of("id", 4, "value", 400)));

        DataStream stream = new DataStream(sampleData);

        Map<String, Integer> element = stream.get(2);
        if (element != null)
            System.out.println(element.get("id"));  // Output: 3

        element = stream.get(-1);
        System.out.println(element == null); // Output: true
    }
}

In essence, stream.get(2) fetches us the map with { "id": 3, "value": 300 }.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal