-1

I have a list of strings and I want to add to a set all indexes from array where the string is not empty,

I tried doing this:

columnNum.addAll((Collection<? extends Integer>) IntStream.range(0, row.size()).filter(i-> StringUtils.isNotEmpty(row.get(i))));

but I get an exception

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
shani
  • 1

2 Answers2

1

You have to use boxed:

var list = List.of("","a","","b");

var set = IntStream.range(0, list.size())
        .filter(i -> 
   !list.get(i).isEmpty()).boxed().collect(Collectors.toSet());
csviri
  • 1,159
  • 3
  • 16
  • 31
0

Collect the stream to a List first. An IntStream is not a Collection.

columnNum.addAll(IntStream.range(0, row.size())
    .filter(i-> StringUtils.isNotEmpty(row.get(i)))
    .boxed().collect(Collectors.toList())); // or .toList() with Java 16+
Unmitigated
  • 76,500
  • 11
  • 62
  • 80