Convert a Queue to List

后端 未结 6 1153
Happy的楠姐
Happy的楠姐 2020-12-31 01:29

What is the fastest way to convert a Queue into a List while keeping the Queue order?

6条回答
  •  天涯浪人
    2020-12-31 01:37

    Answering to old question for users who are already on java 8
    Java 8 provides the option of using streams and you can get a list from queue as:

    For example:

    Queue queue = new LinkedList<>();
            Student s1 = new Student("A",2);
            Student s2 = new Student("B",1);
            Student s3 = new Student("C",3);
            queue.add(s1);
            queue.add(s2);
            queue.add(s3);
    
        List studentList = queue.stream().collect(Collectors.toCollection(ArrayList::new));
    

提交回复
热议问题