Reputation: 285
I'm looking for a concise way to filter out items in a List at a particular index. My example input looks like this:
List<Double> originalList = Arrays.asList(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0);
List<Integer> filterIndexes = Arrays.asList(2, 4, 6, 8);
I want to filter out items at index 2
, 4
, 6
, 8
. I have a for loop that skips items that match the index but I was hoping there would be an easy way of doing it using streams. The final result would look like that:
List<Double> filteredList = Arrays.asList(0.0, 1.0, 3.0, 5.0, 7.0, 9.0, 10.0);
Upvotes: 17
Views: 30843
Reputation: 18253
Probably you can use simple Iterator
?
Stream<Integer> rows = Stream.of(1, 2, 3);
Iterator<Integer> it = rows.iterator();
int pos = 0;
while (it.hasNext()) {
Integer num = it.next();
if (pos++ == 0) {
// this is a head
} else {
// this is a record
}
}
Upvotes: 0
Reputation: 21608
If you sort your indexes descending, then you can use java.util.List.remove(int)
to remove the items.
List<Double> originalList = new ArrayList<>(Arrays.asList(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0));
List<Integer> filterIndexes = Arrays.asList(2, 4, 6, 8);
filterIndexes.stream()
// remove higher indixes first, because each remove affects all indexes to the right
.sorted(Comparator.reverseOrder())
// make sure to use remove(int) not remove(Object) in java.util.List to use indexes
.mapToInt(Integer::intValue)
// remove each index
.forEach(originalList::remove);
// print results
originalList.forEach(System.out::println);
Upvotes: 3
Reputation: 100349
If your filteredIndexes
list is presorted, you can avoid checking every element in this way:
List<Double> filteredList = IntStream.rangeClosed(0, filterIndexes.size())
.mapToObj(idxPos -> idxPos == 0
? originalList.subList(0, filterIndexes.get(idxPos))
: idxPos == filterIndexes.size()
? originalList.subList(filterIndexes.get(idxPos-1)+1, originalList.size())
: originalList.subList(filterIndexes.get(idxPos-1)+1, filterIndexes.get(idxPos)))
.flatMap(List::stream)
.collect(Collectors.toList());
Here we create a number of sublists which contain all the elements between the filtered indices, then just flatten them into the single final list. For big input (e.g. a million of numbers) this solution could be magnitudes faster than one proposed by @AlexisC.
Upvotes: 6
Reputation: 93902
You can generate an IntStream
to mimic the indices of the original list, then remove the ones that are in the filteredIndexes
list and then map those indices to their corresponding element in the list (a better way would be to have a HashSet<Integer>
for indices since they are unique by definition so that contains
is a constant time operation).
List<Double> filteredList =
IntStream.range(0, originalList.size())
.filter(i -> !filterIndexes.contains(i))
.mapToObj(originalList::get)
.collect(Collectors.toList());
Upvotes: 30