Reputation: 1767
I have a number of input tables in a list, each input table has a header row which I would like to ignore.
Minimal example:
List<String> a = Arrays.asList("header", "v1", "v2", "v3");
List<String> b = Arrays.asList("header", "a1", "a2");
List<String> c = Arrays.asList("header", "b1", "b2", "b3", "b4");
List<List<String>> data = Arrays.asList(a, b, c);
List<String> result = data.stream()
.map(t -> t.subList(1, t.size()))
.flatMap(List::stream)
.collect(Collectors.toList());
Is there a "nicer" way to skip the header row then using map
on each table?
Note: I am using Java 8 and unable to migrate to a newer JDK
Upvotes: 2
Views: 2047
Reputation: 7165
One more possible way is to use filter as below,
List<String> result = data.stream()
.flatMap(List::stream)
.filter(e->!e.equals("header"))
.collect(Collectors.toList());
Upvotes: 0
Reputation: 45319
You can either call stream
on the the result of subList()
:
List<String> result = data.stream()
.flatMap(t -> t.subList(1, t.size()).stream())
.collect(Collectors.toList());
Or, alternatively, call skip
on the inner stream:
List<String> result = data.stream()
.flatMap(l -> l.stream().skip(1))
.collect(Collectors.toList());
Upvotes: 2
Reputation: 393831
Use skip()
:
List<String> result = data.stream()
.flatMap(t -> t.stream().skip(1))
.collect(Collectors.toList());
Upvotes: 6