CSE Module 4
CSE Module 4
ArrayList
LinkedList
Vector
Stack
The List interface includes all the methods of the Collection interface. Its
because Collection is a super interface of List.
Some of the commonly used methods of the Collection interface that’s also
available in the List interface are:
add() - adds an element to a list
addAll() - adds all elements of one list to another
get() - helps to randomly access elements from lists
iterator() - returns iterator object that can be used to sequentially
access elements of lists
set() - changes elements of lists
remove() - removes an element from the list
import java . u t i l . L i s t ;
import java . u t i l . A r r a y L i s t ;
c l a s s Main3 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
L i s t <I n t e g e r > numbers = new A r r a y L i s t < >();
numbers . add ( 1 ) ;
numbers . add ( 2 ) ;
numbers . add ( 3 ) ;
System . o u t . p r i n t l n ( ” L i s t : ” + numbers ) ;
i n t number = numbers . g e t ( 2 ) ;
System . o u t . p r i n t l n ( ” A c c e s s e d E l e m e n t : ” + number ) ;
i n t removedNumber = numbers . remove ( 1 ) ;
System . o u t . p r i n t l n ( ” Removed E l e m e n t : ” + removedNumber
}}
In Java, we need to declare the size of an array before we can use it.
Once the size of an array is declared, it’s hard to change it.
To handle this issue, we can use the ArrayList class. It allows us to
create resizable arrays.
Unlike arrays, arraylists can automatically adjust their capacity when
we add or remove elements from them. Hence, arraylists are also
known as dynamic arrays.
Creating an ArrayList Before using ArrayList, we need to import the
java.util.ArrayList package first. Here is how we can create arraylists in
Java:
A r r a y L i s t <Type> a r r a y L i s t= new A r r a y L i s t < >();
Here, Type indicates the type of an arraylist.
// c r e a t e S t r i n g t y p e a r r a y l i s t
A r r a y L i s t <S t r i n g > a r r a y L i s t = new A r r a y L i s t < >();
In the above program, we have used Integer not int. It is because we cannot
use primitive types while creating an arraylist. Instead, we have to use the
corresponding wrapper classes.
import java . u t i l . A r r a y L i s t ;
c l a s s Array1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
// c r e a t e A r r a y L i s t
A r r a y L i s t <S t r i n g > l a n g u a g e s = new A r r a y L i s t < >();
// Add e l e m e n t s t o A r r a y L i s t
l a n g u a g e s . add ( ” J a v a ” ) ;
l a n g u a g e s . add ( ” Python ” ) ;
l a n g u a g e s . add ( ” S w i f t ” ) ;
System . o u t . p r i n t l n ( ” A r r a y L i s t : ” + l a n g u a g e s ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
15 / 88
Basic operations on ArrayList
import java . u t i l . A r r a y L i s t ;
c l a s s Array2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
// c r e a t e A r r a y L i s t
A r r a y L i s t <S t r i n g > l a n g u a g e s = new A r r a y L i s t < >();
// add ( ) method w i t h o u t t h e i n d e x p a r a m e t e r
l a n g u a g e s . add ( ” J a v a ” ) ;
l a n g u a g e s . add ( ”C ” ) ;
l a n g u a g e s . add ( ” Python ” ) ;
System . o u t . p r i n t l n ( ” A r r a y L i s t : ” + l a n g u a g e s ) ;
}
}
Creating a Vector
V e c t o r <Type> v e c t o r = new V e c t o r < >();
Here, Type indicates the type of a linked list.
// c r e a t e I n t e g e r t y p e l i n k e d l i s t
V e c t o r <I n t e g e r > v e c t o r= new V e c t o r < >();
// c r e a t e S t r i n g t y p e l i n k e d l i s t
V e c t o r <S t r i n g > v e c t o r= new V e c t o r < >();
The HashSet class of the Java Collections framework provides the function-
alities of the hash table data structure.
It’s possible to create a hash table without defining its capacity and load
factor. For example,
// HashSet w i t h d e f a u l t c a p a c i t y and l o a d f a c t o r
HashSet<I n t e g e r > numbers1 = new HashSet < >();
By default,
the capacity of the hash set will be 16
the load factor will be 0.75
Methods of HashSetThe HashSet class provides various methods that
allow us to perform various operations on the set.
i m p o r t j a v a . u t i l . HashSet ;
c l a s s Hash6 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashSet<I n t e g e r > numbers = new HashSet < >();
numbers . add ( 2 ) ;
numbers . add ( 5 ) ;
numbers . add ( 6 ) ;
System . o u t . p r i n t l n ( ” HashSet : ” + numbers ) ;
b o o l e a n v a l u e 1 = numbers . remove ( 5 ) ;
System . o u t . p r i n t l n ( ” I s 5 removed ? ” + v a l u e 1 ) ;
b o o l e a n v a l u e 2 = numbers . r e m o v e A l l ( numbers ) ;
System . o u t . p r i n t l n ( ” Are a l l e l e m e n t s removed ? ” +v a l u e
}}
// L i n k e d H a s h S e t w i t h 8 c a p a c i t y and 0 . 7 5 l o a d f a c t o r
L i n k e d H a s h S e t <I n t e g e r > numbers = new L i n k e d H a s h S e t <>(8
Default capacity and load factor
// L i n k e d H a s h S e t w i t h d e f a u l t c a p a c i t y and l o a d f a c t o r
L i n k e d H a s h S e t <I n t e g e r > numbers1 = new L i n k e d H a s h S e t < >
1 We can access and modify values using the keys associated with them.
2 The Map interface maintains 3 different sets:
3 the set of keys
4 the set of values
5 the set of key/value associations (mapping).
Hence we can access keys, values, and associations individually.
i m p o r t j a v a . u t i l . Map ;
i m p o r t j a v a . u t i l . TreeMap ;
c l a s s map2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Map<S t r i n g , I n t e g e r > v a l u e s = new TreeMap < >();
v a l u e s . p u t ( ” Second ” , 2 ) ;
v a l u e s . put (” F i r s t ” , 1 ) ;
System . o u t . p r i n t l n ( ”Map u s i n g TreeMap : ” + v a l u e s ) ;
v a l u e s . r e p l a c e (” F i r s t ” , 1 1) ;
v a l u e s . r e p l a c e ( ” Second ” , 2 2 ) ;
System . o u t . p r i n t l n ( ” New Map : ” + v a l u e s ) ;
i n t r e m o v e d V a l u e = v a l u e s . remove ( ” F i r s t ” ) ;
System . o u t . p r i n t l n ( ” Removed V a l u e : ” + r e m o v e d V a l u e ) ;
}}
i m p o r t j a v a . u t i l . HashMap ;
c l a s s map3{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashMap<S t r i n g , I n t e g e r > l a n g u a g e s = new HashMap < >();
l a n g u a g e s . put (” Java ” , 8 ) ;
l a n g u a g e s . put (” J a v a S c r i p t ” , 1 ) ;
l a n g u a g e s . p u t ( ” Python ” , 3 ) ;
System . o u t . p r i n t l n ( ” HashMap : ” + l a n g u a g e s ) ;
}
}
i m p o r t j a v a . u t i l . HashMap ;
c l a s s map4 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashMap<S t r i n g , I n t e g e r > numbers = new HashMap < >();
System . o u t . p r i n t l n ( ” I n i t i a l HashMap : ” + numbers ) ;
numbers . p u t ( ” One ” , 1 ) ;
numbers . p u t ( ”Two” , 2 ) ;
numbers . p u t ( ” Three ” , 3 ) ;
System . o u t . p r i n t l n ( ” HashMap a f t e r p u t ( ) : ” + numbers ) ;
}}
i m p o r t j a v a . u t i l . HashMap ;
c l a s s map5 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashMap<I n t e g e r , S t r i n g > l a n g u a g e s = new HashMap < >();
l a n g u a g e s . put (1 , ” Java ” ) ;
l a n g u a g e s . p u t ( 2 , ” Python ” ) ;
l a n g u a g e s . put (3 , ” J a v a S c r i p t ” ) ;
System . o u t . p r i n t l n ( ” HashMap : ” + l a n g u a g e s ) ;
String value = languages . get ( 1 ) ;
System . o u t . p r i n t l n ( ” V a l u e a t i n d e x 1 : ” + v a l u e ) ;
}}
i m p o r t j a v a . u t i l . HashMap ;
c l a s s map6 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashMap<I n t e g e r , S t r i n g > l a n g u a g e s = new HashMap < >();
l a n g u a g e s . put (1 , ” Java ” ) ;
l a n g u a g e s . p u t ( 2 , ” Python ” ) ;
l a n g u a g e s . put (3 , ” J a v a S c r i p t ” ) ;
System . o u t . p r i n t l n ( ” O r i g i n a l HashMap : ” + l a n g u a g e s ) ;
l a n g u a g e s . r e p l a c e ( 2 , ”C++”);
System . o u t . p r i n t l n ( ” HashMap u s i n g r e p l a c e ( ) : ” + l a n g u
}}
i m p o r t j a v a . u t i l . HashMap ;
c l a s s map7 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashMap<I n t e g e r , S t r i n g > l a n g u a g e s = new HashMap < >();
l a n g u a g e s . put (1 , ” Java ” ) ;
l a n g u a g e s . p u t ( 2 , ” Python ” ) ;
l a n g u a g e s . put (3 , ” J a v a S c r i p t ” ) ;
System . o u t . p r i n t l n ( ” HashMap : ” + l a n g u a g e s ) ;
S t r i n g v a l u e = l a n g u a g e s . remove ( 2 ) ;
System . o u t . p r i n t l n ( ” Removed v a l u e : ” + v a l u e ) ;
System . o u t . p r i n t l n ( ” Updated HashMap : ” + l a n g u a g e s ) ;
}}
The wrapper classes in Java are used to convert primitive types (int, char,
float, etc) into corresponding objects.
Each of the 8 primitive types has corresponding wrapper classes.
We can also use the valueOf() method to convert primitive types into cor-
responding objects.
c l a s s wrapper1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
int a = 5;
double b = 5.65;
I n t e g e r aObj = I n t e g e r . v a l u e O f ( a ) ;
Double bObj = Double . v a l u e O f ( b ) ;
i f ( aObj i n s t a n c e o f I n t e g e r ) {
System . o u t . p r i n t l n ( ” An o b j e c t o f I n t e g e r i s c r e a t e d . ” )
}
i f ( bObj i n s t a n c e o f Double ) {
System . o u t . p r i n t l n ( ” An o b j e c t o f Double i s c r e a t e d . ” ) ;
}}}
c l a s s wrapper2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
// c r e a t e s o b j e c t s o f w r a p p e r c l a s s
I n t e g e r aObj = I n t e g e r . v a l u e O f ( 2 3 ) ;
Double bObj = Double . v a l u e O f ( 5 . 5 5 ) ;
// c o n v e r t s i n t o p r i m i t i v e t y p e s
i n t a = aObj . i n t V a l u e ( ) ;
d o u b l e b = bObj . d o u b l e V a l u e ( ) ;
System . o u t . p r i n t l n ( ” The v a l u e o f a : ” + a ) ;
System . o u t . p r i n t l n ( ” The v a l u e o f b : ” + b ) ;
}
}
In the above example, we have used the intValue() and doubleValue()
method to convert the Integer and Double objects into corresponding prim-
itive types.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
79 / 88
Java autoboxing
import java . u t i l . A r r a y L i s t ;
c l a s s auto {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
A r r a y L i s t <I n t e g e r > l i s t = new A r r a y L i s t < >();
// a u t o b o x i n g
l i s t . add ( 5 ) ;
l i s t . add ( 6 ) ;
System . o u t . p r i n t l n ( ” A r r a y L i s t : ” + l i s t ) ;
}
}
import java . u t i l . A r r a y L i s t ;
c l a s s unbox {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
A r r a y L i s t <I n t e g e r > l i s t = new A r r a y L i s t < >();
// a u t o b o x i n g
l i s t . add ( 5 ) ;
l i s t . add ( 6 ) ;
System . o u t . p r i n t l n ( ” A r r a y L i s t : ” + l i s t ) ;
// u n b o x i n g
i n t a = l i s t . get ( 0 ) ;
System . o u t . p r i n t l n ( ” V a l u e a t i n d e x 0 : ” + a ) ;
}}
demo.< I n t e g e r >g e n e r i c M e t h o d ( 2 5 ) ;
In general, the type parameter can accept any data types (except primitive
types).
However, if we want to use generics for some specific types (such as accept
data of number types) only, then we can use bounded types.
In the case of bound types, we use the extends keyword. For example,
<T e x t e n d s A>
This means T can only accept data that are subtypes of A.
c l a s s G e n e r i c s C l a s s <T e x t e n d s Number> {
public void display () {
System . o u t . p r i n t l n ( ” bounded t y p e g e n e r i c s c l a s s . ” ) ;
}}
c l a s s gen3 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
// c r e a t e an o b j e c t o f G e n e r i c s C l a s s
G e n e r i c s C l a s s <S t r i n g > o b j = new G e n e r i c s C l a s s < >();
}
}