Convert an array to list with specific range in Java 8

Viewed 6032

I want to convert one string array to list with specific range. In my case I always want from index 1 to last index. I don't need the index 0 value included in the list. Is there any direct method that I can use to filter and convert to the list as I need ?

public class test1 {

    public static void main(String[] args) {
        String[] optArr = {"start", "map1", "map2", "map3"};
        List<String> list = Arrays.stream(optArr).collect(Collectors.toList());
        System.out.println(list);
    }
}
9 Answers

You can also use the overloaded method Arrays.stream​(T[] array, int startInclusive, int endExclusive) as :

List<String> list = Arrays.stream(optArr, 1, optArr.length)
                          .collect(Collectors.toList());

Returns a sequential Stream with the specified range of the specified array as its source.


Alternatively(non Java-8), using the subList is an option, but I would prefer chaining it in one-line instead of creating a new object as:

List<String> list = Arrays.asList(optArr).subList(1, optArr.length);

You can use Stream.skip():

List<String> list = Arrays.stream(optArr).skip(1).collect(Collectors.toList());

One non Java 8 option might be to just create a view on top of your current list which omits the first element:

List<String> list = Arrays.stream(optArr).collect(Collectors.toList());
List<String> viewList = list.subList(1, list.size());

This would mean though that the underlying data structure is still the original list, but one extra element in memory does not seem like a big penalty.

One method use List.sublist(int,int)

List<String> list = Arrays.asList(optArr).subList(1,optArr.length);
System.out.println(list);

Second method use Stream

List<String> list = Arrays.stream(optArr)
    .skip(1)
    .collect(Collectors.toList());
System.out.println(list);

Looking at your data you might consider :

List<String> list = Stream.of(optArr).filter(s -> s.startsWith("map")).collect(toList());

But if you just want to filter the first index sublist is the way to go. You do not need to use stream wherever you can.

It should be like this.

List<String> al = new ArrayList<>(Arrays.asList(optArr));
List actualList = list.subList(1, al.size());

You can also use Arrays.copyOfRange to copy an array.

Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive.

public static <T> T[] copyOfRange(T[] original, int from, int to)

And then convert into List

List<String> result = Arrays.asList(Arrays.copyOfRange(optArr, 1, optArr.length));

IntStream : one other way by using IntStream

List<String> result = IntStream.range(1, optArr.length).mapToObj(i->optArr[i]).collect(Collectors.toList());

Why do you need to use streams? Why isn't some ancient magic like for-loops enough for the task?

String[] optArr = {"start", "map1", "map2", "map3"};
final List<String> list = new ArrayList<>();

for (int i = 1; i < optArr.length; i++) // skip first element
    list.add(optArr[i]);

The simplest tool for the job.

Related