Professional Documents
Culture Documents
WEek - 05
WEek - 05
WEek - 05
Week 05
Instructor : Dr. Saif Ur Rehman
Lab Engineer: Muhammad Hassan Mujtaba
Course Contents- Week05
• Java Collections
• Array List Todays Contents
• Link List
• Set
• Queue
• Stack
• Map
• Lab
ArrayList
• An ArrayList is a dynamic length collection framework in Java
that also stores the elements of the same type but here we do
not need to mention the size at the time of its creation as the
case in arrays.
• Add elements
• Access elements
• Change elements
• Remove elements
1. Add Elements to an ArrayList
import java.util.ArrayList;
• To add a single class Main {
public static void main(String[] args){
element to the
// create ArrayList
arraylist, we use the ArrayList<String> languages = new ArrayList<>();
// add() method without the index parameter
add() method of the
languages.add("Java");
ArrayList class. For languages.add("C");
languages.add("Python");
example,
System.out.println("ArrayList: " + languages);
}
}
2. Access ArrayList Elements
import java.util.ArrayList;
• Each element is known as a node. Duem to the dynamicity and ease of insertions
and deletions, they are preferred over the arrays.
• It also has a few disadvantages like the nodes cannot be accessed directly instead
we need to start from the head and follow through the link to reach a node we
wish to access.
ArrayList Vs LinkedList
Java LinkedList
// Import the LinkedList class
• The LinkedList
import java.util.LinkedList;
class is almost public class Main {
public static void main(String[] args) {
identical to the
LinkedList<String> cars = new LinkedList<String>();
ArrayList: cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
}
}
How the LinkedList works
• The LinkedList stores its items in "containers." The list has a link to the
first container and each container has a link to the next container in
the list. To add an element to the list, the element is placed into a
new container and that container is linked to one of the other
containers in the list.
How the LinkedList works
• The LinkedList stores its items in "containers." The list has a link to the
first container and each container has a link to the next container in
the list. To add an element to the list, the element is placed into a
new container and that container is linked to one of the other
containers in the list.
LinkedList Methods
• add(int index, E element) • addFirst(E e)
This method Inserts the specified element at the
This method Inserts the specified element at the specified beginning of this list.
position in this list. • addLast(E e)
• add(E e) This method Appends the specified element to the end of
this list.
This method Appends the specified element to the end of this • clear()
list.
This method removes all of the elements from this list.
• addAll(int index, Collection<E> c) • clone()
This method returns a shallow copy of this LinkedList.
This method Inserts all of the elements in the specified
• contains(Object o)
collection into this list, starting at the specified position.
This method returns true if this list contains the specified
• addAll(Collection<E> c) element.
Stack() constructor of this class. The below example creates an empty Stack.
• One object is used as a key (index) to another object (value). It can store different
types: String keys and Integer values, or the same type, like: String keys and String
values:
Add Items
• The HashMap class has many useful methods.
• For example, to add items to it, use the put() method:
Add Items
// Import the HashMap class
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// Create a HashMap object called capitalCities
HashMap<String, String> capitalCities = new HashMap<String, String>();
// Add keys and values (Country, City)
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities);
}
}
Access an Item
• To access a value in the HashMap, use the get() method and
capitalCities.get("England");
Remove an Item
• To remove an item, use the remove() method and refer to the
key:
capitalCities.remove("England");
Any Question
THANK YOU