1-n-list-in-java

How to Generate List from 1 to N in Java

There are multiple ways to generate the list from 1 to N or M to N in Java. We will look at 3 with various versions of Java. Using Simple For Loop Just iterate from 1 to N in for a loop and add it to the list. int n = 10; List<Integer> list = new ArrayList<>(n); // 1 to n (inclusive) for (int i = 1; i <= n; i++) { list.add(i); } Using IntStream - Java 8 Using IntStream鈥檚 range for end exclusive and rangeClosed for end inclusive. Here is how. ...

October 15, 2023 路 1 min 路 Imran Shaikh
How to Select Specific Folders or Files As Input in Flink

How to Select Specific Folders or Files As Input in Flink

Hey, tea lovers! Today I will show you how to specifically select the desired folders or files in Flink Batch Job programmatically. We will discuss when given a root path you can decide which file or folder to read in Flink Batch Job. And why suddenly do I pick up this topic you say? Well, I was working on such a task myself where I needed to read the S3 files which are under folders with dates. Those date folders are in a rolling fashion like , 2021_01_13_1, 2021_01_13_2, 2021_01_13_3 and so on. And in these folders, there were multiple files, so based on the given date I needed to read all those files which are in the given date folder (the folder name should contain the date obviously). ...

February 20, 2021 路 7 min 路 Imran Shaikh
How to Use partitioningBy Collector in Java Stream API

How to Use partitioningBy Collector in Java Stream API

Hey, tea lovers! Today I will be talking about the partitioningBy Collector method of Stream API. I will be focusing on what is partitioningBy by method, where, and how to use it with different examples. I have already discussed How to use groupingBy Collector in Java Streams. Both partitioningBy and groupingBy are the java.util.stream.Collectors. partitioningBy is a terminal operation of the Stream API pipeline. ...

January 22, 2021 路 5 min 路 Imran Shaikh
How to use groupingBy Collector in Java Streams

How to use groupingBy Collector in Java Streams

Hey, Tea lovers! Today we will talk about the groupingBy Collector method of Java Stream API. Yes, it is similar to GROUP BY of SQL since it groups and collects the objects based on a given condition or value. In case you want to get familiar with the Stream API, I recommend you to read the post " Be More Functional with Java鈥檚 Functional Interfaces" and " Stream API: The Hero Without a Cape". These will help you understand the post and might refresh your memory if you already know. Prepare your tea then, to sip and code. ...

October 15, 2020 路 5 min 路 Imran Shaikh
Stream API The Hero Without a Cape

Stream API: The Hero Without a Cape

Hey Tea Lovers! In this post, we will be talking about an unsung hero of the Java world, the Stream API which was added to Java 8 and has changed the way we do programs in java and how! It added neatness as well as made the program more readable. It helps you do functional programming in Java. Without any further ado, let us read more about this jewel. What is Stream API The Stream API, included in Java 8, is utilized for processing聽collections of objects. It is the flow of Objets on which various methods get applied in the pipeline to have the result. Simply put, it flows through the given聽Collection<Object>聽and applies the different methods on the聽Object聽to aggregate them into the desired result without affecting the original聽Object. ...

May 11, 2020 路 9 min 路 Imran Shaikh
Functional Interfaces To Become More Functional in Java

Functional Interfaces in Java

Hey! tea lovers. This post is about Java鈥檚 functional interfaces. We will talk about the basic ones briefly. These functional interfaces are used by Stream API heavily, so knowing them will give you a huge advantage. Not just streams you can use it anywhere. Prerequisites for Functional Interfaces Not much, Just make sure the concept of functional interfaces and lambdas are clear. You can find the code on GitHub here or the full project here. And yes fill your cup of tea to sip and learn with me. ...

May 9, 2020 路 6 min 路 Imran Shaikh