Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Array List

• ArrayList class implements List interface and


List interface extends Collection interface.
Array List(Cont..)
• Array List is a generic class that has this declaration
• Class ArrayList<E>
• Here E Specifies the type of objects that the list will
hold.
• Java ArrayList is a part of the Java collection
framework and it is a class of java.util package.
• Java ArrayList class uses a dynamic array for
storing the elements. It is like an array, but there
is no size limit.
• Syntax
• ArrayList<Type> arrayList= new ArrayList<>();
• Example
• ArrayList<Integer> arrayList = new ArrayList<>();

• // create String type arraylist


• ArrayList<String> arrayList = new ArrayList<>();
Points to be Remembered

• 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
the array works on an index basis.
import java.util.ArrayList;

public class Main {


public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
for (String i : cars)
{
System.out.println(i);
}
}
}
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
Collections.sort(cars);
for (String i : cars) {
System.out.println(i);
}
}
}
import java.util.*;

public class Main {


public static void main(String[] args) {
ArrayList cars = new ArrayList();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
cars.add(1);
cars.add(5.8);
for (int i = 0; i < cars.size(); i++) {
System.out.println(cars.get(i));
}
}
}
Linked List
• The LinkedList class of the Java collections
framework provides the functionality of the
linked list data structure.
• However, while the ArrayList class and
the LinkedList class can be used in the same
way, they are built very differently.
• Use an ArrayList for storing and accessing
data, and LinkedList to manipulate data.
import java.util.LinkedList;

class Main {
public static void main(String[] args){

// create linkedlist
LinkedList<String> animals = new LinkedList<>();

// Add elements to LinkedList


animals.add("Dog");
animals.add("Cat");
animals.add("Cow");
System.out.println("LinkedList: " + animals);
}
}
SET
• The set is an unordered collection of objects in
which duplicate values cannot be stored.
• It is an interface that implements the
mathematical set.
• This interface contains the methods inherited
from the Collection interface and adds a
feature that restricts the insertion of the
duplicate elements.
• In the above diagram, the NavigableSet and SortedSet are
both the interfaces. The NavigableSet extends the
SortedSet, so it will not retain the insertion order and store
the data in a sorted way
HashSet
• A HashSet is a collection of items where every item is
unique.
• The HashSet class extends AbstractSet class which
implements Set interface.
• Syntax:
• class HashSet<E>
Methods of Set

• add() - adds the specified element to the set


• addAll() - adds all the elements of the specified
collection to the set
• iterator() - returns an iterator that can be used to access
elements of the set sequentially
• remove() - removes the specified element from the set
• removeAll() - removes all the elements from the set
that is present in another specified set
• retainAll() - retains all the elements in the set that are
also present in another specified set
• clear() - removes all the elements from the set
• size() - returns the length (number of elements) of the
set
• toArray() - returns an array containing all the elements
of the set
• contains() - returns true if the set contains the specified
element
• containsAll() - returns true if the set contains all the
elements of the specified collection
• hashCode() - returns a hash code value (address of the
element in the set)
Set Operations

• Union - to get the union of two sets x and y,


we can use x.addAll(y)
• Intersection - to get the intersection of two
sets x and y, we can use x.retainAll(y)
• Subset - to check if x is a subset of y, we can
use y.containsAll(x)
• Difference: The difference operation deletes
the values from the set which are present in
another set removeAll()
Tree Set
• Java TreeSet class implements the Set
interface that uses a tree for storage.
• It inherits AbstractSet class and implements
the NavigableSet interface.
• The objects of the TreeSet class are stored in
ascending order.
• Syntax
Class TreeSet<E>

You might also like