Collections
Collections
• Collection interfaces provide basic functionalities whereas collection classes provides their concrete
implementation
Collection Classes
• Collection classes are standard classes that implement collection interfaces
• Some Collection Classes are abstract and some classes are concrete and can be used as it is.
• Important Collection Classes:
✔ AbstractCollection
✔ AbstractList
✔ AbstractSequentialList
✔ LinkedList
✔ ArrayList
✔ AbstractSet
✔ HasSet
✔ LinkedHashSet
✔ TreeSet
ArrayList
• Growable Array implementation of List interface.
• Insertion order is preserved.
• Duplicate elements are allowed.
• Multiple null elements of insertion are allowed.
• Default initial capacity of an ArrayList is 10.
• The capacity grows with the below formula, once ArrayList reaches its max capacity.
• newCapacity= (oldCapacity * 3)/2 + 1
• When to use?
• If elements are to be retrieved frequently. Because ArrayList implements RandomAccess
Interface
• When not to use?
• If elements are added/removed at specific positions frequently
ArrayList - Example
import java.util.*;
class Test{
public static void main(String args[]){
ArrayList<Integer> al1 = new
ArrayList<Integer>();
al1.add(20);
al1.add(9);
al1.addAll(al2);
Collections.sort(al1);
Output:
System.out.println(al1);
System.out.println(al1.get(3));} [9, 20, 22, 53]
} 53
LinkedList - Methods
Constructor/Method Description
public boolean add(E e); It adds the specified element at the end of the list.
public void addFirst(E e); It adds the specified element in the beginning of the list.
public void addLast(E e); It adds the specified element to the end of the list
public E removeFirst(); It removes and returns the first element from the list.
public E removeLast(); It removes and returns the last element from the list.
Method Description
public synchronized E pop(); Removes the item at the top of the stack and returns that
item
public synchronized E peek(); Returns the item at the top of the stack
public synchronized int search(Object o); Returns the position of an object in the stack.
Set Interface
Set Interface
• The set interface is an unordered collection of objects in which duplicate values cannot be stored.
• The Java Set does not provide control over the position of insertion or deletion of elements.
Output:
Set data: 34 12 45 63
List Iterator
• List Iterator is used to traverse forward and backward directions
Method Description
boolean hasNext() This method return true if the list iterator has
more elements when traversing the list in the
forward direction.
Object next() This method return the next element in the list
and advances the cursor position.
boolean hasPrevious() This method return true if this list iterator has
more elements when traversing the list in the
reverse direction.
printlist(list1);
printlist(list2);
printlist(list3);
printlist(list4);
}
private static void printlist(List<Number> list) {
System.out.println(list);}
}
Output:
list1, list3, list4 – compilation error
Type not applicable for the arguements
Wildcard in Generics
class test{
public static void main(String[] args) {
List<Integer> list1= Arrays.asList(1,2,3);
List<Number> list2=Arrays.asList(1.1,2.2,3.3);
List<Double> list3=Arrays.asList(1.1,2.2,3.3);
List<String> list4=Arrays.asList("s","j","r");
printlist(list1);
printlist(list2);
printlist(list3);
printlist(list4);
}
private static void printlist(List<?> list) {
System.out.println(list);
}
} Output:
[1, 2, 3]
[1.1, 2.2, 3.3]
[1.1, 2.2, 3.3]
[s, j, r]
Upper Bounded Wildcard
class test{
public static void main(String[] args) {
List<Integer> list1= Arrays.asList(1,2,3);
List<Number> list2=Arrays.asList(1.1,2.2,3.3);
List<Double> list3=Arrays.asList(1.1,2.2,3.3);
List<String> list4=Arrays.asList("s","j","r");
printlist(list1);
printlist(list2);
printlist(list3);
printlist(list4);
}
private static void printlist(List<? extends Number> list){
System.out.println(list);}
}
Output:
list4 – compilation error
Type not applicable for the arguements
Lower Bounded Wildcard
class test{
public static void main(String[] args) {
List<Integer> list1= Arrays.asList(1,2,3);
List<Number> list2=Arrays.asList(1.1,2.2,3.3);
List<Double> list3=Arrays.asList(1.1,2.2,3.3);
printlist(list1);
printlist(list2);
printlist(list3);
}
private static void printlist(List<? super Integer> list)
{
System.out.println(list);
}
}
Output:
list3 – compilation error
Type not applicable for the arguements
Comparable Interface
Comparable Interface
• It is used to order the objects of user-defined class.
• It is found in java.lang package and contains only one method named compareTo(Object)
• Elements can be sorted based on single data member eg: account number, name or age.
class Test{
public static void main(String[] args) {
List<Account> al = new ArrayList<Account>();
al.add(new Account(111,"Ankit",5000));
al.add(new Account(112,"Ashok",4000));
al.add(new Account(123,“Ryan",5000));
Collections.sort(al);
for(Account a:al)
System.out.println(a);
}
}
Comparator
Interface
Comparator Interface
• Used to order user defined class
import java.util.*;
class Account{
int acc;
String name;
float amt;
Account(int acc,String name,float amt){
this.acc = acc;
this.name = name;
this.amt = amt; }
public String toString() {
return "Acc. No.: "+acc+" Name: "+name+" Amount: "+amt;}
}
Comparator - Example
al.add(new Account(123,"Ankit",5000));
al.add(new Account(112,"Ashok",4000));
al.add(new Account(111,"Ryan",5000));
System.out.println("Comparison on Amount");
Collections.sort(al,new AmtCmp());
for(Account a:al)
System.out.println(a);
class test {
public static void main(String[] args) {
List<Account> al = new ArrayList<Account>();
al.add(new Account(111,"Ryan",5000));
al.add(new Account(112,"Ryan",5000));
al.add(new Account(111,"Ryan",5000));
System.out.println(al.get(0).equals(al.get(2)));
System.out.println(al.get(0).equals(al.get(1))); }
}
Bounds in Generics (Comparator)
public class test {
public static void main(String[] args) {
System.out.printf("Max of %d, %d and %d is %d\n\n",
3, 4, 5, maximum( 3, 4, 5 ));
System.out.printf("Max of %.1f,%.1f and %.1f is %.1f\n\n",
6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ));
System.out.printf("Max of %s,%s and %s is %s\n\n",
"s", "j", "r", maximum( "s", "j", "r" ));
}
public static <T extends Comparable<T>> T maximum(T x, T y, T z) {
T max = x;
if(y.compareTo(max) > 0) {
max = y; }
if(z.compareTo(max) > 0) {
max = z; } Output:
return max; } Max of 3, 4 and 5 is 5
} Max of 6.6,8.8 and 7.7 is 8.8
Max of s,j and r is s
Multiple Bounds in Generics
public class test {
public static void main(String[] args) {
System.out.printf("Max of %d, %d and %d is %d\n\n",
3, 4, 5, maximum( 3, 4, 5 ));
System.out.printf("Max of %.1f,%.1f and %.1f is %.1f\n\n",
6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ));
System.out.printf("Max of %s,%s and %s is %s\n\n",
"s", "j", "r", maximum( "s", "j", "r" ));
}
public static <T extends Number & Comparable<T>> T maximum(T x, T y, T z) {
T max = x;
if(y.compareTo(max) > 0) {
max = y; }
if(z.compareTo(max) > 0) {
max = z; }
return max; } }
Error:
The method maximum(T, T, T) in the type test is not applicable for the arguments (String, String, String)