Set, List, Map Presentation
Set, List, Map Presentation
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.
size() This method is used to get the size of the set. This returns an
integer value which signifies the number of elements.
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
import java.util.*;
class HashMapDemo {
public static void main(String args[])
{
Map<String, Integer> hm
= new HashMap<String, Integer>();
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.