0% found this document useful (0 votes)
34 views4 pages

Java Collections: Comprehensive Guide

The document provides a comprehensive reference for Java Collections and Data Types, detailing the methods and time complexities for various data structures including ArrayList, LinkedList, HashSet, HashMap, Queue, Stack, and PriorityQueue. It also includes comparisons between ArrayList and LinkedList, HashSet and HashMap, as well as Queue, Stack, and PriorityQueue. Additionally, it covers String and Integer methods with their respective complexities.

Uploaded by

sssuriya287
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)
34 views4 pages

Java Collections: Comprehensive Guide

The document provides a comprehensive reference for Java Collections and Data Types, detailing the methods and time complexities for various data structures including ArrayList, LinkedList, HashSet, HashMap, Queue, Stack, and PriorityQueue. It also includes comparisons between ArrayList and LinkedList, HashSet and HashMap, as well as Queue, Stack, and PriorityQueue. Additionally, it covers String and Integer methods with their respective complexities.

Uploaded by

sssuriya287
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

Java Collections & Data Types - Complete Reference

ArrayList
-> add(E e): Adds element to list. O(1)

-> add(int index, E e): Inserts element at specific index. O(n)

-> get(int index): Returns element at index. O(1)

-> set(int index, E e): Replaces element at index. O(1)

-> remove(int index): Removes element at index. O(n)

-> contains(Object o): Checks if element exists. O(n)

-> size(): Returns number of elements. O(1)

-> clear(): Removes all elements. O(n)

-> ensureCapacity(int minCapacity): Increases internal capacity. O(n)

LinkedList
-> add(E e): Adds element at end. O(1)

-> addFirst(E e): Adds element at beginning. O(1)

-> addLast(E e): Adds element at end. O(1)

-> get(int index): Returns element at index. O(n)

-> removeFirst(): Removes first element. O(1)

-> removeLast(): Removes last element. O(1)

-> peek(): Retrieves first element without removing. O(1)

-> poll(): Retrieves and removes first element. O(1)

-> isEmpty(): Checks if list is empty. O(1)

HashSet
-> add(E e): Adds element if not present. O(1) average

-> remove(Object o): Removes element. O(1) average

-> contains(Object o): Checks if element exists. O(1) average

-> size(): Returns number of elements. O(1)

-> isEmpty(): Checks if set is empty. O(1)

-> clear(): Removes all elements. O(n)

-> iterator(): Returns iterator. O(1)


HashMap
-> put(K key, V value): Inserts or updates key-value pair. O(1) average

-> get(Object key): Returns value for key. O(1) average

-> remove(Object key): Removes key-value mapping. O(1) average

-> containsKey(Object key): Checks if key exists. O(1)

-> containsValue(Object value): Checks if value exists. O(n)

-> keySet(): Returns set of keys. O(1)

-> values(): Returns collection of values. O(1)

-> entrySet(): Returns set of key-value pairs. O(1)

-> size(): Returns size. O(1)

-> clear(): Removes all entries. O(n)

Queue
-> offer(E e): Adds element to queue. O(1)

-> poll(): Retrieves and removes head. O(1)

-> peek(): Retrieves head without removing. O(1)

-> isEmpty(): Checks if queue is empty. O(1)

-> size(): Returns number of elements. O(1)

Stack
-> push(E e): Pushes element onto stack. O(1)

-> pop(): Removes and returns top element. O(1)

-> peek(): Returns top element without removing. O(1)

-> search(Object o): Searches element position. O(n)

-> empty(): Checks if stack is empty. O(1)

PriorityQueue
-> offer(E e): Inserts element according to priority. O(log n)

-> poll(): Retrieves and removes head (min). O(log n)

-> peek(): Retrieves head without removing. O(1)

-> size(): Returns number of elements. O(1)

-> isEmpty(): Checks if empty. O(1)

-> comparator(): Returns comparator used. O(1)


String
-> charAt(int index): Returns char at index. O(1)

-> substring(int start, int end): Returns substring. O(n)

-> length(): Returns length of string. O(1)

-> contains(CharSequence s): Checks substring presence. O(n)

-> equals(Object another): Compares strings. O(n)

-> equalsIgnoreCase(String another): Compares ignoring case. O(n)

-> toLowerCase(): Converts to lowercase. O(n)

-> toUpperCase(): Converts to uppercase. O(n)

-> split(String regex): Splits string. O(n)

-> replace(CharSequence target, CharSequence replacement): Replaces text. O(n)

-> trim(): Removes leading/trailing spaces. O(n)

Integer
-> parseInt(String s): Converts string to int. O(n)

-> valueOf(String s): Returns Integer object. O(n)

-> toString(int i): Converts int to string. O(n)

-> compare(int x, int y): Compares two ints. O(1)

-> intValue(): Returns primitive int value. O(1)

Comparison Tables
ArrayList vs LinkedList:

- Structure: Array vs Doubly Linked List

- Access Time: O(1) vs O(n)

- Insert/Delete (Middle): O(n) vs O(1)

- Memory Usage: Lower vs Higher

HashSet vs HashMap:

- Data: Only keys vs Key-Value pairs

- Nulls: One null element vs One null key, many null values

- Order: Unordered vs Unordered

- Duplicate Handling: Not allowed vs Unique keys


Queue vs Stack vs PriorityQueue:

- Queue: FIFO order, O(1) add/remove

- Stack: LIFO order, O(1) push/pop

- PriorityQueue: Ordered by priority, O(log n) add/remove

You might also like