Hi,
I want to use the list in Java for inserting the data to it. I want to preserve the insertion order of item. So. I just wanted to know is list ordered in Java?
So, Let's know if list ordered in Java?
Thanks
About the List Interface in Java
The List interface in Java is from java.util package and the interface name is java.util.List, which is a child interface of Collection in Java programming language.
The List in Java is an ordered collection of objects, which means it preserve the order of insertion. The List in java allows the storing of duplicate values.
The List preserves the insertion order and it can be used for the positional access and insertion of elements.
In Java programming the List Interface is implemented by ArrayList, LinkedList, Vector and Stack.
So, if you are using any of the above List in your program then no need to worry as it preserver the order of insertion.
Thanks
Hi,
Yes List is ordered in Java. The implementation of List interfaces are ArrayList, LinkedList, Vector and Stack, which provides order access of data.
The values added to List preserves the order of insertion.
Thanks
Hi,
This is sample example:
package example.code; import java.util.ArrayList; import java.util.List; public class OrderedListExample { public static void main(String[] args) { //Create Array List object List list = new ArrayList(); //Add 10 Values for(int i=0;i<10;i++){ list.add(i); } //print the values stored in List for(int i=0;i<10;i++){ System.out.println(list.get(i)); } /* Program Output: 0 1 2 3 4 5 6 7 8 9 */ } }
Thanks
Ads