0

lets say i have an arraylist with the values

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

now in the list if i use streams and then i use skip function as skip(1), so the first element gets skipped, and then i want to limit it to lets say element 8 which would then have an index of 7, but i dont wanna hard code it as limit(7), rather i want a function to be passed which gives out an integer value such as if (number is equal to 8) then return index of 8.

so how do i do that?

what i specifically want is that can i use lambda functions to pass in any value like this i know i may be wrong but just trying

n -> n == 8

List<Integer> l = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        l.stream()
        .skip(1)
        .limit()
        .forEach(n->System.out.println(n));

i tried this

package classes;

import java.util.*;
public class Test {
    public static void main(String[] args) {
        List<Integer> l = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        int n = getInd(8, l);
        l.stream()
        .skip(1)
        .limit(n)
        .forEach(System.out::println);
    }
    static int getInd(int n, List<Integer> list) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == n) {return i;}
        }
        return -1;
    }
}

0 Answers0