0

I have a piece of code:

table.stream().filter(row -> !hash.containsKey(row[keyColumnNumber])) .map(row -> row[keyColumnNumber]).map(hash::get) 

The last step: .map(hash::get) returns Collection<String[]>. and as a result I need to collect all of that to List.

.collect(Collectors.toList()) 

returns List> what is expected, but

.flatMap(Stream::of).collect(Collectors.toList()).collect(Collectors.toList()) 

returns the same result.

6
  • 4
    You have a lot of typos here. Commented Jul 10, 2018 at 10:01
  • 1
    Sorry, I typed code by hands, not copied Commented Jul 10, 2018 at 10:02
  • map(hash::get) returns Stream<String[]> or Stream<Collection<String[]>>? Commented Jul 10, 2018 at 10:03
  • @Fairy You can still edit your own question to correct those typos. It's not just for us, but for everyone: a question full of typos will be badly received, while a correct question can generate more and better-quality answers Commented Jul 10, 2018 at 10:04
  • Should the final result by a List<String> or List<String[]>? Commented Jul 10, 2018 at 10:06

3 Answers 3

5

You can use flatMap like so :

List<String> list = table.stream() .map(row -> hash.get(row[keyColumnNumber])) .filter(Objects::nonNull) .flatMap(Arrays::stream) .collect(Collectors.toList()); 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, that works. The final result looks like: table.stream().filter(row -> !hash.containsKey(row[keyColumnNumber])).map(row -> row[keyColumnNumber]) .flatMap(x -> Arrays.stream(hash.get(x).toArray(new String[0][0]))).collect(Collectors.toList());
@Fairy Err, don't do that. You are converting a Collection<String[]> into String[][], then flatten it back to String[].
@Fairy It's better to use @Eran solution if the result should be List<String[]>
When hash.containsKey(row[keyColumnNumber]) returns false, the subsequent hash.get(row[keyColumnNumber]) (her split into two operations), will return null. This doesn’t sound very useful. I suppose, the actual intention is to accept the elements where containsKey return true. Generally, it’s more efficient to use only get without a preceding containsKey: table.stream().map(row -> hash.get(row[keyColumnNumber])).filter(Objects::nonNull) .flatMap(Arrays::stream).collect(Collectors.toList());
4

If

table.stream().filter(row -> !hash.containsKey(row[keyColumnNumber])) .map(row -> row[keyColumnNumber]).map(hash::get) 

returns a Stream<Collection<String[]>> and you require a List<String[]>, you do need flatMap:

List<String[]> result = table.stream() .filter(row -> !hash.containsKey(row[keyColumnNumber])) .map(row -> row[keyColumnNumber]) .map(hash::get) // Stream<Collection<String[]>> .flatMap(Collection::stream) // Stream<String[]> .collect(Collectors.toList()); // List<String[]> 

It should be possible to join the map and flatMap calls into a single flatMap, as Aomine commented:

List<String[]> result = table.stream() .filter(row -> !hash.containsKey(row[keyColumnNumber])) .flatMap(row -> hash.get(row[keyColumnNumber]).stream()) // Stream<String[]> .collect(Collectors.toList()); // List<String[]> 

2 Comments

"It might be possible to join the map and flatMap calls into a single flatMap, but it's hard to tell without knowing the type of table and row[keyColumnNumber]." wouldnt it just be .flatMap(row -> hash.get(row[keyColumnNumber]).stream())?
@Aominè that’s right, but note the similarity to the preceding containsKey operation. I’d rather eliminate that, but then, you’d need a null check after get again, i.e. table.stream().map(row -> hash.get(row[keyColumnNumber])).filter(Objects::nonNull) .flatMap(Collection::stream).collect(Collectors.toList()); Btw., I reversed the logic to keep the non-null elements rather than the nulls (equivalent to containsKey rather than !containsKey)…
0

try this. i hope it can solve...

List<String> list = table.stream().filter(row -> !hash.containsKey(row[keyColumnNumber])) .map(row -> row[keyColumnNumber]) .flatMap(x -> Arrays.stream(hash.get()))//<<----------------- .collect(Collectors.toList()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.