• A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements.


  • In addition to the operations inherited from Collection, the List interface includes operations for the following:


    1. Positional access — manipulates elements based on their numerical position in the list. This includes methods such as get, set, add, addAll, and remove.


    2. Search — searches for a specified object in the list and returns its numerical position. Search methods include indexOf and lastIndexOf.


    3. Iteration — extends Iterator semantics to take advantage of the list's sequential nature. The listIterator methods provide this behavior.


    4. Range-view — The sublist method performs arbitrary range operations on the list.


  • The Java platform contains two general-purpose List implementations. ArrayList, which is usually the better-performing implementation, and LinkedList which offers better performance under certain circumstances.


  •  
    import java.util.*; 
    public class List_example 
    { 
        public static void main(String[] args) 
        { 
            List num = new ArrayList();
            num.add(123);
            num.add(456);
            System.out.println("List elements"+num);
            System.out.println(num.get(0));
            System.out.println(num.set(0,1234));
            System.out.println("New List elements"+num);
            num.remove(0);
            System.out.println("After remove new List elements"+num);
            
        } 
    } 
    
    Output
    List elements[123, 456]
    123
    123
    New List elements[1234, 456]
    After remove new List elements[456]
    
    
  • To remove all the elements in the ArrayList, use the clear() method:


  •  
    cars.clear();
    
    
  • ArrayList Size To find out how many elements an ArrayList have, use the size method:


  •  
    cars.size();
    
    
  • Loop Through an ArrayList Loop through the elements of an ArrayList with a for loop, and use the size() method to specify how many times the loop should run:


  •  
    public class MyClass {
      public static void main(String[] args) {
        ArrayList cars = new ArrayList();
        cars.add("Volvo");
        cars.add("BMW");
        cars.add("Ford");
        cars.add("Mazda");
        for (int i = 0; i < cars.size(); i++) {
          System.out.println(cars.get(i));
        }
      }
    }
    
    
    Output
    Volvo
    BMW
    Ford
    Mazda
    
    
  • You can also loop through an ArrayList with the for-each loop:


  •  
    public class MyClass {
      public static void main(String[] args) {
        ArrayList cars = new ArrayList();
        cars.add("Volvo");
        cars.add("BMW");
        cars.add("Ford");
        cars.add("Mazda");
        for (String i : cars) {
          System.out.println(i);
        }
      }
    }
    
    Output
    Volvo
    BMW
    Ford
    Mazda
    
    
  • Other Types Elements in an ArrayList are actually objects. In the examples above, we created elements (objects) of type "String". Remember that a String in Java is an object (not a primitive type). To use other types, such as int, you must specify an equivalent wrapper class: Integer. For other primitive types, use: Boolean for boolean, Character for char, Double for double, etc:


  • Create an ArrayList to store numbers (add elements of type Integer):


  •  
    import java.util.ArrayList;
    
    public class MyClass {
      public static void main(String[] args) {
        ArrayList myNumbers = new ArrayList();
        myNumbers.add(10);
        myNumbers.add(15);
        myNumbers.add(20);
        myNumbers.add(25);
        for (int i : myNumbers) {
          System.out.println(i);
        }
      }
    }
    
    Output
    10
    15
    20
    25
    
    
  • Sort an ArrayList Another useful class in the java.util package is the Collections class, which include the sort() method for sorting lists alphabetically or numerically:


  • Sort an ArrayList of Strings:


  •  
    import java.util.ArrayList;
    import java.util.Collections;  // Import the Collections class
    
    public class MyClass {
      public static void main(String[] args) {
        ArrayList cars = new ArrayList();
        cars.add("Volvo");
        cars.add("BMW");
        cars.add("Ford");
        cars.add("Mazda");
        Collections.sort(cars);  // Sort cars
        for (String i : cars) {
          System.out.println(i);
        }
      }
    }
    
    output
    BMW
    Ford
    Mazda
    Volvo
    
    
  • Sort an ArrayList of Integers:


  •  
    import java.util.ArrayList;
    import java.util.Collections;  // Import the Collections class
    
    public class MyClass {
      public static void main(String[] args) {
        ArrayList myNumbers = new ArrayList();
        myNumbers.add(33);
        myNumbers.add(15);
        myNumbers.add(20);
        myNumbers.add(34);
        myNumbers.add(8);
        myNumbers.add(12);
    
        Collections.sort(myNumbers);  // Sort myNumbers
    
        for (int i : myNumbers) {
          System.out.println(i);
        }
      }
    }
    
    Output
    8
    12
    15
    20
    33
    34
    
    
  • The shuffle algorithm, which is included in the Java platform's Collections class, randomly permutes the specified list using the specified source of randomness.


  • It's a bit subtle: It runs up the list from the bottom, repeatedly swapping a randomly selected element into the current position.


  •  
    import java.util.*;
    
    public class Shuffle {
        public static void main(String[] args) {
            List start= new ArrayList();
            start.add(123);
            start.add(2);
            start.add(567);
            start.add(98);
            System.out.println("Original list"+start);
            List list = new ArrayList(start);
            Collections.shuffle(list);
            System.out.println("New list"+list);
        }
    }
    
    Output
    Original list[123, 2, 567, 98]
    New list[2, 123, 98, 567]
    
    
  • In fact, this program can be made even shorter and faster.


  •  
    import java.util.*;
    public class Shuffle {
        public static void main(String[] args) {
            List list = new ArrayList();
            list.add("I");
            list.add("am");
            list.add("Pranav");
            list.add("Nerurkar");
            System.out.println(list);
            Collections.shuffle(list);
            System.out.println(list);
        }
    }
    
    Output
    [I, am, Pranav, Nerurkar]
    [Nerurkar, am, Pranav, I]
    
    
  • The range-view operation, subList(int fromIndex, int toIndex), returns a List view of the portion of this list whose indices range from fromIndex, inclusive, to toIndex, exclusive. This half-open range mirrors the typical for loop.


  •  
    import java.util.*;
    public class Shuffle {
        public static void main(String[] args) {
            List list = new ArrayList();
            list.add("I");
            list.add("am");
            list.add("Pranav");
            list.add("Nerurkar");
             list.add("I");
            list.add("am");
            list.add("good");
            list.add("boy");
            List sub = new ArrayList();
            sub = list.subList(0, 2);
            
            System.out.println("Original"+list);
            System.out.println("Sublist"+sub);
            
            System.out.println("Index of element I is "+list.indexOf("I"));
              System.out.println("Last Index of element I is "+list.lastIndexOf("I"));
        }
    }
    
    Output:
    
    Original[I, am, Pranav, Nerurkar, I, am, good, boy]
    Sublist[I, am]
    Index of element I is 0
    Last Index of element I is 4
    
    
  • sort — sorts a List using a merge sort algorithm, which provides a fast, stable sort. (A stable sort is one that does not reorder equal elements.)


  • shuffle — randomly permutes the elements in a List.


  • reverse — reverses the order of the elements in a List.


  • rotate — rotates all the elements in a List by a specified distance.


  • swap — swaps the elements at specified positions in a List.


  • replaceAll — replaces all occurrences of one specified value with another.


  • fill — overwrites every element in a List with the specified value.


  •  
    import java.util.*;
    import java.util.Collection;
    
    
    public class Shuffle {
        public static void main(String[] args) {
            List list = new ArrayList();
           
            list.add("I");
            list.add("am");
            list.add("Pranav");
            list.add("Nerurkar");
            list.add("I");
            list.add("am");
            list.add("good");
            list.add("boy");
            Collections.reverse(list);
            System.out.println("Reverse "+list);
            Collections.sort(list);
            System.out.println("Sort "+list);
            Collections.shuffle(list);
            System.out.println("Shuffle "+list);
            Collections.rotate(list,2);
            System.out.println("Rotate "+list);
            Collections.swap(list,1,2);
            System.out.println("Swap "+list);
            Collections.replaceAll(list,"I","We");
            System.out.println("Replace all "+list);
            Collections.fill(list,"I");
            System.out.println("Fill "+list);
         
           
        }
    }
    
    Output
    Reverse [boy, good, am, I, Nerurkar, Pranav, am, I]
    Sort [I, I, Nerurkar, Pranav, am, am, boy, good]
    Shuffle [am, I, boy, I, Nerurkar, good, am, Pranav]
    Rotate [am, Pranav, am, I, boy, I, Nerurkar, good]
    Swap [am, am, Pranav, I, boy, I, Nerurkar, good]
    Replace all [am, am, Pranav, We, boy, We, Nerurkar, good]
    Fill [I, I, I, I, I, I, I, I]
    
    
    
  • copy — copies the source List into the destination List.


  • binarySearch — searches for an element in an ordered List using the binary search algorithm.


  • indexOfSubList — returns the index of the first sublist of one List that is equal to another.


  • lastIndexOfSubList — returns the index of the last sublist of one List that is equal to another.


  •  
    // Java program to demonstrate 
    // copy() method 
      
    import java.util.*; 
      
    public class Sample { 
        public static void main(String[] argv) 
        { 
           
      
                // creating object of Source list and destination List 
                List srclst = new ArrayList(3); 
                List destlst = new ArrayList(2); 
      
                // Adding element to srclst 
                srclst.add("I"); 
                srclst.add("am"); 
                srclst.add("Don"); 
      
                // Adding element to destlst 
                destlst.add("1"); 
                destlst.add("2"); 
                destlst.add("3"); 
                
                // printing the srclst 
                System.out.println("Value of source list: " + srclst); 
      
                // printing the destlst 
                System.out.println("Value of destination list: " + destlst); 
      
                System.out.println("\nAfter copying:\n"); 
      
                // copy element into destlst 
                Collections.copy(destlst, srclst); 
      
                // printing the srclst 
                System.out.println("Value of source list: " + srclst); 
      
                // printing the destlst 
                System.out.println("Value of destination list: " + destlst); 
      
        } 
    }
    
    
    Output
    Value of source list: [I, am, Don]
    Value of destination list: [1, 2, 3]
    
    After copying:
    
    Value of source list: [I, am, Don]
    Value of destination list: [I, am, Don]