List:- List interface is the child interface of Collection. It is available in java.util package. List is an ordered collection of objects which allow duplicate values. List interface is implemented by ArrayList, LinkedList and Vector classes.
Creating object of List:- After introduction of Generics in java 1.5, it is possible to restrict the type of object that can be stored in List so we can declare type safe List:
List<Obj> list = new List <Obj>();
Adding and Accessing Elements:- To add elements to a List, we will use add() method. This is inherited from the collection interface.
Here we have few examples:-
List <String> li = new ArrayList(); // li is the object of string type
li.add("John");
li.add("Harry");
li.add("Jack");
OR
li.add(0, "Jacob"); // adding element on index basis
li.add(1, "Oliver");
Example:
package JavaPkg;
import java.util.ArrayList;
import java.util.List;
public class ListInterface {
public static void main(String[] args) {
List <String >li = new ArrayList<String>(); // Make generic by putting String object type
li.add("John");
li.add("Harry");
li.add("Jack");
li.add(0,"Jacob");
li.add(1, "Oliver");
for(String listelements:li)
{
System.out.println(listelements);
}
}
}
Output:
Jacob
Oliver
John
Harry
Jack
List set() method: We can set element at the place of added old elements by using index value.
Example:
package JavaPkg;
import java.util.ArrayList;
import java.util.List;
public class ListInterface {
public static void main(String[] args) {
List <String >li = new ArrayList<String>();
li.add("John");
li.add("Harry");
li.add("Jack");
li.set(2,"Leo");
for(String listelements:li)
{
System.out.println(listelements);
}
}
}
Output:
John
Harry
Leo
Note: Index of elements start from 0, so index of Jack is 2, so it will replace with Leo.
Removing elements from the List:- We can remove element in two ways:
1. remove(object element): In this we will give element name which we want to remove.
2. remove(int index): It will remove the element on given index.
Clearing a List: clear() method is use to remove the all elements from the list.
Example:
package JavaPkg;
import java.util.ArrayList;
import java.util.List;
public class ListInterface {
public static void main(String[] args) {
List <String >li = new ArrayList<String>();
li.add("John");
li.add("Harry");
li.add("Jack");
li.clear(); // clear the list
for(String listelements:li)
{
System.out.println(listelements);
}
}
}
List size: By using size() method we can get size of the List.
List <String >li = new ArrayList<String>();
li.add("John");
li.add("Harry");
int list_size= li.size(); // finding size of the list
1.int indexOf(Object o): This method return the index of first element(If element are exist more than one). If given element is not present in the list then it return -1.
2.int lastIndexOf(Object o): This method return the index of last element(If element are exist more than one). If given element is not present in the list then it return -1.
Example:
package JavaPkg;
import java.util.ArrayList;
import java.util.List;
public class ListInterface {
public static void main(String[] args) {
List <String >li = new ArrayList<String>();
li.add("Jack");
li.add("John");
li.add("Harry");
li.add("Jack");
System.out.println(li.indexOf("Jack")); // return 0
System.out.println(li.lastIndexOf("Jack")); // return 3
System.out.println(li.indexOf("Oliver")); // return -1
}
}
Collection Framework in Java in Depth
Creating object of List:- After introduction of Generics in java 1.5, it is possible to restrict the type of object that can be stored in List so we can declare type safe List:
List<Obj> list = new List <Obj>();
Adding and Accessing Elements:- To add elements to a List, we will use add() method. This is inherited from the collection interface.
Here we have few examples:-
List <String> li = new ArrayList(); // li is the object of string type
li.add("John");
li.add("Harry");
li.add("Jack");
OR
li.add(0, "Jacob"); // adding element on index basis
li.add(1, "Oliver");
Example:
package JavaPkg;
import java.util.ArrayList;
import java.util.List;
public class ListInterface {
public static void main(String[] args) {
List <String >li = new ArrayList<String>(); // Make generic by putting String object type
li.add("John");
li.add("Harry");
li.add("Jack");
li.add(0,"Jacob");
li.add(1, "Oliver");
for(String listelements:li)
{
System.out.println(listelements);
}
}
}
Output:
Jacob
Oliver
John
Harry
Jack
List set() method: We can set element at the place of added old elements by using index value.
Example:
package JavaPkg;
import java.util.ArrayList;
import java.util.List;
public class ListInterface {
public static void main(String[] args) {
List <String >li = new ArrayList<String>();
li.add("John");
li.add("Harry");
li.add("Jack");
li.set(2,"Leo");
for(String listelements:li)
{
System.out.println(listelements);
}
}
}
Output:
John
Harry
Leo
Note: Index of elements start from 0, so index of Jack is 2, so it will replace with Leo.
Removing elements from the List:- We can remove element in two ways:
1. remove(object element): In this we will give element name which we want to remove.
2. remove(int index): It will remove the element on given index.
Clearing a List: clear() method is use to remove the all elements from the list.
Example:
package JavaPkg;
import java.util.ArrayList;
import java.util.List;
public class ListInterface {
public static void main(String[] args) {
List <String >li = new ArrayList<String>();
li.add("John");
li.add("Harry");
li.add("Jack");
li.clear(); // clear the list
for(String listelements:li)
{
System.out.println(listelements);
}
}
}
List size: By using size() method we can get size of the List.
List <String >li = new ArrayList<String>();
li.add("John");
li.add("Harry");
int list_size= li.size(); // finding size of the list
Search the Element in List: List provides methods to search the element and return the numeric value. Following methods are:
1.int indexOf(Object o): This method return the index of first element(If element are exist more than one). If given element is not present in the list then it return -1.
2.int lastIndexOf(Object o): This method return the index of last element(If element are exist more than one). If given element is not present in the list then it return -1.
Example:
package JavaPkg;
import java.util.ArrayList;
import java.util.List;
public class ListInterface {
public static void main(String[] args) {
List <String >li = new ArrayList<String>();
li.add("Jack");
li.add("John");
li.add("Harry");
li.add("Jack");
System.out.println(li.indexOf("Jack")); // return 0
System.out.println(li.lastIndexOf("Jack")); // return 3
System.out.println(li.indexOf("Oliver")); // return -1
}
}
Collection Framework in Java in Depth
No comments:
Post a Comment