Java Collections
Java Collections
The Java Collections API provide Java developers with a set of classes and interfaces that
makes it easier to work with collections of objects, e.g. lists, maps, stacks etc.
Rather than having to write your own collection classes, Java provides these ready-to-
use collection classes for you.
Array :
3. The main advantage of array is we can represent huge no of elements by using single variable. So that
readability of the code will be improved.
1. Collections are growable in nature that is based on our requirement we can increase (or) decrease the
size hence memory point of view collections concept is recommended to use.
3. Every collection class is implemented based on some standard data structure hence for every
requirement ready-made method support is available being a programmer we can use these methods
directly without writing the functionality on our own.
ArrayList<Object> list = new ArrayList<>();
[Link](new Student("Alice", 20));
[Link](new Customer("C123", "123 Main St"));
ARRAYS COLLECTIONS
1) Fixed in size. 1. Collections are growable in nature.
2) Memory point of view arrays are not 2. Memory point of view collections are
recommended to use. highly recommended to use.
3) Performance point of view arrays are 3. Performance point of view collections are
recommended to use not recommended to use.
4) Arrays can hold only homogeneous data type 4. Collections can hold both homogeneous
elements. and heterogeneous elements.
5) There is no underlying data structure for 5. Every collection class is implemented
arrays and hence there is no readymade based on some standard data structure
method support. and hence readymade method support is
available.
6) Arrays can hold both primitives and object 6. Collections can hold only objects but not
types. primitives.
Collection:
If we want to represent a group of objects as single entity then we should go for collections.
Collection framework:
It defines several classes and interfaces to represent a group of objects as a single entity.
Java Collection
Java List
Java Set
Java SortedSet
Java navigableSet
Java Map
Java SortedMap
Java NavigableMap
Java Queue
Collection
1. If we want to represent a group of "individual objects" as a single entity then we should go for
collection.
2. In general we can consider collection as root interface of entire collection framework.
3. Collection interface defines the most common methods which can be applicable for any collection
object.
List :
1. It is the child interface of Collection.
2. If we want to represent a group of individual objects as a single entity where "duplicates are allow and
insertion order must be preserved" then we should go for List interface
SET :
1. It is the child interface of Collection.
2. If we want to represent a group of individual objects as single entity "where duplicates are not allow
and insertion order is not preserved" then we should go for Set interface.
SORTED SET :
1. It is the child interface of Set.
2. If we want to represent a group of individual objects as single entity "where duplicates are not allow
but all objects will be insertion according to some sorting order then we should go for SortedSet. (or)
3. If we want to represent a group of "unique objects" according to some sorting order then we should
go for SortedSet.
NavigableSet:
Queue:
2. If we want to represent a group of individual objects prior to processing then we should go for queue
concept
Note: All the above interfaces (Collection, List, Set, SortedSet, NavigableSet, and Queue) meant for
representing a group of individual objects. If we want to represent a group of objects as key-value pairs
then we should go for Map.
Map:
2. If we want to represent a group of objects as key-value pairs then we should go for Map interface.
SortedMap:
NavigableMap:
1) It is the child interface of SortedMap and defines several methods for navigation purposes.
The Java Collections class, [Link], contains a long list of utility methods for
working with collections in Java.
addAll()
The Java Collections addAll() method can add a variable number of elements to
a Collection (typically either a List or a Set . Here is a java code example of calling
the Collections addAll() method:
binarySearch()
The Collections binarySearch() method can search a Java List for an element using a binary search
algorithm. The List must be sorted in ascending order before you search it using binarySearch() . See the
tutorial about sorting Java Lists for more information about how to sort a List in ascending order. Here is
an example of searching a List using the Collections binarySearch() method:
copy()
The Collections copy() method can copy all elements of a List into another List. Here is a Java
example of calling the Collections copy() method:
reverse()
The Collections reverse() method can reverse the elements in a Java List.
Here is an example of reversing the elements of a List:
[Link]("one");
[Link]("two");
[Link]("three");
[Link](list);
After executing the above code, the sequence of the elements in the List will
be three, two, one .
shuffle()
The Collections shuffle() method can shuffle the elements of a List. Here is an example of
shuffling a list with the Collections shuffle() method:
[Link](list);
sort()
The Collections sort() method can sort a Java [Link] is an example of sorting a
Java List using Collections sort() method:
[Link]("one");
[Link]("two");
[Link]("three");
[Link]("four");
[Link](list);
After running this code the order of the elements in the List will be four, one, three, four, as
the String elements will be sorted alphabetically.
min()
The Collections min() method can find the minimum element in a List according to the
natural ordering of the elements (see my Java List sorting tutorial). Here is an example
of finding the minimum element in a Java List using Collections min() method:
After running the code above, the min variable will contain the String value 1 .
max()
The Collections max() method can find the maximum element in a List according to the
natural order of the elements. Here is an example of finding the maximum element in a
Java List:
After running the code above, the max variable will contain the String value 3 .
replaceAll()
The Java Collections replaceAll() method can replace all occurrences of one
element with another element. You pass the element to replace and the element
to replace it with as parameters to the replaceAll() method.
The Collections replaceAll() method returns true if any elements were replaced,
and false if not. Here is an example of replacing all occurrences of one element
with another in a Java List:
After executing this example, the source List will contain the elements C, B and C.
The replacedAny variable will contain the value true because at least one element
was replaced in the List.
The Collections replaceAll() method uses the equals() method of each element
to determine if the element is equal to the element to replace or not. I have a
written a few more details about how the equals() method works in my section
about the Java equals() method.
unmodifiableSet()
Collection--------------------interface
Collections------------------class
Collection interface:
The Java Collection interface ([Link]) is one of the root interfaces of the Java Collection API.
Though you do not instantiate a Collection directly, but rather a subtype of Collection, you may often
treat these subtypes uniformly as a Collection.
If we want to represent a group of individual objects as a single entity then we should go for Collection
interface. This interface defines the most common general methods which can be applicable for any
Collection object.
The following is the list of methods present in Collection interface.
6. Void clear();
9. boolean isEmpty();
Create a Collection
As just mentioned above, you do not create a Collection instance directly, but an instance of
one of the subtypes of Collection. Here is an example of creating a List which is a subtype
of Collection:
Collection Subtypes
The following interfaces (collection types) extends the Java Collection interface:
List
Set
SortedSet
NavigableSet
Queue
Deque
Java does not come with a usable implementation of the Collection interface, so you will have
to use one of the listed subtypes. The Collection interface just defines a set of methods
(behaviour) that each of these Collection subtypes share. This makes it possible ignore what
specific type of Collection you are using, and just treat it as a Collection. This is standard
inheritance, so there is nothing magical about, but it can still be a nice feature from time to
time. Later sections in this text will describe the most used of these common operations.
And here are a few ways to call this method with different Collection subtypes:
[Link](set);
[Link](list);
Regardless of what Collection subtype you are using there are a few standard methods to add
elements to a Collection. Adding an element to a Collection is done via the add() method. Here
is an example of adding an element to a Java Collection:
The remove() method removes the given element from the Collection and returns true if the
removed element was present in the Collection, and was removed. If the element was not
present, the remove() method returns false. Here is an example of removing an element from a
Java Collection:
You can also add a collection of objects to a Java Collection using the addAll(). Here is an
example of adding a collection of objects to a Java Collection:
The Java Collection addAll() adds all elements found in the Collection passed as parameter to
the method. The Collection object itself is not added. Only its elements. If you had
called add() with the Collection as parameter instead, the Collection object itself would have
been added, not its elements.
Exactly how the addAll() method behaves depends on the Collection subtype. Some Collection
subtypes allows the same element to be added more than once, and others don't.
The Java Collection removeAll() removes all elements found the Collection passed as parameter
to the method. If the Collection parameter contains any elements not found the target
collection, these are just ignored. Here is an example of removing a collection of elements from
a Java Collection:
[Link](objects);
The Java Collection retainAll() does the opposite of removeAll(). Instead of removing all the
elements found in the parameter Collection, it keeps all these elements, and removes all other
elements. Keep in mind, that only if the elements were already contained in the target
collection, are they retained. Any new elements found in the parameter Collection which are
not in the target collection, are not automatically added. They are just ignored. Here is an
example of retaining all elements from one Colletion in another Java Collection:
[Link]("A");
[Link]("B");
[Link]("C");
[Link]("1");
[Link]("2");
[Link]("3");
The Collection interface has two methods to check if a Collection contains one or more certain
elements. These are the contains() and containsAll() methods. They are illustrated here:
Collection collection = new HashSet();
boolean containsElement = [Link]("an element");
contains() returns true if the collection contains the element, and false if not.
containsAll() returns true if the collection contains all the elements in the parameter collection,
and false if not.
Collection Size
You can check the size of a collection using the size() method. By "size" is meant the number of
elements in the collection. Here is an example:
Iterate a Collection
You can iterate all elements of a collection. This is done by obtaining an Java Iterator from the
collection, and iterate through that. Here is how it looks:
You can also iterate a Java Collection using the Java for-each loop :
List interface:
It is the child interface of Collection.
If we want to represent a group of individual objects as a single entity where duplicates are allow and
insertion order is preserved. Then we should go for List.
We can differentiate duplicate objects and we can maintain insertion order by means of index hence
"index play very important role in List".
8. ListIterator listIterator();
ArrayList:
1. The underlying data structure is resizable array (or) growable array.
4. Heterogeneous objects are allowed.(except TreeSet , TreeMap every where heterogenious objects are
allowed)
Constructor :
Usually we can use collection to hold and transfer objects from one tier to another tier. To provide support for
this requirement every Collection class already implements Serializable and Cloneable interfaces.
ArrayList and Vector classes implements RandomAccess interface so that any random element we can access
with the same speed. Hence ArrayList is the best choice of "retrival operation".
RandomAccess interface present in util package and doesn't contain any methods. It is a marker interface.
LIST VS ARRAYLIST :
Example:
Similarly we can get synchronized version of Set and Map objects by using the following methods. 1)
public static Set synchronizedSet(Set s); 2) public static Map synchronizedMap(Map m);
ArrayList is the worst choice if our frequent operation is insertion (or) deletion in the middle because it
requires several internal shift operations.
LinkedList :
1. The underlying data structure is double LinkedList
2. If our frequent operation is insertion (or) deletion in the middle then LinkedList is the best choice.
Usually we can use LinkedList to implement Stacks and Queues. To provide support for this requirement
LinkedList class defines the following 6 specific methods.
3. Object getFirst();
4. Object getLast();
5. Object removeFirst();
6. Object removeLast(); We can apply these methods only on LinkedList object.
Constructors:
2. LinkedList l=new LinkedList(Collection c); To create an equivalent LinkedList object for the given
collection.
Vector:
1. The underlying data structure is resizable array (or) growable array.
6. Implements Serializable, Cloneable and RandomAccess interfaces. Every method present in Vector is
synchronized and hence Vector is Thread safe.
1. add(Object o);-----Collection
3. addElement(Object o);-----
1. remove(Object o);--------Collection
2. remove(int index);--------------List 3
. removeElement(Object o);----Vector
4. removeElementAt(int index);-----Vector
5. removeAllElements();-----Vector
6. clear();-------
3. Object firstElement();--------------Vector
4. Object lastElement();---------------
3. Enumeration elements();
Constructors:
Creates an empty Vector object with default initial capacity 10. o Once Vector reaches its maximum
capacity then a new Vector object will be created with double capacity. That is
"newcapacity=currentcapacity*2".
Stack:
1. It is the child class of Vector.
2. Whenever last in first out(LIFO) order required then we should go for Stack. Constructor: It contains
only one constructor.
Methods:
2. Object pop();
3. Object peek();
Use ArrayList when you need fast random access (get(index)) and less memory overhead.
Use LinkedList when you have frequent insertions and deletions (especially in the middle)
and don't need fast random access
1. Enumeration
2. Iterator
3. ListIterator
Enumeration:
1. We can use Enumeration to get objects one by one from the legacy collection objects.
Enumeration e=[Link]();
using Vector Object Enumeration interface defines the following two methods
Limitations of Enumeration:
1. We can apply Enumeration concept only for legacy classes and it is not a universal cursor.
2. By using Enumeration we can get only read access and we can't perform remove operations.
Vector
Hashtable
Properties
Stack
Enumeration is one of the oldest cursor interfaces in Java, introduced in JDK 1.0. It is
used to iterate over elements of legacy collection classes like Vector and Hashtable.
However, it has several limitations, which led to the introduction of more advanced
iterators like Iterator and ListIterator
Iterator:
1. We can use Iterator to get objects one by one from any collection object.
2. We can apply Iterator concept for any collection object and it is a universal cursor.
3. While iterating the objects by Iterator we can perform both read and remove operations. We can get
Iterator object by using iterator() method of Collection interface.
Iterator itr=[Link]();
Limitations of Iterator :
1. Both enumeration and Iterator are single direction cursors only. That is we can always move only
forward direction and we can't move to the backward direction.
2. While iterating by Iterator we can perform only read and remove operations and we can't perform
replacement and addition of new objects.
ListIterator:
1. ListIterator is the child interface of Iterator.
2. By using listIterator we can move either to the forward direction (or) to the backward direction that is
it is a bi-directional cursor.
3. While iterating by listIterator we can perform replacement and addition of new objects in addition to
read and remove operations By using listIterator method we can create listIterator object.
The most powerful cursor is listIterator but its limitation is it is applicable only for "List objects".
Set interface:
1. It is the child interface of Collection.
2. If we want to represent a group of individual objects as a single entity where duplicates are not allow
and insertion order is not preserved then we should go for Set interface.
Set interface does not contain any new method we have to use only Collection interface methods.
HashSet :
2. Insertion order is not preserved and it is based on hash code of the objects. 3. Duplicate objects are
not allowed.
4. If we are trying to insert duplicate objects we won't get compile time error and runtime error add()
method simply returns false.
7. Implements Serializable and Cloneable interfaces but not RandomAccess. 8. HashSet is best suitable, if
our frequent operation is "Search".
Constructors:
Java provides multiple constructors for HashSet, each serving different purposes:
java
CopyEdit
HashSet<Integer> set = new HashSet<>(32);
java
CopyEdit
HashSet<Integer> set = new HashSet<>(32, 0.5f);
java
CopyEdit
ArrayList<String> list = new ArrayList<>();
[Link]("A");
[Link]("B");
HashSet<String> set = new HashSet<>(list);
The load factor (fill ratio) determines when the HashSet increases its capacity.
Formula for resizing:
Example:
java
CopyEdit
HashSet<Integer> set = new HashSet<>(8, 0.5f);
Initial Capacity = 8
Load Factor = 0.5
Resize happens after 8 * 0.5 = 4 elements are inserted.
After resizing, the new capacity becomes 16.
A lower load factor (e.g., 0.5) reduces collisions but increases memory usage.
A higher load factor (e.g., 0.9) reduces memory usage but may cause more collisions, affecting
performance.
LinkedHashSet :
1. It is the child class of HashSet.
TreeSet:
TreeSet is a part of Java's [Link] package and implements the SortedSet interface. It is a
sorted collection that stores unique elements in a sorted order.
Unlike HashSet, which uses hashing, TreeSet is implemented using a self-balancing Red-
Black tree. This ensures that all operations like insertion, deletion, and lookup run in O(log n)
time.
3. Insertion order is not preserved and it is based on some sorting order of objects.
4. Heterogeneous objects are not allowed if we are trying to insert heterogeneous objects then we will
get ClassCastException.
TreeSet Constructors
1. TreeSet t = new TreeSet();
Creates an empty TreeSet that maintains elements in natural sorting order (ascending
order for numbers, lexicographic order for strings).
Elements must be comparable, otherwise, it will throw ClassCastException.
Example:
import [Link];
Explanation:
Creates an empty TreeSet but allows custom sorting order using a Comparator.
Useful when you need descending order sorting or custom sorting logic.
java
CopyEdit
import [Link].*;
[Link](50);
[Link](20);
[Link](10);
[Link](40);
Explanation:
Example:
java
CopyEdit
import [Link].*;
Explanation:
Example:
java
CopyEdit
import [Link].*;
public class TreeSetFromCollection {
public static void main(String[] args) {
List<Integer> list = [Link](30, 10, 20, 10, 50, 20);
Explanation:
Key Takeaways
Constructor Description
TreeSet(Comparator c) Creates a TreeSet with custom sorting order (e.g., descending order).
TreeSet(SortedSet s) Creates a TreeSet from an existing SortedSet, keeping the same order.
Null acceptance:
For the empty TreeSet as the 1st element "null" insertion is possible but after inserting that null if we
are trying to insert any other we will get NullPointerException.
For the non empty TreeSet if we are trying to insert null then we will get NullPointerException
Comparable interface:
Comparable interface present in [Link] package and contains only one method compareTo()
method.
Java Comparable interface is used to order the objects of the user-defined class. This interface
is found in [Link] package and contains only one method named compareTo(Object). It
provides a single sorting sequence only, i.e., you can sort the elements on the basis of single
data member only. For example, it may be rollno, name, age or anything else.
If we are depending on default natural sorting order then internally JVM will use
compareTo() method to arrange objects in sorting order.
If we are not satisfying with default natural sorting order (or) if default natural
sorting order is not available then we can define our own customized sorting by
Comparator object.
Comparable meant for default natural sorting order.
Comparator meant for customized sorting order.
Comparator interface:
Comparator interface present in [Link] package this interface defines the following 2 methods.
[Link](10);
[Link](0);
[Link](15);
[Link](5);
[Link](20);
[Link](t);
}
At line "1" if we are not passing Comparator object then JVM will always calls compareTo()
method which is meant for default natural sorting order(ascending order)hence in this case the
output is [0, 5, 10, 15, 20].
At line "1" if we are passing Comparator object then JVM calls compare() method of
MyComparator class which is meant for customized sorting order(descending order) hence in
this case the output is [20, 15, 10, 5, 0].
Requirement: Write a program to insert String objects into the TreeSet where the sorting order
is reverse of alphabetical order.
Comparable vs Comparator:
For predefined Comparable classes default natural sorting order is already available if we are
not satisfied with default natural sorting order then we can define our own customized sorting
order by Comparator.
For predefined non Comparable classes [like StringBuffer] default natural sorting order is not
available we can define our own sorting order by using Comparator object.
For our own classes [like Customer, Student, and Employee] we can define default natural
sorting order by using Comparable interface. The person who is using our class, if he is not
satisfied with default natural sorting order then he can define his own sorting order by using
Comparator object
1. ArrayList Questions
Q1. Find the frequency of an element in an ArrayList.
java
Copy
import [Link];
import [Link];
int element = 2;
int frequency = [Link](list, element);
[Link]("Frequency of " + element + ": " + frequency);
}
}
Q2. Remove duplicates from an ArrayList.
java
Copy
import [Link];
import [Link];
2. LinkedList Questions
Q3. Reverse a LinkedList.
java
Copy
import [Link];
import [Link];
java
Copy
import [Link];
3. Set Questions
Q5. Check if two arrays have the same elements (ignoring duplicates).
Write a program to check if two arrays contain the same elements, ignoring
duplicates, using a Set.
java
Copy
import [Link];
import [Link];
import [Link];
java
Copy
import [Link];
import [Link];
// Union
Set<Integer> union = new HashSet<>(set1);
[Link](set2);
[Link]("Union: " + union);
// Intersection
Set<Integer> intersection = new HashSet<>(set1);
[Link](set2);
[Link]("Intersection: " + intersection);
}
}
4. Mixed Questions
Q7. Convert an ArrayList to a LinkedList and vice versa.
java
Copy
import [Link];
import [Link];
import [Link];
// LinkedList to ArrayList
List<String> newArrayList = new ArrayList<>(linkedList);
[Link]("LinkedList to ArrayList: " + newArrayList);
}
}
java
Copy
import [Link];
import [Link];
Key Takeaways
ArrayList: Good for random access and iteration but slower for
insertions/deletions in the middle.
java
Copy
import [Link];
import [Link];
// Ascending order
[Link](list);
[Link]("Ascending Order: " + list);
// Descending order
[Link](list, [Link]());
[Link]("Descending Order: " + list);
}
}
java
Copy
import [Link];
java
Copy
import [Link];
import [Link];
2. LinkedList Questions
Q12. Remove the first and last element from a LinkedList.
Write a program to remove the first and last element from a LinkedList.
java
Copy
import [Link];
java
Copy
import [Link];
import [Link];
java
Copy
import [Link];
import [Link];
3. Set Questions
Q15. Find the difference between two sets.
java
Copy
import [Link];
import [Link];
java
Copy
import [Link];
import [Link];
java
Copy
import [Link];
import [Link];
import [Link];
4. Mixed Questions
Q18. Find the intersection of an ArrayList and a LinkedList.
java
Copy
import [Link];
import [Link];
import [Link];
import [Link];
Q19. Remove all elements from a LinkedList that are present in a Set.
Write a program to remove all elements from a LinkedList that are present in a Set.
java
Copy
import [Link];
import [Link];
import [Link];
java
Copy
import [Link];
import [Link];
import [Link];
import [Link];
Key Takeaways
ArrayList: Best for frequent read operations and random access.
Set: Ensures uniqueness and is useful for operations like union, intersection, and
difference.
These additional questions will help you practice and master the Java Collection
Framework. Keep coding! 🚀
MAP :
1. If we want to represent a group of objects as "key-value" pair then we should go for Map interface.
Object put(Object key,Object value); To add an entry to the Map, if key is already available then
the old value replaced with new value and old value will be returned.
void putAll(Map m);
Object get(Object key);
Object remove(Object key);
It removes the entry associated with specified key and returns the corresponding
value.
boolean containsKey(Object key);
boolean containsValue(Object value);
boolean isEmpty();
Int size();
void clear();
Set keySet();
The set of keys we are getting.
Collection values();
The set of values we are getting.
Set entrySet();
The set of entryset we are getting
Entry interface:
Each key-value pair is called one entry. Hence Map is considered as a group of entry Objects, without
existing Map object there is no chance of existing entry object hence interface entry is define inside Map
interface(inner interface).
HashMap:
1. The underlying data structure is Hashtable.
3. Insertion order is not preserved and it is based on hash code of the keys.
5. Null is allowed for keys(only once) and for values(any number of times).
Constructors:
There are multiple ways to iterate over a HashMap in Java. Below are the different approaches:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Node Class :
Hash Function
During the insertion (put) of a key-value pair, the HashMap first
calculates the hash code of the key. The hash function then
computes an integer for the key. Classes can use
the hashCode method of the Object class or override this method
and provide their own implementation. (Read about the hash
code contract here). The hash code is then XORed (eXclusive
OR) with its upper 16 bits (h >>> 16) to achieve a more uniform
distribution.
XOR is a bitwise operation that compares two bits, resulting in 1
if the bits are different and 0 if they are the same. In this
context, performing a bitwise XOR operation between the hash
code and its upper 16 bits (obtained using the unsigned right
shift >>> operator) helps to mix the bits, leading to a more
evenly distributed hash code.
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = [Link]()) ^ (h >>> 16);
}
Index Calculation
Once the hash code for a key is generated,
the HashMap calculates an index within the array of buckets to
determine where the key-value pair will be stored. This is done
using a bitwise AND operation, which is an efficient way to
calculate the modulo when the array length is a power of two.
int index = (n - 1) & hash;
[Link](key, value);
Get operation:A get operation retrieves data from a data structure or an entry
from a bucket in a hashmap.
Chaining/Linking: Each bucket in the array is a linked
list of nodes. If a key already exists at a particular index
and another key gets hashed to the same index, it gets
appended to the list.
Treeify: If the number of nodes exceeds a certain
threshold, the linked list is converted into a tree (This
was introduced in Java 8).
Time Complexity
The basic operations of a HashMap, such as put, get, and remove,
generally offer constant time performance of O(1), assuming
that the keys are uniformly distributed. In cases where there is
poor key distribution and many collisions occur, these
operations might degrade to a linear time complexity of O(n).
Under treeification, where long chains of collisions are
converted into balanced trees, lookup operations can improve
to a more efficient logarithmic time complexity of O(log n).
TreeMap:
1. The underlying data structure is RED-BLACK Tree.
3. Insertion order is not preserved and all entries will be inserted according to some sorting order of
keys.
4. If we are depending on default natural sorting order keys should be homogeneous and Comparable
otherwise we will get ClassCastException.
5. If we are defining our own sorting order by Comparator then keys can be heterogeneous and non
Comparable.
6. There are no restrictions on values they can be heterogeneous and non Comparable.
7. For the empty TreeMap as first entry null key is allowed but after inserting that entry if we are trying
to insert any other entry we will get NullPointerException.
8. For the non empty TreeMap if we are trying to insert an entry with null key we will get
NullPointerException.
Constructors:
2. If we want to represent a group of individual objects prior (happening before something else) to
processing then we should go for Queue interface.
3. Usually Queue follows first in first out(FIFO) order but based on our requirement we can implement
our own order also.
5. LinkedList based implementation of Queue always follows first in first out order.
Link : [Link]
Link : [Link]
Link : [Link]
Link : [Link]
Write a Java program that takes a string as input and returns a Map<String,
Integer> where the key is each unique word in the string, and the value is the
frequency of that word in the string.
Requirements:
1. Ignore case sensitivity (e.g., "Hello" and "hello" should be treated as
the same word).
Answer:
java
Copy
import [Link].*;
Answer:
java
Copy
import [Link].*;
[Link](sortedMap);
}
}