List is a type of Collection mainly useful to store the Objects which are duplicates as well. Its available in the package java.util.List. The behavior of the List is implemented by the java classes
- ArrayList
- LinkedList
- Vector
Java Mock Interview Questions about java.util.List as follows:
1. Adding an new item to the List
2. Adding an new item first to the List.
3. Adding an item from in the middle of the List.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package totalqa; import java.util.ArrayList; import java.util.List; public class ListExamples{ public static void main(String args[]) { List<String> list = new ArrayList<String>(); list.add("abc"); list.add("def"); //Adding an new item first to the List. list.add(0, "ghi"); // Adding an item from in the middle of the List. list.add(1, "jkl"); for(int i =0 ; i < list.size();i++) { System.out.println(list.get(i) + ":::" + i ); } } } |
4. Remove an item from the List.
5. Remove an item from the List at particular index.
6. Update an item from the List.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package totalqa; import java.util.ArrayList; import java.util.List; public class ListExamples{ public static void main(String args[]) { List<String> list = new ArrayList<String>(); list.add("item1"); list.add("item2"); list.add("item3"); list.add("item4"); //Remove an item from the List. list.remove("item2"); //Remove an item from the List at particular index. list.remove(2); //Update first item in the List list.set(0, "item1update"); for(int i =0 ; i < list.size();i++) { System.out.println(list.get(i) + ":::" + i ); } } } |
Conclusion:
Addition,Deletion and Updating the items of List is achieved using the methods available in the java.util.List package.