I want to pick first N elements from a list in java
String[] ligature = new String{there,is not,a,single,person,who,knows,the,answer};
Now I want to pick first 5 elements from this.something like
Stiring[] selectedLigature = ligature[0:4];
I want to do it without using for loop.
Do not stream this simple case, there is subList
for this:
// for a List
yourList.subList(0, 5)...
// for an array
Arrays.copyOfRange
Arrays
class has a method for this:
Arrays.copyOfRange(ligature, 0, 5);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With