Launch your tech mastery with us—your coding journey starts now!
Course Content
Array Handling
0/1
String Handling
0/1
Wrapper Classes
0/1
Collections in Java
0/1
Packages
0/1
File Handling
0/1
Multithreading
0/1
Java Networking
0/1
Core Java
  • provides readymade architecture.
  • represents set of classes and interface.
  • is optional.
  • Interfaces and its implementations i.e. classes
  • Algorithm

Java – The List Interface

  • ArrayList : An implementation that stores elements in a backing array. The array’s size will be automatically expanded if there isn’t enough room when adding new elements into the list. It’s possible to set the default size by specifying an initial capacity when creating a new ArrayList.
  • LinkedList : An implementation that stores elements in a doubly-linked list data structure. It offers constant time for adding and removing elements at the end of the list; and linear time for operations at other positions in the list. Therefore, we can consider using a LinkedList if fast adding and removing elements at the end of the list is required.

  • 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.
ArrayList al=new ArrayList();      //creating old non-generic arraylist
ArrayList<String> al=new ArrayList<String>();       //creating new generic arraylist
import java.util.*;  
class TestCollection1{  
public static void main(String args[]){  
ArrayList<String> list=new ArrayList<String>();       //Creating arraylist  
list.add("Ravi");

//Adding object in arraylist  
list.add("Vijay");  
list.add("Ravi");  
list.add("Ajay");  

//Traversing list through Iterator  
Iterator itr=list.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
}  
}
  • Java LinkedList class can contain duplicate elements.
  • Java LinkedList class maintains insertion order.
  • Java LinkedList class is non synchronized.
  • In Java LinkedList class, manipulation is fast because no shifting needs to be occurred.
  • Java LinkedList class can be used as list, stack or queue.
import java.util.*;  
public class TestCollection7{  
public static void main(String args[]){  
LinkedList<String> al=new LinkedList<String>();  
al.add("Ravi");  
al.add("Vijay");  
al.add("Ravi");  
al.add("Ajay");
Iterator<String> itr=al.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
}  
}
Output:
Ravi
Vijay
Ravi
Ajay
import java.util.*;  
class TestCollection9{  
public static void main(String args[]){  
//Creating HashSet and adding elements  
HashSet<String> set=new HashSet<String>();  
set.add("Ravi");  
set.add("Vijay");  
set.add("Ravi");
set.add("Ajay");  
//Traversing elements  
Iterator<String> itr=set.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
}  
}
Output:
Ajay
Vijay
Ravi
import java.util.*;  
class TestCollection10{  
public static void main(String args[]){  
LinkedHashSet<String> al=new LinkedHashSet<String>();  
al.add("Ravi");  
al.add("Vijay");  
al.add("Ravi");  
al.add("Ajay");  
Iterator<String> itr=al.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
}  
}
import java.util.*;  
class TestCollection11{  
public static void main(String args[]){  
//Creating and adding elements  
TreeSet<String> al=new TreeSet<String>();  
al.add("Ravi");  
al.add("Vijay");  
al.add("Ravi");  
al.add("Ajay");  
//Traversing elements  
Iterator<String> itr=al.iterator();  
while(itr.hasNext()){  
System.out.println(itr.next());  
}  
}  
}
Output:
Ajay
Ravi
Vijay

  • Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key.
  • Several methods throw a NoSuchElementException when no items exist in the invoking map.
  • ClassCastException is thrown when an object is incompatible with the elements in a map.
  • NullPointerException is thrown if an attempt is made to use a null object and null is not allowed in the map.
  • An UnsupportedOperationException is thrown when an attempt is made to change an unmodifiable map.
import java.util.*;  
class TestCollection13{  
public static void main(String args[]){  
HashMap<Integer,String> hm=new HashMap<Integer,String>();  
hm.put(100,"Amit");  
hm.put(101,"Vijay");  
hm.put(102,"Rahul");  
for(Map.Entry m:hm.entrySet()){  
System.out.println(m.getKey()+" "+m.getValue());  
}  
}  
}
Output:
102 Rahul
100 Amit
101 Vijay