- Java ArrayList class uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.
- Java ArrayList class can contain duplicate elements.
- Java ArrayList class maintains insertion order.
- Java ArrayList class is non synchronized.
- Java ArrayList allows random access because array works at the index basis.
- In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.
Java Non-generic Vs Generic Collection
Java collection framework was non-generic before JDK 1.5. Since 1.5, it is generic.
Java new generic collection allows you to have only one type of object in collection. Now it is type safe so typecasting is not required at run time.
Let's see the old non-generic example of creating java collection.
- ArrayList al=new ArrayList();
Let's see the new generic example of creating java collection.
- ArrayList<String> al=new ArrayList<String>();
In generic collection, we specify the type in angular braces. Now ArrayList is forced to have only specified type of objects in it. If you try to add another type of object, it gives compile time error.
Example of Java ArrayList class
- import java.util.*;
- class TestCollection1{
- public static void main(String args[]){
-
- ArrayList<String> al=new ArrayList<String>();
- al.add("Ravi");
- al.add("Vijay");
- al.add("Ravi");
- al.add("Ajay");
-
- Iterator itr=al.iterator();
- while(itr.hasNext()){
- System.out.println(itr.next());
- }
- }
- }
Two ways to iterate the elements of collection in java
- By Iterator interface.
- By for-each loop.
In the above example, we have seen traversing ArrayList by Iterator. Let's see the example to traverse ArrayList elements using for-each loop.
Iterating the elements of Collection by for-each loop
- import java.util.*;
- class TestCollection2{
- public static void main(String args[]){
- ArrayList<String> al=new ArrayList<String>();
- al.add("Ravi");
- al.add("Vijay");
- al.add("Ravi");
- al.add("Ajay");
- for(String obj:al)
- System.out.println(obj);
- }
- }
User-defined class objects in Java ArrayList
- class Student{
- int rollno;
- String name;
- int age;
- Student(int rollno,String name,int age){
- this.rollno=rollno;
- this.name=name;
- this.age=age;
- }
- }
- import java.util.*;
- public class TestCollection3{
- public static void main(String args[]){
-
- Student s1=new Student(101,"Sonoo",23);
- Student s2=new Student(102,"Ravi",21);
- Student s2=new Student(103,"Hanumat",25);
-
- ArrayList<Student> al=new ArrayList<Student>();
- al.add(s1);
- al.add(s2);
- al.add(s3);
-
- Iterator itr=al.iterator();
-
- while(itr.hasNext()){
- Student st=(Student)itr.next();
- System.out.println(st.rollno+" "+st.name+" "+st.age);
- }
- }
- }
101 Sonoo 23
102 Ravi 21
103 Hanumat 25
Example of addAll(Collection c) method
- import java.util.*;
- class TestCollection4{
- public static void main(String args[]){
-
- ArrayList<String> al=new ArrayList<String>();
- al.add("Ravi");
- al.add("Vijay");
- al.add("Ajay");
-
- ArrayList<String> al2=new ArrayList<String>();
- al2.add("Sonoo");
- al2.add("Hanumat");
-
- al.addAll(al2);
-
- Iterator itr=al.iterator();
- while(itr.hasNext()){
- System.out.println(itr.next());
- }
- }
- }
Ravi
Vijay
Ajay
Sonoo
Hanumat
Example of removeAll() method
- import java.util.*;
- class TestCollection5{
- public static void main(String args[]){
-
- ArrayList<String> al=new ArrayList<String>();
- al.add("Ravi");
- al.add("Vijay");
- al.add("Ajay");
-
- ArrayList<String> al2=new ArrayList<String>();
- al2.add("Ravi");
- al2.add("Hanumat");
-
- al.removeAll(al2);
-
- System.out.println("iterating the elements after removing the elements of al2...");
- Iterator itr=al.iterator();
- while(itr.hasNext()){
- System.out.println(itr.next());
- }
-
- }
- }
iterating the elements after removing the elements of al2...
Vijay
Ajay
Example of retainAll() method
- import java.util.*;
- class TestCollection6{
- public static void main(String args[]){
- ArrayList<String> al=new ArrayList<String>();
- al.add("Ravi");
- al.add("Vijay");
- al.add("Ajay");
- ArrayList<String> al2=new ArrayList<String>();
- al2.add("Ravi");
- al2.add("Hanumat");
-
- al.retainAll(al2);
-
- System.out.println("iterating the elements after retaining the elements of al2...");
- Iterator itr=al.iterator();
- while(itr.hasNext()){
- System.out.println(itr.next());
- }
- }
- }
iterating the elements after retaining the elements of al2...
Ravi
No comments:
Post a Comment