Lesson 1
Understanding Data Streams in Java
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:

Java
1import java.util.ArrayList; 2import java.util.HashMap; 3import java.util.Map; 4 5class DataStream { 6 private ArrayList<Map<String, Integer>> data; 7 8 public DataStream(ArrayList<Map<String, Integer>> data) { 9 this.data = data; 10 } 11}

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:

Java
1class Program { 2 public static void main(String[] args) { 3 ArrayList<Map<String, Integer>> sampleData = new ArrayList<>(); 4 5 sampleData.add(new HashMap<>(Map.of("id", 1, "value", 100))); 6 sampleData.add(new HashMap<>(Map.of("id", 2, "value", 200))); 7 sampleData.add(new HashMap<>(Map.of("id", 3, "value", 300))); 8 sampleData.add(new HashMap<>(Map.of("id", 4, "value", 400))); 9 10 DataStream stream = new DataStream(sampleData); 11 } 12}
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:

Java
1import java.util.ArrayList; 2import java.util.Map; 3 4class DataStream { 5 private ArrayList<Map<String, Integer>> data; 6 7 public DataStream(ArrayList<Map<String, Integer>> data) { 8 this.data = data; 9 } 10 11 public Map<String, Integer> get(int i) { 12 if (i >= 0 && i < data.size()) 13 return data.get(i); 14 15 return null; // Returning null if index is out of bounds 16 } 17}

Here's how we can use the get method:

Java
1class Program { 2 public static void main(String[] args) { 3 ArrayList<Map<String, Integer>> sampleData = new ArrayList<>(); 4 5 sampleData.add(new HashMap<>(Map.of("id", 1, "value", 100))); 6 sampleData.add(new HashMap<>(Map.of("id", 2, "value", 200))); 7 sampleData.add(new HashMap<>(Map.of("id", 3, "value", 300))); 8 sampleData.add(new HashMap<>(Map.of("id", 4, "value", 400))); 9 10 DataStream stream = new DataStream(sampleData); 11 12 Map<String, Integer> element = stream.get(2); 13 if (element != null) 14 System.out.println(element.get("id")); // Output: 3 15 16 element = stream.get(-1); 17 System.out.println(element == null); // Output: true 18 } 19}

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

Slicing — A Useful Technique

To fetch a range of elements, we can use slicing. Java offers methods like subList in ArrayList to achieve this. Here's how you might implement a slice method:

Java
1import java.util.ArrayList; 2import java.util.Map; 3 4class DataStream { 5 private ArrayList<Map<String, Integer>> data; 6 7 public DataStream(ArrayList<Map<String, Integer>> data) { 8 this.data = data; 9 } 10 11 public Map<String, Integer> get(int i) { 12 if (i >= 0 && i < data.size()) 13 return data.get(i); 14 15 return null; // Returning null if index is out of bounds 16 } 17 18 public ArrayList<Map<String, Integer>> slice(int i, int j) { 19 return new ArrayList<>(data.subList(i, j)); 20 } 21}

Here's a quick usage example:

Java
1class Program { 2 public static void main(String[] args) { 3 ArrayList<Map<String, Integer>> sampleData = new ArrayList<>(); 4 5 sampleData.add(new HashMap<>(Map.of("id", 1, "value", 100))); 6 sampleData.add(new HashMap<>(Map.of("id", 2, "value", 200))); 7 sampleData.add(new HashMap<>(Map.of("id", 3, "value", 300))); 8 sampleData.add(new HashMap<>(Map.of("id", 4, "value", 400))); 9 10 DataStream stream = new DataStream(sampleData); 11 12 ArrayList<Map<String, Integer>> slicedStream = stream.slice(1, 3); 13 for (Map<String, Integer> item : slicedStream) { 14 System.out.println(item.get("id")); // Output: 2, 3 15 } 16 } 17}
Transforming Data Streams to String — Another Key Operation

For better readability, we may want to convert our data streams into strings. In Java, we can utilize libraries like Gson to achieve this. Below is an example of implementing the toString method:

Java
1import com.google.gson.Gson; 2import java.util.ArrayList; 3import java.util.Map; 4 5class DataStream { 6 private ArrayList<Map<String, Integer>> data; 7 private Gson gson = new Gson(); 8 9 public DataStream(ArrayList<Map<String, Integer>> data) { 10 this.data = data; 11 } 12 13 public Map<String, Integer> get(int i) { 14 if (i >= 0 && i < data.size()) 15 return data.get(i); 16 17 return null; 18 } 19 20 public ArrayList<Map<String, Integer>> slice(int i, int j) { 21 return new ArrayList<>(data.subList(i, j)); 22 } 23 24 @Override 25 public String toString() { 26 return gson.toJson(this.data); 27 } 28}

To demonstrate:

Java
1class Program { 2 public static void main(String[] args) { 3 ArrayList<Map<String, Integer>> sampleData = new ArrayList<>(); 4 5 sampleData.add(new HashMap<>(Map.of("id", 1, "value", 100))); 6 sampleData.add(new HashMap<>(Map.of("id", 2, "value", 200))); 7 sampleData.add(new HashMap<>(Map.of("id", 3, "value", 300))); 8 sampleData.add(new HashMap<>(Map.of("id", 4, "value", 400))); 9 10 DataStream stream = new DataStream(sampleData); 11 12 System.out.println(stream.toString()); 13 // Output: [{"id":1,"value":100},{"id":2,"value":200},{"id":3,"value":300},{"id":4,"value":400}] 14 } 15}
Summary

In this lesson, we explored data streams and how to represent and manipulate them using Java collections like ArrayList. We crafted a class that encapsulates operations on data streams in Java, including accessing elements, slicing ranges, and converting streams to string representations.

Now it's time to apply your newfound knowledge in the practice exercises that follow!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.