0% found this document useful (0 votes)
11 views25 pages

Collections Framework - List, Set, Map.

programming in java for B tech/BE

Uploaded by

kumarkartbya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
11 views25 pages

Collections Framework - List, Set, Map.

programming in java for B tech/BE

Uploaded by

kumarkartbya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 25

Collections Framework-

List, Set, Map.


Collections

● The Collection in Java is a framework that provides an architecture to store


and manipulate the group of objects.
● Java Collections can achieve all the operations that you perform on a data
such as searching, sorting, insertion, manipulation, and deletion.
● Java Collection means a single unit of objects.
● Java Collection framework
○ interfaces (Set, List, Queue, Deque)
○ classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).
What is Collection framework

● The Collection framework represents a unified architecture for storing and


manipulating a group of objects. It has:
● Interfaces and its implementations, i.e., classes
● Algorithm
Methods of the Collection Interface

This method is used to add an object


add(Object)
to the collection.

This method adds all the elements in


addAll(Collection c)
the given collection to this collection.

This method removes all of the


clear()
elements from this collection.
This method returns true if the collection
contains(Object o)
contains the specified element.

This method returns true if the collection


containsAll(Collection c) contains all of the elements in the given
collection.

This method compares the specified


equals(Object o)
object with this collection for equality.

This method returns true if this collection


isEmpty()
contains no elements.

This method returns an iterator over the


iterator()
elements in this collection.

max() This method is used to return the


maximum value present in the collection.
This method is used to remove the given
object from the collection. If there are
remove(Object o)
duplicate values, then this method removes
the first occurrence of the object.

This method is used to remove all the


removeAll(Collection c) objects mentioned in the given collection
from the collection.
This method is used to return an array
toArray() containing all of the elements in this
collection.
● List Interface
- An ordered collection that allows us to add and remove elements like an Array.
● Set Interface
-It allows us to store elements in different sets similar to the set in Mathematics.
Queue Interface
- It allows us to store and access elements in a First in First put manner.
Map Interface
Allows elements to be stored in key/value pairs.
Keys are unique names that can be used to access a particular element in a map.
Each keyhas a single value associated with it
Iterator Interface
Provides methods that can be used to access elements of collections.
List interface

List interface in Java is a sub-interface of the Java collections interface.

It contains the index-based methods to insert, update, delete, and search


the elements. It can have duplicate elements also.

We can also store the null elements in the list.

List preserves the insertion order, it allows positional access and insertion
of elements.

It found in the java.util package.


To instantiate the List interface, we must use :
○ List <data-type> list1= new ArrayList();
○ List <data-type> list2 = new LinkedList();
○ List <data-type> list3 = new Vector();
○ List <data-type> list4 = new Stack();
ArrayList

● The ArrayList class implements the List interface.


● It uses a dynamic array to store the duplicate element of different data types.
● The ArrayList class maintains the insertion order and is non-synchronized.
● The elements stored in the ArrayList class can be randomly accessed.
import java.util.*;
class TestJavaCollection1{
public static void main(String args[]){
ArrayList<String> list1=new ArrayList<String>();//Creating arraylist
list1.add("Ravi");//Adding object in arraylist
list1.add("Vijay");
list1.add("Ravi");
list1.add("Ajay");
for (String name : list1)
System.out.println(list1);

}
}
}
import java.util.*;
public class GFG {

public static void main(String args[])


{

// Creating a List
List<String> al = new ArrayList<>();
mango
// Adding elements in the List orange
al.add("mango");
al.add("orange"); Grapes
al.add("Grapes");

// Iterating the List


// element using for-each loop
for (String fruit : al)
System.out.println(fruit);
}
}
import java.util.*; l2.add(1);
class GFG { l2.add(2); [1, 2]
l2.add(3); [1, 1, 2, 3, 2]
public static void main(String[] args) [1, 2, 3, 2]
{ // Will add list l2 from 1 index
l1.addAll(1, l2);
2
// Creating an object of List interface [5, 2, 3, 2]
// implemented by the ArrayList class System.out.println(l1);
List<Integer> l1 = new ArrayList<Integer>(); // Removes element from index 1
l1.remove(1);
// Adding elements to object of List interface
// Custom inputs // Printing the updated List 1
System.out.println(l1);
l1.add(0, 1);
l1.add(1, 2); // Prints element at index 3 in list 1
System.out.println(l1.get(3));
// Print the elements inside the object
System.out.println(l1); // Replace 0th element with 5
// in List 1
// interface implemented ArrayList class l1.set(0, 5);
// Declaring object of integer type
List<Integer> l2 = new ArrayList<Integer>(); // Again printing the updated List 1
System.out.println(l1);}}
import java.util.List;
import java.util.LinkedList;

class Main {

public static void main(String[] args) {


// Creating list using the LinkedList class
List<Integer> numbers = new LinkedList<>(); List: [1, 2, 3]
// Add elements to the list
Accessed Element: 3
numbers.add(1); Position of 3 is 1
numbers.add(2); Removed Element: 2
numbers.add(3);
System.out.println("List: " + numbers);

// Access element from the list


int number = numbers.get(2);
System.out.println("Accessed Element: " + number);

// Using the indexOf() method


int index = numbers.indexOf(2);
System.out.println("Position of 3 is " + index);

// Remove element from the list


int removedNumber = numbers.remove(1);
System.out.println("Removed Element: " + removedNumber); } }
SET
● The Set Interface is present in java.util package and extends the
Collection interface.
● It 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.
● There are two interfaces that extend the set implementation namely
SortedSet and NavigableSet.
import java.util.*;
import java.lang.*;
import java.io.*;

class SetCollection {
public static void main(String[] args) {
Set<String> hSet = new HashSet<>();
hSet.add("A");
hSet.add("B");
hSet.add("C"); List contains B? : true
Is list empty or not:false
System.out.println("List contains B? : " + hSet.contains("B"));
Elements of hashset: A
hSet.remove("C"); B
System.out.println("Is list empty or not:" + hSet.isEmpty());
System.out.print("Elements of hashset: ");
Iterator<String> it = hSet.iterator();
while (it.hasNext()) {
System.out.print(it.next() + "\t");
}
System.out.println(); }}
import java.util.*;
import java.lang.*;
import java.io.*;
class SetCollection {
public static void main(String[] args) {
LinkedHashSet<String> lhSet = new LinkedHashSet<>();
lhSet.add("E");
lhSet.add("F");
//It will not add E again as it is already present.
Hashcode for set:139
lhSet.add("E"); Tset represented in sorted
System.out.println("Hashcode for set:" + lhSet.hashCode()); form:[H, I, L]
First vaue of tSet:H
// ------> TreeSet <------ Last vaue of tSet:Y
TreeSet<String> tSet = new TreeSet<>();
tSet.add("L");
tSet.add("I");
tSet.add("H");
System.out.println("Tset represented in sorted form:" + tSet);
tSet.add("Y");
System.out.println("First vaue of tSet:" + tSet.first());
System.out.println("Last vaue of tSet:" + tSet.last()); } }
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{ // Creating an empty HashMap
Map<String, Integer> hm = new HashMap<String, Integer>();
// Inserting pairs in above Map
// using put() method a:100
hm.put("a", new Integer(100));
hm.put("b", new Integer(200)); b:200
hm.put("c", new Integer(300)); c:300
hm.put("d", new Integer(400));
d:400
// Traversing through Map using for-each loop
for (Map.Entry<String, Integer> me :
hm.entrySet()) {

// Printing keys
System.out.print(me.getKey() + ":");
System.out.println(me.getValue()); } } }

You might also like