0% found this document useful (0 votes)
523 views31 pages

OopsWithJava-Unit-4-by-MultiAtoms (1) - Removed

The document provides an overview of various classes and interfaces in the Java Collections Framework, including HashSet, LinkedHashSet, SortedSet, TreeSet, Map, HashMap, LinkedHashMap, TreeMap, and Hashtable. It highlights key features, commonly used methods, and sorting mechanisms, emphasizing the importance of Comparable and Comparator interfaces for ordering objects. Additionally, it discusses the Properties class for managing key-value pairs in configuration settings.

Uploaded by

nikku
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)
523 views31 pages

OopsWithJava-Unit-4-by-MultiAtoms (1) - Removed

The document provides an overview of various classes and interfaces in the Java Collections Framework, including HashSet, LinkedHashSet, SortedSet, TreeSet, Map, HashMap, LinkedHashMap, TreeMap, and Hashtable. It highlights key features, commonly used methods, and sorting mechanisms, emphasizing the importance of Comparable and Comparator interfaces for ordering objects. Additionally, it discusses the Properties class for managing key-value pairs in configuration settings.

Uploaded by

nikku
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/ 31

Multi Atoms Plus

Oops With Java


Unit-4
(Java Collections Framework)

Aktu 2nd Year Subject Code - (BCS 403)


Multi Atoms Plus
HashSet class
The HashSet class in Java is an implementation of the Set interface that uses a hash table for storage. It
is part of the Java Collections Framework and provides a collection that does not allow duplicate
elements and does not guarantee any specific order of elements.

Key Features of HashSet:

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.

Not Synchronized: HashSet is not synchronized, so it is not thread-safe. Use Collections.synchronizedSet


if thread safety is required.
Multi Atoms Plus
Multi Atoms Plus
LinkedHashSet Class
The LinkedHashSet class in Java is an implementation of the Set interface that combines the features of
HashSet and LinkedList. It maintains the insertion order of elements while providing the performance
characteristics of a hash table.

Key Features of LinkedHashSet:

● Insertion Order: Maintains the order of elements as they were inserted.


● No Duplicates: Does not allow duplicate elements.
● Efficient Operations: Provides O(1) time complexity for add, remove, and contains
operations, similar to HashSet.
● Linked List Structure: Uses a doubly linked list to maintain the order of elements
Multi Atoms Plus
Multi Atoms Plus
SortedSet interface
The SortedSet interface in Java is a subinterface of Set that provides a total ordering on its elements. It
extends the Set interface to handle sorted sets, ensuring that the elements are maintained in a specific
order.

Key Features of SortedSet:

● 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.

● E first(): Returns the first (lowest) element in the set.


● E last(): Returns the last (highest) element in the set.

● 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.

● SortedSet<E> subSet(E fromElement, E toElement): Returns a view of the portion of the


set whose elements range from fromElement, inclusive, to toElement, exclusive.
Multi Atoms Plus
TreeSet class
The TreeSet class in Java is a collection that implements the NavigableSet interface, which extends
SortedSet. It uses a Red-Black tree to store elements, ensuring that the elements are sorted and that
operations like addition, removal, and lookup are efficient.

Key Features of TreeSet:

● 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 Features of Map:

● 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.

● void clear(): Removes all mappings from the map.


Multi Atoms Plus
HashMap class
The HashMap class in Java is a widely-used implementation of the Map interface that stores key-value
pairs in a hash table. It allows for efficient retrieval, insertion, and deletion of elements, and does not
guarantee the order of its elements.

Key Features of HashMap:

● 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.

Key Features of LinkedHashMap:

● 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.

Key Features of TreeMap:

● 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.

Key Features of Hashtable:

● Synchronization: Hashtable is synchronized, making it thread-safe. Multiple threads can access a


Hashtable concurrently without the need for additional synchronization.
● No Null Keys or Values: Does not allow null keys or values. Attempting to insert a null key or
value will result in a NullPointerException.
● Legacy Class: Hashtable is considered a legacy class and is part of the original version of Java. It
is retained for backward compatibility but is generally replaced by HashMap in modern code.
● Performance: Due to its synchronization, Hashtable can be slower than HashMap in single-
threaded or unsynchronized environments.
Multi Atoms Plus
Multi Atoms Plus
Sorting
In Java, sorting can be applied to collections and arrays. The Collections class provides static methods
for sorting lists, while arrays can be sorted using the Arrays class. Sorting is typically done in ascending
order, but you can customize the sort order using comparators. Here’s an overview of sorting in Java:

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.

-> Zero if the current object is equal to 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 Features of Properties Class

● 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.

Properties properties = new Properties();


Multi Atoms Plus
Common Methods:
1. Loading Properties

● 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

● void store(OutputStream outStream, String comments): Saves properties to an output


stream.
● void store(Writer writer, String comments): Saves properties to a writer.

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

For Notes Join Telegram Channel

You might also like