Use LISTAGG() on multiple joined table in Oracle

You may know how to use LISTAGG() on a single table, but don’t know how to use LISTAGG on multiple joined tables. This example demonstrates how to use aggregate function on joined multiple tables in Oracle 12g. Assuming we have the following two tables. “User” Table ID Name 111 aaa 222 bbb 333 ccc “Record” … Read more

Java Runtime.exec() Linux Pipe

If you want to utilize Linux pipe advantages, you can do it in Java. You may want to do the following, which is wrong. String cmd = "ls" Process p = Runtime.getRuntime().exec(cmd);String cmd = "ls" Process p = Runtime.getRuntime().exec(cmd); The correct way of executing shell command is the following: String[] cmd = { "/bin/sh", "-c", … Read more

How to Calculate Time Difference in Java?

Given two strings of time, you may want to calculate the difference between them. The following provide a simple solution. import java.text.SimpleDateFormat; import java.util.Date;   public class Main { public static void main(String[] args) throws Exception{ String time1 = "12:00:00"; String time2 = "12:01:00";   SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); Date date1 = format.parse(time1); Date … Read more

Transform Stream using Stream.flatMap()

The flatMap() method can be used for combining a stream of streams to one stream of objects. In this post I will show how to use flatMap() by using several use cases. Use Case 1 Suppose you have a list of orders and each order contains a list of items. class Order{ String orderId; ArrayList<Item> … Read more

How to write a counter in Java 8?

Writing a counter in Java can be as simple as 2 lines. In addition to its simplicity, we can also utilize the parallel computation to increase the counter’s performance. import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.Map;   public class Java8Counter { public static void main(String[] args) { String[] arr = { "program", "creek", "program", "creek", "java", … Read more

Primitive Type Stream Examples

In addition to Stream, java.util.stream package also provide a set of primitive type Streams such as DoubleStream, IntStream and LongStream. The primitive type streams are pretty similar with Stream. In this post I will use the IntStream to illustrate how to use primitive type streams. Create an IntStream From integer array: IntStream stream = IntStream.of(2, … Read more

How to Use Optional Type in Java 8 ?

1. Correct Way to Use Optional Consider the following example: List<String> list = new ArrayList<String>(); list.add("java"); list.add("php"); list.add("python"); list.add("perl"); list.add("c"); list.add("lisp"); list.add("c#"); Stream<String> wordStream = list.stream();   Stream<Integer> lengthStream = wordStream.map(s -> s.length()); Optional<Integer> sum = lengthStream.reduce((x, y) -> x + y); System.out.println(sum.get());List<String> list = new ArrayList<String>(); list.add("java"); list.add("php"); list.add("python"); list.add("perl"); list.add("c"); list.add("lisp"); list.add("c#"); Stream<String> … Read more

Collect Stream Result Examples

Instead of reducing a stream to a value, we can also collect the results. We can collect results to an array, a set/list, or a map by using the Stream.collect() method. 1. Collect Stream Results to Array List<String> list = new ArrayList<String>(); list.add("java"); list.add("php"); list.add("python"); Stream<String> wordStream = list.stream();   Stream<Integer> lengthStream = wordStream.map(s -> … Read more

Reduce Stream Examples

The Java 8 Stream API contains a set of terminal operations (such as average, sum, min, max, and count) which return one value by combining the elements of a stream. Such terminal operations are called reduction operations. In addition to those terminal operations, the JDK also provides the general-purpose reduction method – reduce(), which this … Read more

Transform Stream Using Stream.filter()

The filter() method is an intermediate operation. It returns a stream consisting of the elements of this stream that match the given condition. import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream;     public class Java8Filter<T> {   public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("java"); list.add("php"); list.add("python"); list.add("lisp"); list.add("c++");   … Read more

Transform Stream using Steam.map()

The map() method is an intermediate operation. It returns a stream consisting of the results of applying the given function to each element in the stream. The following code returns a stream of Integers, which are results of applying String.length() method. import java.util.ArrayList; import java.util.List; import java.util.stream.Stream;     public class Java8Map {   public … Read more

5 Ways of Creating a Stream in Java 8

1. From Arrays String[] arr = { "program", "creek", "program", "creek", "java", "web", "program" }; stream = Stream.of(arr);String[] arr = { "program", "creek", "program", "creek", "java", "web", "program" }; stream = Stream.of(arr); stream = Stream.of("program", "creek", "program", "creek", "java", "web", "program");stream = Stream.of("program", "creek", "program", "creek", "java", "web", "program"); String[] stringArr = {"a", "b", "c", … Read more