Search code examples
javalistarraydeque

How to get a sublist of an ArrayDeQue?


I want to get sublist of an ArrayDeque such there is in ArrayList. Is there a way to do it ?


Solution

  • The simplest route would be to create a new ArrayList from the Deque and sublist that.

    public void test() {
        ArrayDeque<String> ad = new ArrayDeque<>();
        ad.add("Hello");
        ad.add("Bye!");
        ArrayList<String> al = new ArrayList<>(ad);
        List<String> alp = al.subList(1, al.size());
        System.out.println("ad=" + ad);
        System.out.println("al=" + al);
        System.out.println("alp=" + alp);
    }
    

    Note, however, that the ArrayList is a copy of the state of the Deque at the time and does not reflect changes in the Deque. If that is unacceptable then there are otrher (less simple) options.