public final class Lines extends Object
Usage example 1: The following shows how to merge lines to logical log lines.
// Some lines of example log files final List<String> fileLines = lines("[20:57:30] Thread 0: Start request\n" + "[20:57:31] Thread 0: Request ID: Example 1\n" + "[20:57:31] Thread 0: Request failed: SomeException\n" + " on line 12\n" + " on line 34\n" + "[20:57:32] Thread 0: Stop request"); // Calculating logical log lines based on consecutive lines final Stream<List<String>> logicalLogLines = consecutive( fileLines, (lines, line) -> !line.startsWith("[")); // Output logical log lines with three dashes in between each of them System.out.println(logicalLogLines // .map(lines -> lines.stream().collect(joining("\n"))) .collect(joining("\n---\n")));
Console output for usage example 1:
[20:57:30] Thread 1: Start request --- [20:57:31] Thread 1: Request ID: Example 1 --- [20:57:31] Thread 1: Request failed: SomeException on line 12 on line 34 --- [20:57:32] Thread 1: Stop request
Usage example 2: The following shows how to group log lines based on their thread name.
// Some lines of example log files final List<String> fileLines = lines( "[20:57:30] Thread 1: Start request\n" + "[20:57:30] Thread 2: Stop request\n" + "[20:57:30] Thread 1: Request ID: Example 2.1\n" + "[20:57:31] Thread 2: Start request\n" + "[20:57:31] Thread 1: Request processed\n" + "[20:57:31] Thread 2: Request ID: Example 2.2\n" + "[20:57:32] Thread 1: Stop request\n" + "[20:57:32] Thread 2: Request processed"); // Grouping log lines based on thread names final Stream<Entry<String, List<String>>> groupedLogLines = grouped( fileLines, line -> line.substring(11, 19), (lines, line) -> { if (line.endsWith("Start request")) { return GroupedLineType.START; } if (line.endsWith("Stop request")) { return GroupedLineType.END; } return GroupedLineType.MIDDLE; }); // Output grouped lines with three dashes between each group System.out.println(groupedLogLines .map(entry -> entry.getKey() + ":\n" + entry.getValue().stream().collect(joining("\n"))) .collect(joining("\n---\n")));
Console output for usage example 2:
Thread 2: [20:57:30] Thread 2: Stop request --- Thread 1: [20:57:30] Thread 1: Start request [20:57:30] Thread 1: Request ID: Example 2.1 [20:57:31] Thread 1: Request processed [20:57:32] Thread 1: Stop request --- Thread 2: [20:57:31] Thread 2: Start request [20:57:31] Thread 2: Request ID: Example 2.2 [20:57:32] Thread 2: Request processed
Based on your your needs you might need to combine the methods
lines(...)
, consecutive(...)
and grouped(...)
. In
addition MultiReader
might be helpful to parse
data, which is spread over multiple files.
Modifier and Type | Class and Description |
---|---|
static class |
Lines.GroupedLineType
Specifies the way groups of lines are created and closed.
|
Modifier and Type | Method and Description |
---|---|
static <T> Stream<List<T>> |
consecutive(Iterable<T> lines,
BiPredicate<List<T>,T> isNextConsecutive)
Creates lists of consecutive lines based on
lines . |
static <T> Stream<List<T>> |
consecutive(Iterator<T> linesIterator,
BiPredicate<List<T>,T> isNextConsecutive)
Creates lists of consecutive lines based on
lines . |
static <T> Stream<List<T>> |
consecutive(Stream<T> lines,
BiPredicate<List<T>,T> isNextConsecutive)
Creates lists of consecutive lines based on
lines . |
static <K,V> Stream<Map.Entry<K,List<V>>> |
grouped(Iterable<V> lines,
Function<V,K> getGroupKey,
BiFunction<List<V>,V,Lines.GroupedLineType> getLineType)
Groups lines based on a key and line types.
|
static <K,V> Stream<Map.Entry<K,List<V>>> |
grouped(Iterator<V> lines,
Function<V,K> getGroupKey,
BiFunction<List<V>,V,Lines.GroupedLineType> getLineType)
Groups lines based on a key and line types.
|
static <K,V> Stream<Map.Entry<K,List<V>>> |
grouped(Stream<V> lines,
Function<V,K> getGroupKey,
BiFunction<List<V>,V,Lines.GroupedLineType> getLineType)
Groups lines based on a key and line types.
|
static Stream<String> |
lines(Reader reader)
Reads all characters of
reader and splits them into lines using
BufferedReader.lines() . |
static List<String> |
lines(String value)
Splits
value into lines using BufferedReader.lines() . |
public static <T> Stream<List<T>> consecutive(Iterable<T> lines, BiPredicate<List<T>,T> isNextConsecutive)
lines
. An input line is
added to an output list whenever isNextConsecutive
returns
true
. On false
a new list gets created.
Check out usage example 1 at Lines
.
Tip: lines
must not necessarily consist of elements of type
String
.
T
- type of line (most probably String
)lines
- the linesisNextConsecutive
- when returning true
the input line is added
to an output list, else a new list gets created. The
first argument is the previous list of lines,
whereas the second argument is the current line.public static <T> Stream<List<T>> consecutive(Iterator<T> linesIterator, BiPredicate<List<T>,T> isNextConsecutive)
lines
. An input line is
added to an output list whenever isNextConsecutive
returns
true
. On false
a new list gets created.
Check out usage example 1 at Lines
.
Tip: lines
must not necessarily consist of elements of type
String
.
T
- type of line (most probably String
)linesIterator
- the linesisNextConsecutive
- when returning true
the input line is added
to an output list, else a new list gets created. The
first argument is the previous list of lines,
whereas the second argument is the current line.public static <T> Stream<List<T>> consecutive(Stream<T> lines, BiPredicate<List<T>,T> isNextConsecutive)
lines
. An input line is
added to an output list whenever isNextConsecutive
returns
true
. On false
a new list gets created.
Check out usage example 1 at Lines
.
Tip: lines
must not necessarily consist of elements of type
String
.
T
- type of line (most probably String
)lines
- the linesisNextConsecutive
- when returning true
the input line is added
to an output list, else a new list gets created. The
first argument is the previous list of lines,
whereas the second argument is the current line.public static <K,V> Stream<Map.Entry<K,List<V>>> grouped(Iterable<V> lines, Function<V,K> getGroupKey, BiFunction<List<V>,V,Lines.GroupedLineType> getLineType)
getGroupKey
calculates the key that is used for grouping getLineType
calculates
the line type, which specifies the way groups of lines are created and
closed.
Check out usage example 2 at Lines
.
Tip: lines
must not necessarily consist of elements of type
String
.
K
- type of the group keyV
- type of linelines
- the linesgetGroupKey
- calculates the lines group keygetLineType
- calculates the lines type to specify the way groups of
lines are created and closed. The first argument is the
previous list of lines, whereas the second argument is the
current line.public static <K,V> Stream<Map.Entry<K,List<V>>> grouped(Iterator<V> lines, Function<V,K> getGroupKey, BiFunction<List<V>,V,Lines.GroupedLineType> getLineType)
getGroupKey
calculates the key that is used for grouping getLineType
calculates
the line type, which specifies the way groups of lines are created and
closed.
Check out usage example 2 at Lines
.
Tip: lines
must not necessarily consist of elements of type
String
.
K
- type of the group keyV
- type of linelines
- the linesgetGroupKey
- calculates the lines group keygetLineType
- calculates the lines type to specify the way groups of
lines are created and closed. The first argument is the
previous list of lines, whereas the second argument is the
current line.public static <K,V> Stream<Map.Entry<K,List<V>>> grouped(Stream<V> lines, Function<V,K> getGroupKey, BiFunction<List<V>,V,Lines.GroupedLineType> getLineType)
getGroupKey
calculates the key that is used for grouping getLineType
calculates
the line type, which specifies the way groups of lines are created and
closed.
Check out usage example 2 at Lines
.
Tip: lines
must not necessarily consist of elements of type
String
.
K
- type of the group keyV
- type of linelines
- the linesgetGroupKey
- calculates the lines group keygetLineType
- calculates the lines type to specify the way groups of
lines are created and closed. The first argument is the
previous list of lines, whereas the second argument is the
current line.public static Stream<String> lines(Reader reader)
reader
and splits them into lines using
BufferedReader.lines()
.reader
- character stream to split into linespublic static List<String> lines(String value)
value
into lines using BufferedReader.lines()
.value
- value to splitCopyright © 2025. All rights reserved.