OopsWithJava-Unit-4-by-MultiAtoms (1) - Removed
OopsWithJava-Unit-4-by-MultiAtoms (1) - Removed
No Duplicate Elements: Ensures that no duplicate elements are added to the set.
Unordered: Does not maintain any order of elements. The order of elements is not predictable.
● Ordering: Elements in a SortedSet are sorted according to their natural ordering or by a specified
comparator.
● Subsets: Provides methods to view subsets of the set, which are themselves sorted.
Multi Atoms Plus
Commonly Used
Methods:
● Comparator<? super E> comparator(): Returns the comparator used to sort the set, or null
if the set is sorted according to natural ordering.
● SortedSet<E> headSet(E toElement): Returns a view of the portion of the set whose
elements are strictly less than toElement.
● SortedSet<E> tailSet(E fromElement): Returns a view of the portion of the set whose
elements are greater than or equal to fromElement.
● Sorted Order: Maintains elements in a sorted order, either by their natural ordering or by a
custom comparator.
● No Duplicates: Does not allow duplicate elements.
● Efficient Operations: Provides O(log n) time complexity for basic operations such as add,
remove, and contains.
Multi Atoms Plus
Multi Atoms Plus
Multi Atoms Plus
Map Interface
The Map interface in Java represents a collection of key-value pairs, where each key is associated with
exactly one value. It does not extend the Collection interface. The Map interface provides a way to store
data in a key-value pair format, where you can efficiently retrieve, update, and delete values based on
their keys.
● Key-Value Pairs: Maps store data as key-value pairs, where each key is
unique and maps to a single value.
● No Duplicate Keys: Keys must be unique. If a key is duplicated, the new
value will replace the old value.
● Efficient Operations: Provides efficient methods for adding, removing, and
accessing key-value pairs.
Multi Atoms Plus
Commonly Used
Methods:
● V put(K key, V value): Associates the specified value with the specified key in the map. If
the map previously contained a mapping for the key, the old value is replaced.
● V get(Object key): Returns the value to which the specified key is mapped, or null if the
map contains no mapping for the key.
● V remove(Object key): Removes the mapping for the specified key from the map if
present.
● boolean containsKey(Object key): Returns true if the map contains a mapping for the
specified key.
● boolean containsValue(Object value): Returns true if the map maps one or more keys to
the specified value.
● Hash Table: Uses a hash table to store the key-value pairs, which provides constant-time
complexity for basic operations such as add, remove, and contains, assuming a good hash
function.
● No Guaranteed Order: Does not guarantee any specific order of its elements.
Multi Atoms Plus
Multi Atoms Plus
LinkedHashMap Class
The LinkedHashMap class in Java is a hash table and linked list implementation of the Map interface. It
maintains a doubly-linked list running through all of its entries to maintain the insertion order. This
class combines the hash table's efficiency with a linked list's predictable iteration order.
● Insertion Order: Maintains the order of elements based on their insertion sequence.
● No Duplicate Keys: Does not allow duplicate keys. If a key is re-inserted, the old value is
replaced.
● Performance: Provides constant-time performance for basic operations like get and put, similar
to HashMap.
Multi Atoms Plus
Multi Atoms Plus
TreeMap Class
The TreeMap class in Java is an implementation of the NavigableMap interface that uses a Red-Black
tree to store its key-value pairs. It provides a way to store and access key-value pairs in a sorted order
based on the natural ordering of its keys or by a specified comparator.
● Sorted Order: Stores keys in a sorted order (ascending by default). The order is maintained using
a Red-Black tree.
● Key Comparison: Keys are compared using their natural ordering or a custom Comparator if one
is provided.
● Performance: Provides O(log n) time complexity for basic operations like add, remove, and
contains.
Multi Atoms Plus
Multi Atoms Plus
Hashtable Class
The Hashtable class in Java is a legacy class that implements the Map interface using a hash table. It
provides a way to store key-value pairs, similar to HashMap, but with some important differences in
behavior and performance characteristics.
Sorting Lists
1. Using Collections.sort(): The Collections.sort() method is used to sort a List in ascending order. It
uses the natural ordering of elements or a custom Comparator.
Multi Atoms Plus
Multi Atoms Plus
Sorting Arrays
Using Arrays.sort(): The Arrays.sort() method is used to sort arrays. It provides overloads for different
data types and can also sort arrays using a custom Comparator for objects.
Multi Atoms Plus
Comparable and Comparator interfaces
The Comparable and Comparator interfaces in Java are essential for sorting and ordering objects in
collections. They provide a standardized way to define and control how objects are compared and
ordered, which is crucial for many operations in programming.
Comparable Interface
● The Comparable interface in Java is used to define the natural ordering of objects of a class. It
allows objects of the class to be compared to one another, which is useful for sorting and
ordering. Located in java.lang package
● int compareTo(T o): Compares the current object with the specified object for order.
Returns: -> A negative integer if the current object is less than the specified object.
-> A positive integer if the current object is greater than the specified object.
Multi Atoms Plus
Output
Multi Atoms Plus
Comparator Interface
● The Comparator interface is used to define custom ordering of objects. It allows you to specify
multiple different sorting orders, unlike Comparable, which allows only one natural ordering.
● Located in java.util package
● int compare(T o1, T o2): Compares two objects for order.
Usage: Used when objects need to be sorted in ways other than their natural ordering, or when the class
does not implement Comparable.
Multi Atoms Plus
Output:
Multi Atoms Plus
Properties Class in Java
The Properties class in Java is part of the java.util package and extends the Hashtable class. It is used to
maintain a set of key-value pairs where both the keys and values are strings. The Properties class is
commonly used for configuration settings and managing application properties.
● Key-Value Storage: Stores properties in key-value pairs where both keys and values are strings.
● Persistence: Supports saving and loading properties from files or streams.
● Inheritance: Can inherit properties from another Properties object, allowing for default values.
● void load(InputStream inStream): Loads properties from an input stream (e.g., file).
● void load(Reader reader): Loads properties from a reader (e.g., text file).
2. Storing Properties
3. Accessing Properties
● String getProperty(String key): Retrieves the value associated with the specified key.
● String setProperty(String key, String value): Sets the value associated with the specified
key.
Multi Atoms Plus
Output:
Multi Atoms Plus
Thank You
Subscribe Multi Atoms & Multi Atoms Plus