0% found this document useful (0 votes)
0 views2 pages

Java Useful Methods

The document provides a summary of useful methods for various Java collection types, including HashMap, HashSet, Stack, ArrayList, and Queue. Each collection type is described along with its common methods for adding, removing, and accessing elements. Understanding these methods is essential for competitive programming, software engineering, and system design.

Uploaded by

harsainyamsingh6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

Java Useful Methods

The document provides a summary of useful methods for various Java collection types, including HashMap, HashSet, Stack, ArrayList, and Queue. Each collection type is described along with its common methods for adding, removing, and accessing elements. Understanding these methods is essential for competitive programming, software engineering, and system design.

Uploaded by

harsainyamsingh6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Collections: Useful Methods Summary

1. HashMap<K, V>

A map-based collection that stores key-value pairs. Keys are unique.

Common Methods: - put(K key, V value) – Adds a key-value pair to the map. - get(Object
key) – Returns the value associated with the key. - containsKey(Object key) – Checks if the key
exists. - containsValue(Object value) – Checks if the value exists. - remove(Object key) –
Removes the entry by key. - keySet() – Returns a set of all keys. - values() – Returns a collection
of all values. - entrySet() – Returns a set of all key-value pairs. - isEmpty() – Checks if map is
empty. - size() – Returns the number of entries. - clear() – Removes all entries.

2. HashSet<E>

Implements Set, backed by a HashMap. Stores unique elements only.

Common Methods: - add(E element) – Adds the element to the set. - contains(Object
element) – Checks if the set contains the element. - remove(Object element) – Removes the
element if it exists. - isEmpty() – Checks if the set is empty. - size() – Returns the number of
elements. - clear() – Removes all elements. - iterator() – Returns an iterator over elements.

3. Stack<E>

A subclass of Vector that implements a LIFO stack.

Common Methods: - push(E item) – Pushes item on top. - pop() – Removes and returns the top
item. - peek() – Returns top item without removing it. - empty() – Checks if the stack is empty. -
search(Object o) – Returns 1-based position from the top of the stack. - size() – Number of
elements.

4. ArrayList<E>

A resizable array implementation of the List interface.

Common Methods: - add(E element) – Adds element to end. - add(int index, E element) –
Inserts element at specified position. - get(int index) – Returns element at index. - set(int
index, E element) – Replaces element at index. - remove(int index) – Removes element at
index. - remove(Object o) – Removes the first occurrence. - contains(Object o) – Checks if
element exists. - size() – Returns number of elements. - isEmpty() – Checks if list is empty. -
clear() – Removes all elements. - indexOf(Object o) – Returns first index of element. -

1
lastIndexOf(Object o) – Returns last index. - subList(int fromIndex, int toIndex) –
Returns a sub-list.

5. Queue<E> (Using LinkedList)

Queue follows FIFO principle. Often implemented using LinkedList.

Common Methods: - add(E element) – Adds element; throws exception if full. - offer(E
element) – Adds element; returns false if fails. - remove() – Removes and returns head; throws
exception if empty. - poll() – Removes and returns head; returns null if empty. - element() –
Retrieves head; throws exception if empty. - peek() – Retrieves head; returns null if empty. -
isEmpty() – Checks if queue is empty. - size() – Number of elements.

These methods are essential for competitive programming, software engineering, and system design.
Practice them to get comfortable manipulating collections in Java.

You might also like