0% found this document useful (0 votes)
41 views11 pages

Set, List, Map Presentation

The set interface in Java provides an unordered collection that does not allow duplicate elements. It implements mathematical set operations. Common set implementations include HashSet. The set interface contains methods for adding elements, checking for presence of elements, removing elements, and retrieving the size. The list interface provides an ordered collection that allows duplicate elements and supports indexed access. Common list implementations are ArrayList and LinkedList. The list interface contains methods for adding/removing elements at specific indexes or from the entire list, retrieving elements by index, and checking for presence of elements.

Uploaded by

Jatin Joshi
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)
41 views11 pages

Set, List, Map Presentation

The set interface in Java provides an unordered collection that does not allow duplicate elements. It implements mathematical set operations. Common set implementations include HashSet. The set interface contains methods for adding elements, checking for presence of elements, removing elements, and retrieving the size. The list interface provides an ordered collection that allows duplicate elements and supports indexed access. Common list implementations are ArrayList and LinkedList. The list interface contains methods for adding/removing elements at specific indexes or from the entire list, retrieving elements by index, and checking for presence of elements.

Uploaded by

Jatin Joshi
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/ 11

Set

The set interface present in the java.util package and extends


the Collection Interface is an unordered collection of objects in
which duplicate values cannot be stored. It is an interface which
implements the mathematical set. This interface contains the
methods inherited from the Collection interface and adds a feature
which restricts the insertion of the duplicate elements. 
Example of a Set:

import java.util.*;
public class SetExample{
public static void main(String[] args)
{
Set<String> hash_Set
= new HashSet<String>();

hash_Set.add(“Hello");
hash_Set.add(“World");
hash_Set.add(“Program");
hash_Set.add(“N");
hash_Set.add("Set");
System.out.println(hash_Set);
}
}
The following methods are present in the set interface:
METHOD DESCRIPTION
This method is used to add a specific element to the set. The
function adds the element only if the specified element is not
add(element) already present in the set else the function returns False if the
element is already present in the Set.
This method is used to append all of the elements from the
addAll(collection) mentioned collection to the existing set. The elements are
added randomly without following any specific order.

clear() This method is used to remove all the elements from the set
but not delete the set. The reference for the set still exists.

contains(element) This method is used to check whether a specific element is


present in the Set or not.
This method is used to check whether the set contains all the
containsAll(collection) elements present in the given collection or not. This method
returns true if the set contains all the elements and returns
false if any of the elements are missing.
This method is used to get the hashCode value for this instance
hashCode() of the Set. It returns an integer value which is the hashCode
value for this instance of the Set.
isEmpty() This method is used to check whether the set is empty or not.
This method is used to return the iterator of the set. The
iterator() elements from the set are returned in a random order.
This method is used to remove the given element from the set.
remove(element) This method returns True if the specified element is present in
the Set otherwise it returns False.
This method is used to remove all the elements from the
removeAll(collection) collection which are present in the set. This method returns
true if this set changed as a result of the call.
This method is used to retain all the elements from the set
retainAll(collection) which are mentioned in the given collection. This method
returns true if this set changed as a result of the call.

size() This method is used to get the size of the set. This returns an
integer value which signifies the number of elements.

toArray() This method is used to form an array of the same elements as


that of the Set.
List
List in Java provides the facility to maintain the ordered collection. It contains the
index-based methods to insert, update, delete and search the elements. It can have
the duplicate elements also. We can also store the null elements in the list.
The List interface is found in the java.util package and inherits the Collection
interface. It is a factory of ListIterator interface. Through the ListIterator, we can
iterate the list in forward and backward directions. The implementation classes of
List interface are ArrayList, LinkedList, Stack and Vector. The ArrayList and LinkedList
are widely used in Java programming. The Vector class is deprecated since Java 5.
Java List Methods
Method Description
void add(int index, E element) It is used to insert the specified element at the specified position in a
list.
boolean add(E e) It is used to append the specified element at the end of a list.
boolean addAll(Collection<? extends E> c) It is used to append all of the elements in the specified collection to
the end of a list.
boolean addAll(int index, Collection<? extends E> c) It is used to append all the elements in the specified collection,
starting at the specified position of the list.
void clear() It is used to remove all of the elements from this list.

boolean equals(Object o) It is used to compare the specified object with the elements of a list.
int hashcode() It is used to return the hash code value for a list.
E get(int index) It is used to fetch the element from the particular position of the list.
boolean isEmpty() It returns true if the list is empty, otherwise false.
int lastIndexOf(Object o) It is used to return the index in this list of the last occurrence of the
specified element, or -1 if the list does not contain this element.
Object[] toArray() It is used to return an array containing all of the elements in this list
in the correct order.
<T> T[] toArray(T[] a) It is used to return an array containing all of the elements in this list
in the correct order.
boolean contains(Object o) It returns true if the list contains the specified element
boolean containsAll(Collection<?> c) It returns true if the list contains all the specified element
int indexOf(Object o) It is used to return the index in this list of the first occurrence of the
specified element, or -1 if the List does not contain this element.
E remove(int index) It is used to remove the element present at the specified position in
the list.
boolean remove(Object o) It is used to remove the first occurrence of the specified element.
boolean removeAll(Collection<?> c) It is used to remove all the elements from the list.
void replaceAll(UnaryOperator<E> operator) It is used to replace all the elements from the list with the specified
element.
void retainAll(Collection<?> c) It is used to retain all the elements in the list that are present in the
specified collection.
E set(int index, E element) It is used to replace the specified element in the list, present at the
specified position.
void sort(Comparator<? super E> c) It is used to sort the elements of the list on the basis of specified
comparator.
Spliterator<E> spliterator() It is used to create spliterator over the elements in a list.
List<E> subList(int fromIndex, int toIndex) It is used to fetch all the elements lies within the given range.
int size() It is used to return the number of elements present in the list.
The ArrayList and LinkedList classes provide the implementation of List interface. Let's see
the examples to create the List:

//Creating a List of type String using ArrayList  
List<String> list=new ArrayList<String>();  
  
//Creating a List of type Integer using ArrayList  
List<Integer> list=new ArrayList<Integer>();  
  
//Creating a List of type Book using ArrayList  
List<Book> list=new ArrayList<Book>();  
  
//Creating a List of type String using LinkedList  
List<String> list=new LinkedList<String>(); 
Let's see a simple example of List where we are using the ArrayList class as the implementation.

import java.util.*;  
public class ListExample1{  
public static void main(String args[]){  
 //Creating a List  
 List<String> list=new ArrayList<String>();  
 //Adding elements in the List  
 list.add("Mango");  
 list.add("Apple");  
 list.add("Banana");  
 list.add("Grapes");  
 //Iterating the List element using for-each loop  
 for(String fruit:list)  
  System.out.println(fruit);  
  
}  
}  
Map

• The Map interface present in java.util package represents a mapping


between a key and a value. The Map interface is not a subtype of
the Collection Interface. Therefore it behaves a bit differently from
the rest of the collection types. A map contains unique keys.
Example:

import java.util.*;
class HashMapDemo {
public static void main(String args[])
{
Map<String, Integer> hm
= new HashMap<String, Integer>();

hm.put("a", new Integer(100));


hm.put("b", new Integer(200));
hm.put("c", new Integer(300));
hm.put("d", new Integer(400));

// Traversing through the map


for (Map.Entry<String, Integer> me : hm.entrySet()) {
System.out.print(me.getKey() + ":");
System.out.println(me.getValue());
}
}
}
Characteristics of a Map Interface
1.A Map cannot contain duplicate keys and each key can map to at most one value. Some implementations allow null key and
null value like the HashMap and LinkedHashMap, but some do not like the TreeMap.
2.The order of a map depends on the specific implementations. For example, TreeMap and LinkedHashMap have predictable order,
while HashMap does not.
3.There are two interfaces for implementing Map in java. They are, Map and SortedMap, and three classes: HashMap, TreeMap and
LinkedHashMap.
Why and When to use Maps?
Maps are perfect to use for key-value association mapping such as dictionaries. The maps are used to perform lookups by keys or
when someone wants to retrieve and update elements by keys. Some examples are:
•A map of error codes and their descriptions.
•A map of zip codes and cities.
•A map of managers and employees. Each manager (key) is associated with a list of employees (value) he manages.
•A map of classes and students. Each class (key) is associated with a list of students (value).
Creating Map Objects
Since Map is an interface, objects cannot be created of the type map. We always need a class which extends this map in order to
create an object. And also, after the introduction of Generics in Java 1.5, it is possible to restrict the type of object that can be stored
in the Map. This type-safe map can be defined as:
// Obj is the type of the object to be stored in Map
Map hm = new HashMap ();
Methods in Map Interface
METHOD DESCRIPTION
This method is used to clear and remove all of the elements or
clear()
mappings from a specified Map collection.
This method is used to check whether a particular key is being mapped
containsKey(Object) into the Map or not. It takes the key element as a parameter and
returns True if that element is mapped in the map.

This method is used to check whether a particular value is being


containsValue(Object) mapped by a single or more than one key in the Map. It takes the value
as a parameter and returns True if that value is mapped by any of the
key in the map.

This method is used to create a set out of the same elements contained
entrySet() in the map. It basically returns a set view of the map or we can create a
new set and store the map elements into them.

This method is used to check for equality between two maps. It verifies
equals(Object) whether the elements of one map passed as a parameter is equal to
the elements of this map or not.
This method is used to retrieve or fetch the value mapped by a
get(Object) particular key mentioned in the parameter. It returns NULL when the
map contains no such mapping for the key.
This method is used to generate a hashCode for the given map
hashCode()
containing key and values.
This method is used to check if a map is having any entry for key and
isEmpty() value pairs. If no mapping exists, then this returns true.
This method is used to return a Set view of the keys contained in this
keySet() map. The set is backed by the map, so changes to the map are reflected
in the set, and vice-versa.

put(Object, Object) This method is used to associate the specified value with the specified
key in this map.
putAll(Map) This method is used to copy all of the mappings from the specified map
to this map.
remove(Object) This method is used to remove the mapping for a key from this map if it
is present in the map.
size() This method is used to return the number of key/value pairs available
in the map.

values() This method is used to create a collection out of the values of the map.
It basically returns a Collection view of the values in the HashMap.

You might also like