DataStructure in Java
DataStructure in Java
Mayet Gizachew
College of Informatics
Department of Computer Science
1 List
ArrayList
2 Set
HashSet
3 Map∥DictionaryofJava
HashMap
4 Queue
Package: java.util
Superinterface: Collection<E>
Characteristics:
Maintains insertion order
Allows duplicate elements
Provides positional access via index
Common Implementations:
ArrayList
LinkedList
Vector
Stack
Description:
Resizable array implementation of List
Fast random access
Slower insertions/deletions in the middle
Method Description
add(E e) Appends element
add(int, E) Inserts at index
get(int) Gets element at index
set(int, E) Replaces element
remove(int) Removes element at index
remove(Object) Removes by value
size() Returns number of elements
isEmpty() Checks if list is empty
contains(Object) Checks presence
indexOf(Object) Returns index of element
clear() Removes all elements
toArray() Converts to array
Package: java.util
Superinterface: Collection<E>
Characteristics:
Does not allow duplicate elements
No guaranteed order (unless using SortedSet or LinkedHashSet)
Allows at most one null element (in most implementations)
Common Implementations:
HashSet
LinkedHashSet
TreeSet
Description:
Backed by a hash table
No guaranteed order of elements
Constant time for add, remove, and contains (on average)
Allows one null element
Method Description
add(E e) Adds element if not already present
remove(Object o) Removes specified element
contains(Object o) Checks if element exists
isEmpty() Returns true if set is empty
size() Returns the number of elements
clear() Removes all elements
iterator() Returns iterator over elements
Package: java.util
Not a subtype of: Collection
Characteristics:
Stores key-value pairs
Keys must be unique
Values can be duplicated
Accessed via keys, not indices
Common Implementations:
HashMap
LinkedHashMap
TreeMap
Hashtable (Legacy)
Description:
Backed by a hash table
No guaranteed order of keys
Allows one null key and multiple null values
Average constant time for operations
Method Description
put(K key, V value) Adds or updates entry
get(Object key) Gets value by key
remove(Object key) Removes entry by key
containsKey(Object key) Checks if key exists
containsValue(Object value) Checks if value exists
keySet() Returns set of keys
values() Returns collection of values
entrySet() Returns set of key-value pairs
clear() Removes all entries
Package: java.util
Extends: Collection<E>
Characteristics:
Represents FIFO (First-In-First-Out) data structure
Common operations: offer(), poll(), peek()
Allows null elements? No (throws NullPointerException on null
insertion)
Class Description
LinkedList Doubly-linked list, supports Queue and Deque
ArrayDeque Resizable-array implementation, faster than LinkedList
PriorityQueue Elements ordered by natural order or comparator