0% found this document useful (0 votes)
7 views

Unit 4 java

Uploaded by

Sattwik Manna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Unit 4 java

Uploaded by

Sattwik Manna
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

‭Collection in Java‬ ‭Iterator Interface‬

‭➔‬ A ‭ collection in Java is a framework that provides an architecture to store‬ ‭➔‬ T‭ he Iterator interface provides a way to access elements of a collection‬
‭and manipulate a group of objects.‬ ‭sequentially without exposing the underlying structure.‬
‭➔‬ ‭Java Collections can achieve all the operations that you perform on data‬ ‭➔‬ ‭It is part of the‬‭java.util package‬‭and is a universal‬‭iterator for all‬
‭such as‬‭searching‬‭,‬‭sorting‬‭,‬‭insertion‬‭,‬‭manipulation‬‭,‬‭and‬‭deletion‬‭.‬ ‭collections.‬
‭➔‬ ‭Key Points:‬ ‭➔‬ ‭Key Points:‬
‭➔‬ ‭Collections are used to‬‭store‬‭,‬‭retrieve‬‭,‬‭manipulate‬‭,‬‭and‬ ‭➔‬ ‭Methods‬‭:‬
‭communicate aggregate data.‬ ‭◆‬ ‭boolean hasNext()‬‭: Returns true if there are more‬
‭➔‬ ‭Collections can hold both homogeneous and heterogeneous data.‬ ‭elements to iterate over.‬
‭➔‬ ‭They can dynamically grow and shrink in size.‬ ‭◆‬ ‭E next()‬‭: Returns the next element in the iteration.‬
‭◆‬ ‭void remove():‬‭Removes the last element returned by‬
‭Collection Framework in Java‬ ‭the iterator (optional operation).‬
‭➔‬ T‭ he Collection Framework provides a unified architecture for‬ ‭➔‬ ‭Usage‬‭:‬
‭representing and manipulating collections.‬ ‭◆‬ ‭The Iterator interface is used to traverse collections such‬
‭➔‬ ‭All the collections frameworks contain the following:‬ ‭as‬‭List‬‭,‬‭Set‬‭, and‬‭Map‬‭.‬
‭➔‬ ‭Interfaces‬‭: These are abstract data types that represent‬ ‭◆‬ ‭It supports both‬‭read‬‭and‬‭remove‬‭operations.‬
‭collections. The interfaces allow collections to be manipulated‬
L‭ ist‬‭<‭S‬ tring‬‭>‬‭list‬‭=‬‭new‬‭ArrayList‬‭<>();‬
‭independently of the details of their representation.‬ ‭list‬‭.‬‭add‬‭("‬‭A‬‭");‬
‭1.Collection‬ ‭3.Set‬ ‭5.Map‬ ‭list‬‭.‬‭add‬‭("‬‭B‭"‬ );‬
‭2.List‬ ‭4.Queue‬ ‭list‬‭.‬‭add‬‭("‬‭C‬‭");‬
‭➔‬ ‭Implementations‬‭: These are the concrete implementations‬‭of the‬
‭collection interfaces. Essentially, they are reusable data structures.‬ I‭terator‬‭<‭S‬ tring‬‭>‬‭iterator‬‭=‬‭list‬‭.‬‭iterator‬‭();‬
‭1.ArrayList 3. LinkedList 5.HashSet‬ ‭while‬‭(‭i‬terator‬‭.‬‭hasNext‬‭())‬‭{‬
‭2.TreeSet 4.HashMap 6.TreeMap‬ ‭String‬‭element‬‭=‬‭iterator‬‭.‭n ‬ ext‬‭();‬
‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭e‬ lement‬‭);‬
‭➔‬ ‭Algorithms‬‭: These are the methods that perform useful‬
‭}‬
‭computations, such as‬‭searching‬‭and‬‭sorting‬‭, on objects‬‭that‬
‭implement collection interfaces.‬
‭Collection Interface‬
‭Advantages of Collection Framework‬ ➔
‭ ‬ ‭The Collection interface is the root of the collection hierarchy.‬
‭➔‬ C ‭ onsistent API:‬‭The collection interfaces have a basic‬‭set of operations‬ ‭➔‬ ‭It represents a group of objects known as elements.‬
‭(such as‬‭adding‬‭and‬‭removing‬‭) that are extended by‬‭all‬ ‭➔‬ ‭The Collection interface is part of the‬‭java.util‬‭package‬‭.‬
‭implementations.‬ ‭➔‬ ‭Key Points:‬
‭➔‬ ‭Reduces Programming Effort:‬‭By providing useful data‬‭structures and‬ ‭➔‬ ‭Methods‬‭:‬
‭algorithms, the Collections Framework reduces the programming effort.‬ ‭◆‬ ‭boolean add(E e):‬‭Ensures that this collection contains‬
‭the specified element.‬
‭Java Collections Framework Hierarchy‬ ‭◆‬ ‭boolean remove(Object o)‬‭: Removes a single instance‬
‭➔‬ T‭ he Java Collections Framework is structured as a unified architecture‬ ‭of the specified element from this collection.‬
‭for representing and manipulating collections.‬ ‭◆‬ ‭int size()‬‭: Returns the number of elements in this‬
‭➔‬ ‭The hierarchy is broadly divided into three major‬‭groups:‬ ‭collection.‬
‭➔‬ ‭List, Set,‬‭and‬‭Queue‬‭, which extend the‬‭Collection‬‭interface‬‭,‬ ‭◆‬ ‭boolean isEmpty():‬‭Returns true if this collection‬
‭➔‬ ‭Map‬‭, which is a separate hierarchy.‬ ‭contains no elements.‬
‭➔‬ ‭Here are the important points for each component:‬ ‭◆‬ ‭boolean contains(Object o):‬‭Returns true if this‬
‭➔‬ ‭Collection Interface‬ ‭collection contains the specified element.‬
‭◆‬ ‭The root interface of the collections framework.‬ ‭◆‬ ‭Iterator<E> iterator():‬‭Returns an iterator over the‬
‭◆‬ ‭It provides common methods like‬‭add(),‬‭remove(),‬ ‭elements in this collection.‬
‭size()‬‭,‬‭clear(), contains()‬‭, and‬‭iterator()‬ ‭◆‬ ‭boolean addAll(Collection<? extends E> c):‬‭Adds all‬‭of‬
‭➔‬ ‭Map Interface‬ ‭the elements in the specified collection to this‬
‭◆‬ ‭Represents a mapping between a‬‭key‬‭and a‬‭value‬‭.‬ ‭collection.‬
‭◆‬ ‭Does not extend the Collection interface.‬ ‭◆‬ ‭void clear()‬‭: Removes all of the elements from this‬
‭◆‬ ‭Provides methods to‬‭put‬‭,‬‭get‬‭,‬‭remove‬‭elements based‬ ‭collection.‬
‭on a key‬‭.‬ ‭➔‬ ‭Subinterfaces‬‭:‬
‭1.List‬ ‭2.Set‬ ‭3.Queue‬
‭Collection‬‭Interface Map‬‭Interface‬ ‭➔‬ ‭Usage‬‭:‬
‭| |‬
‭+---‬‭List Interface‬ ‭+---‬‭HashMap‬ ‭◆‬ ‭The Collection interface provides the base functionality‬
‭| +--‬‭-ArrayList‬ ‭+---LinkedHashMap‬ ‭for all collections.‬
‭| +---LinkedList +---TreeMap‬
‭| +---Vector‬ ‭+---‬‭Hashtable‬
‭| | +---‬‭Stack‬
‭ ollection‬‭<‭S‬ tring‬‭>‬‭collection‬‭=‬‭new‬‭ArrayList‬‭<>();‬
C
‭|‬ ‭collection‬‭.‬‭add‬‭("‬‭A‬‭");‬
‭+---‬‭Set Interface‬ ‭collection‬‭.‬‭add‬‭("‬‭B‬‭");‬
‭| +---‬‭HashSet‬
‭collection‬‭.‬‭add‬‭("‬‭C‬‭");‬
‭| +---‬‭LinkedHashSet‬
‭| +---‬‭TreeSet‬
‭|‬ ‭for‬‭(‭S‬ tring‬‭element‬‭:‬‭collection‬‭)‬‭{‬
‭+---‬‭Queue Interface‬ ‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭(‭e‬ lement‬‭);‬
‭+---‬‭PriorityQueue‬
‭+---‬‭Deque‬‭Interface‬
‭}‬
‭+---‬‭ArrayDeque‬
‭+---‬‭LinkedList Deque‬
‭List Interface‬ ‭➔‬ B
‭ asic Operations‬
‭➔‬ T‭ he List interface extends the Collection interface and represents an‬ ‭1.boolean add( e)‬ ‭ .void add(int index, element)‬
6
‭ordered collection (also known as a sequence).‬ ‭2. get(int index)‬ ‭7. set(int index, element)‬
‭➔‬ ‭The user can access elements by their integer index (position in the list)‬ ‭3. remove(int index)‬ ‭8.boolean remove(e)‬
‭and search for elements in the list.‬ ‭4. int size()‬ ‭9. void clear()‬
‭➔‬ ‭Key Points:‬ ‭5. boolean contains(e)‬
‭➔‬ ‭Methods‬‭:‬
‭◆‬ ‭void add(int index, E element):‬‭Inserts the specified‬
‭import‬‭java‬‭.‭u
‬ til‬‭.‬‭*‭;‬ ‬
‭element at the specified position in this list.‬
‭◆‬ ‭E get(int index):‬‭Returns the element at the specified‬ ‭public‬‭class‬‭ArrayListExample‬‭{‬
‭position in this list.‬ ‭public‬‭static‬‭void‬‭main‬‭(‬‭String‬‭[]‬‭args‬‭)‬‭{‬
‭◆‬ ‭E set(int index, E element)‬‭: Replaces the element‬‭at the‬ ‭// Creating an ArrayList‬
‭specified position in this list with the specified element.‬ ‭List‬‭<‬‭String‬‭>‬‭arrayList‬‭=‬‭new‬‭ArrayList‬‭<>();‬
‭◆‬ ‭E remove(int index):‬‭Removes the element at the‬
/‭ / Adding elements‬
‭specified position in this list.‬
‭arrayList‬‭.‬‭add‬‭("‬‭A‬‭");‬
‭◆‬ ‭int indexOf(Object o):‬‭Returns the index of the first‬ ‭arrayList‬‭.‬‭add‬‭("‬‭B‬‭");‬
‭occurrence of the specified element in this list.‬ ‭arrayList‬‭.‬‭add‬‭("‬‭C‬‭");‬
‭◆‬ ‭ListIterator<E> listIterator()‬‭: Returns a list iterator‬‭over‬ ‭arrayList‬‭.‬‭add‬‭("‬‭D‬‭");‬
‭the elements in this list.‬
‭➔‬ ‭Implementations‬‭:‬ /‭ / Accessing elements‬
‭1.ArrayList 2. LinkedList 3. Vector 4.Stack‬ ‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Element at index 2:‬‭"‬‭+‬‭arrayList‬‭.‬‭get‬‭(‭2 ‬ ‭)‬ );‬
‭// Iterating elements‬
‭➔‬ ‭Usage‬‭:‬ ‭for‬‭(‭S‬ tring‬‭element‬‭:‬‭arrayList‬‭)‬‭{‬
‭◆‬ T‭ he List interface allows for ordered collections that can‬ ‭System‬‭.‭o‬ ut‬‭.‭p‬ rintln‬‭(‭e‬ lement‬‭);‬
‭contain duplicate elements.‬ ‭}‬
‭// Modifying elements‬
‭List‬‭<‭S‬ tring‬‭>‬‭list‬‭=‬‭new‬‭ArrayList‬‭<>();‬
‭arrayList‬‭.‬‭set‬‭(‭1 ‬ ‭,‬‬‭"‬‭E‬‭");‬
‭// List<Integer> numbers = Arrays.asList(5, 2, 8, 1, 3); method 1‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭After modification:‬‭"‬‭+‬‭arrayList‬‭);‬
‭// method 2‬
‭// Removing elements‬
‭list‬‭.‬‭add‬‭("‬‭A‬‭");‬
‭arrayList‬‭.‬‭remove‬‭("‬‭C‬‭");‬
‭list‬‭.‬‭add‬‭("‬‭B‬‭");‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭After removal:‬‭"‬‭+‬‭arrayList‬‭);‬
‭list‬‭.‬‭add‬‭("‬‭C‬‭");‬
‭// Checking size‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Size of ArrayList:‬‭"‬‭+‬‭arrayList‬‭.‬‭size‬‭());‬
‭for‬‭(‭i‬nt‬‭i‬‭=‬‭0‭;‬ ‬‭i‬‭<‬‭list‬‭.‬‭size‬‭();‬‭i‬‭++)‬‭{‬
‭// Checking if ArrayList contains an element‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭(‭l‬ist‬‭.‬‭get‬‭(‭i‬‭)‬ );‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Does ArrayList contain‬‭'A'?‬‭"‬‭+‬
‭}‬
‭arrayList‬‭.‭c‬ ontains‬‭("‬‭A‬‭"));‬

‭Iterator vs ListIterator‬ /‭ / Clearing the ArrayList‬


‭arrayList‬‭.‬‭clear‬‭();‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭After clearing:‬‭"‬‭+‬‭arrayList‬‭);‬
‭}‬
‭}‬

‭LinkedList‬

‭ ‬ L‭ inkedList is a doubly linked list implementation of the List interface.‬
‭➔‬ ‭It is part of the Java Collections Framework and is found in the‬‭java.util‬
‭package.‬
‭➔‬ ‭Unlike ArrayList, which uses a dynamic array, LinkedList uses a doubly‬
‭linked list to store elements.‬
‭➔‬ ‭Key Points:‬
‭➔‬ ‭Doubly Linked‬‭: Each element in a LinkedList is stored‬‭in a node that‬
‭contains a reference to the next and previous elements.‬
‭➔‬ ‭Dynamic Size:‬‭Can grow and shrink dynamically.‬
‭ArrayList‬ ‭➔‬ ‭Allows duplicate elements.‬
‭➔‬ ‭Efficient for adding or removing elements anywhere in the list.‬

‭ ‬A ‭ rrayList is a resizable array implementation of the List interface.‬
‭➔‬ ‭Slower than ArrayList for random access (get()), as it requires traversing‬
‭➔‬ ‭It is part of the Java Collections Framework and is found in the‬‭java.util‬
‭from the head or tail.‬
‭package.‬
‭➔‬ ‭Basic Operations:‬‭(Same as ArrayList )‬
‭➔‬ ‭ArrayList allows for dynamic arrays that can grow as needed,‬
‭➔‬ ‭which means it can change its size during runtime.‬ ‭import‬‭java‬‭.‬‭util‬‭.‬‭*‭;‬ ‬
‭➔‬ ‭Key Points‬‭:‬
‭➔‬ ‭Unlike arrays in Java, ArrayList can grow and shrink in size‬ ‭public‬‭class‬‭LinkedListExample‬‭{‬
‭dynamically.‬ ‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭)‬‭{‬
‭// Create a LinkedList‬
‭➔‬ ‭Allows duplicate elements.‬
‭LinkedList‬‭<‭S‬ tring‬‭>‬‭linkedList‬‭=‬‭new‬‭LinkedList‬‭<>();‬
‭➔‬ ‭Provides fast random access to elements.‬
‭➔‬ ‭Initial capacity is 10, but it grows automatically as elements are‬
‭added.‬
‭➔‬
‭// Adding elements‬ ‭import‬‭java‬‭.‭u
‬ til‬‭.‬‭Vector‬‭;‬
l‭inkedList‬‭.‬‭add‬‭("‬‭Apple‬‭");‬
‭linkedList‬‭.‬‭add‬‭("‬‭Banana‬‭");‬ ‭public‬‭class‬‭VectorExample‬‭{‬
‭linkedList‬‭.‬‭add‬‭("‬‭Cherry‬‭");‬ ‭public‬‭static‬‭void‬‭main‬‭(‬‭String‬‭[]‬‭args‬‭)‬‭{‬
‭linkedList‬‭.‬‭set‬‭(‭1
‬ ‭,‬‬‭"‭O
‬ range‬‭");‬ ‭// Modify‬‭element‬ ‭Vector‬‭<‬‭String‬‭>‬‭vector‬‭=‬‭new‬‭Vector‬‭<>();‬
‭linkedList‬‭.‬‭add‬‭(‭2 ‬ ‭,‬‬‭"‭G
‬ rape‬‭");‬ ‭// Add an element‬
‭linkedList‬‭.‬‭remove‬‭("‬‭Apple‬‭");‬ ‭// Remove element‬ v‭ ector‬‭.‭a‬ dd‬‭("‬‭A‬‭");‬ ‭// Adding elements‬
‭vector‬‭.‭a‬ dd‬‭("‬‭B‬‭");‬
/‭ / Check if "Banana" is present‬ ‭vector‬‭.‭a‬ dd‬‭("‬‭C‭"‬ );‬
‭boolean‬‭containsBanana‬‭=‬‭linkedList‬‭.‬‭contains‬‭("‬‭Banana‬‭");‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Does LinkedList contain‬‭'Banana'?‬‭"‬‭+‬ /‭ / Accessing elements‬
‭containsBanana‬‭);‬ ‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Element at index 1:‬‭"‬‭+‬‭vector‬‭.‬‭get‬‭(‭1 ‬ ‬‭));‬
‭// Removing element‬
S‭ ystem‬‭.‬‭out‬‭.‬‭println‬‭("Print‬‭forward order element‬‭");‬ ‭vector‬‭.‭r‬ emove‬‭(‬‭2‭)‬ ;‬
‭ListIterator‬‭<‭S‬ tring‬‭>‬‭iterator‬‭=‬‭linkedList‬‭.‭l‬istIterator‬‭();‬ ‭// Size of vector‬
‭while‬‭(‬‭iterator‬‭.‬‭hasNext‬‭())‬‭{‬ ‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Size of vector:‬‭"‬‭+‬‭vector‬‭.‬‭size‬‭());‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭(‬‭iterator‬‭.‭n
‬ ext‬‭());‬ ‭}‬
‭}‬ ‭}‬

S‭ ystem‬‭.‬‭out‬‭.‬‭println‬‭("Print‬‭reverse order element‬‭");‬


‭while‬‭(‬‭iterator‬‭.‬‭hasPrevious‬‭())‬‭{‬ ‭Stack‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭(‬‭iterator‬‭.‭p
‬ revious‬‭());‬ ‭➔‬ S‭ tack is a subclass of Vector that implements a last-in, first-out (LIFO)‬
‭}‬
‭stack of objects.‬
‭} }‬
‭➔‬ ‭Methods‬‭:‬
‭ rrayList(Collection<? extends E> c)‬
A ‭➔‬ ‭push(E item):‬‭Pushes an item onto the top of the stack.‬
‭➔‬ ‭Creates a list containing the elements of the specified collection, in the‬ ‭➔‬ ‭pop():‬‭Removes the object at the top of the stack‬‭and returns it.‬
‭order they are returned by the collection's iterator.‬ ‭➔‬ ‭peek()‬‭: Looks at the object at the top of the stack‬‭without‬
‭➔‬ ‭It is same For Linked List‬ ‭removing it.‬
‭➔‬ ‭isEmpty()‬‭: Checks if the stack is empty.‬
L‭ ist‬‭<‭S‬ tring‬‭>‬‭existingList‬‭=‬‭Arrays‬‭.‬‭asList‬‭("‬‭A‬‭",‬‭"‬‭B‭"‬ ,‬‭"‭C
‬ ‬‭");‬
‭ArrayList‬‭<‭S‬ tring‬‭>‬‭list‬‭=‬‭new‬‭ArrayList‬‭<>(‬‭existingList‬‭);‬ ‭import‬‭java‬‭.‬‭util‬‭.‬‭Stack‬‭;‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭(‭l‬ist‬‭);‬‭// Output: [A, B, C]‬
‭public‬‭class‬‭StackExample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‬‭String‬‭[]‬‭args‬‭)‬‭{‬
‭LinkedList(Collection<? extends E> c)‬ ‭Stack‬‭<‭S‬ tring‬‭>‬‭stack‬‭=‬‭new‬‭Stack‬‭<>();‬
‭List‬‭<‭S‬ tring‬‭>‬‭arrayList‬‭=‬‭new‬‭ArrayList‬‭<>();‬
‭arrayList‬‭.‬‭add‬‭("‬‭Apple‬‭");‬ /‭ / Pushing elements‬
‭arrayList‬‭.‬‭add‬‭("‬‭Banana‬‭");‬ ‭stack‬‭.‬‭push‬‭("‬‭A‬‭");‬
‭arrayList‬‭.‬‭add‬‭("‬‭Cherry‬‭");‬ ‭stack‬‭.‬‭push‬‭("‬‭B‬‭");‬
‭stack‬‭.‬‭push‬‭("‬‭C‭"‬ );‬
/‭ / Create a LinkedList using the ArrayList‬‭elements‬
‭LinkedList‬‭<‭S‬ tring‬‭>‬‭linkedList‬‭=‬‭new‬‭LinkedL‬‭ist‬‭<>(‬‭arrayList‬‭);‬ /‭ / Peeking the top element‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Top element:‬‭"‬‭+‬‭stack‬‭.‬‭peek‬‭());‬
‭// Popping elements‬
‭LinkedList vs ArrayList‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Popped element:‬‭"‬‭+‬‭stack‬‭.‭p ‬ op‬‭());‬
‭➔‬ L‭ inkedList provides methods like‬‭addFirst, addLast,‬‭getFirst, getLast,‬
‭// Checking if stack is empty‬
‭removeFirst, removeLast, offerFirst, offerLast, peekFirst, peekLast,‬ ‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Is stack empty?‬‭"‬‭+‬‭stack‬‭.‬‭isEmpty‬‭());‬
‭pollFirst, pollLast, descendingIterator‬‭for efficient‬‭insertion‬‭,‬‭removal‬‭,‬ ‭}‬
‭and‬‭traversal‬‭operations from both ends.‬ ‭}‬
‭➔‬ ‭ArrayList‬‭is optimized for random access and efficient‬‭element‬
‭insertion‬‭/‬‭removal‬‭in the middle.‬
‭➔‬ ‭Choosing between them depends on the use case:‬ ‭Queue Interface‬
‭➔‬ ‭Use LinkedList for frequent‬‭insertions/removals‬‭at‬‭both ends or‬ ➔
‭ ‬ ‭ ueue is a collection designed for holding elements prior to processing.‬
Q
‭sequential traversal.‬ ‭➔‬ ‭It typically orders elements in a‬‭FIFO (first-in,‬‭first-out)‬‭manner.‬
‭➔‬ ‭Use ArrayList for scenarios requiring random access or faster‬ ‭➔‬ ‭Implementations include‬‭LinkedList‬‭and‬‭PriorityQueue‬‭.‬
‭access by‬‭index‬‭.‬ ‭➔‬ ‭Methods‬‭:‬
‭➔‬ ‭add(e):‬‭Inserts the specified element into the queue‬‭(throws an‬
‭Vector‬ ‭exception if it fails).‬
‭➔‬ V ‭ ector is a part of the Java Collections Framework and implements a‬ ‭➔‬ ‭offer(e)‬‭: Inserts the specified element into the queue‬‭(returns‬
‭growable array of objects.‬ ‭false if it fails).‬
‭➔‬ ‭It is synchronized, making it thread-safe, but can have performance‬ ‭➔‬ ‭remove()‬‭: Retrieves and removes the‬‭head‬‭of the queue‬‭(throws‬
‭overhead due to synchronization.‬ ‭NoSuchElementException an exception if the queue is empty).‬
‭➔‬ ‭Methods‬‭:‬ ‭➔‬ ‭poll()‬‭: Retrieves and removes the‬‭head‬‭of the queue‬‭(returns null‬
‭➔‬ ‭add(e):‬‭Adds an element to the end of the vector.‬ ‭if the queue is empty).‬
‭➔‬ ‭get(int index)‬‭: Returns the element at the specified‬‭position.‬ ‭➔‬ ‭peek():‬‭Retrieves, but does not remove, the‬‭head‬‭of‬‭the queue‬
‭➔‬ ‭remove(int index)‬‭: Removes the element at the specified‬‭position.‬ ‭(returns null if the queue is empty).‬
‭➔‬ ‭size()‬‭: Returns the number of elements in the vector.‬
i‭mport‬‭java‬‭.‬‭util‬‭.‬‭LinkedList‬‭;‬
‭import‬‭java‬‭.‬‭util‬‭.‬‭Queue‬‭;‬ /‭ / LinkedHashSet example‬
‭Set‬‭<‭S‬ tring‬‭>‬‭linkedHashSet‬‭=‬‭new‬‭LinkedHashSet‬‭<>();‬
‭public‬‭class‬‭QueueExample‬‭{‬ ‭linkedHashSet‬‭.‭a‬ dd‬‭("‬‭B‬‭");‬
‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭)‬‭{‬ ‭linkedHashSet‬‭.‭a‬ dd‬‭("‬‭A‬‭");‬
‭Queue‬‭<‭S‬ tring‬‭>‬‭queue‬‭=‬‭new‬‭LinkedList‬‭<>();‬ ‭linkedHashSet‬‭.‭a‬ dd‬‭("‬‭C‬‭");‬
‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭("‬‭LinkedHashSet:‬‭"‬‭+‬‭linkedHashSet‬‭);‬ ‭// B A C‬
/‭ / Adding elements‬
‭queue‬‭.‬‭add‬‭("‬‭A‬‭");‬ /‭ / TreeSet example‬
‭queue‬‭.‬‭add‬‭("‬‭B‬‭");‬ ‭Set‬‭<‭S‬ tring‬‭>‬‭treeSet‬‭=‬‭new‬‭TreeSet‬‭<>();‬
‭queue‬‭.‬‭add‬‭("‬‭C‬‭");‬ ‭treeSet‬‭.‭a‬ dd‬‭("‬‭B‬‭");‬
‭treeSet‬‭.‭a‬ dd‬‭("‬‭A‬‭");‬
/‭ / Peeking the head element‬ ‭treeSet‬‭.‭a‬ dd‬‭("‬‭C‬‭");‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Head element:‬‭"‬‭+‬‭queue‬‭.‭p
‬ eek‬‭());‬ ‭System‬‭.‭o
‬ ut‬‭.‭p
‬ rintln‬‭("‬‭TreeSet:‬‭"‬‭+‬‭treeSet‬‭);‬ ‭// A B C‬
‭}‬
/‭ / Polling elements‬ ‭}‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Polled element:‬‭"‬‭+‬‭queue‬‭.‭p
‬ oll‬‭());‬
‭“”” HashSet‬‭: Fastest for basic operations, no‬‭guaranteed order.‬
/‭ / Checking the size‬ L‭ inkedHashSet‬‭: Maintains insertion order,‬‭slower than HashSet.‬
‭TreeSet‬‭: Maintains elements in sorted order,‬‭slower than HashSet and LinkedHashSet. “””‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Size of queue:‬‭"‬‭+‬‭queue‬‭.‬‭size‬‭());‬
‭}‬ ‭}‬
‭Map Interface‬
‭Set Interface‬ ➔
‭ ‬R ‭ epresents a collection of key-value pairs where each key is unique.‬
‭➔‬ ‭Maps keys to values and does not allow duplicate keys.‬

‭ ‬ ‭Represents a collection that cannot contain duplicate elements.‬
‭➔‬ ‭methods for adding, accessing, removing, and checking for key-value‬
‭➔‬ ‭It models the mathematical set abstraction.‬
‭pairs.‬
‭➔‬ ‭Does not guarantee the order of elements.‬
‭➔‬ ‭Allows at most one null element.‬
‭➔‬ ‭Implementations include‬‭HashSet‬‭,‬‭LinkedHashSet‬‭, and‬‭TreeSet‬‭.‬ ‭HashMap Class‬

‭ ‬ I‭mplements the‬‭Map interface‬‭using a‬‭hash table‬‭.‬
‭HashSet‬ ‭➔‬ ‭Does not guarantee the order of key-value pairs.‬
‭➔‬ I‭mplements the Set interface using a hash table. It does not guarantee‬ ‭➔‬ ‭Provides‬‭constant‬‭-‬‭time‬‭performance for basic operations‬‭(‭p
‬ ut, get,‬
‭the order of elements.‬ ‭remove‬‭) on average.‬
‭➔‬ ‭Offers constant-time performance for basic operations (‬‭add‬‭,‬‭remove‬‭,‬ ‭➔‬ ‭Allows null values and one null key.‬
‭contains‬‭).‬
‭import‬‭java‬‭.‬‭util‬‭.‬‭HashMap‬‭;‬
‭➔‬ ‭Does not maintain the insertion order.‬ i‭mport‬‭java‬‭.‬‭util‬‭.‬‭Map‬‭;‬
‭➔‬ ‭Allows null elements.‬
‭public‬‭class‬‭HashMapExample‬‭{‬
‭LinkedHashSet‬ ‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭)‬‭{‬
‭➔‬ E‭ xtends HashSet and maintains a doubly-linked list of entries to‬ ‭Map‬‭<‭S‬ tring‬‭,‬‭Integer‬‭>‬‭hashMap‬‭=‬‭new‬‭HashMap‬‭<>();‬
‭preserve the insertion order.‬
‭ ashMap‬‭.‬‭put‬‭("‬‭One‬‭",‬‭1‭)‬ ;‬
h
‭➔‬ ‭Iterates over elements in insertion order.‬
‭hashMap‬‭.‬‭put‬‭("‬‭Two‬‭",‬‭2‭)‬ ;‬
‭➔‬ ‭Slower than‬‭HashSet‬‭for basic operations due to maintaining‬‭order.‬ ‭hashMap‬‭.‬‭put‬‭("‬‭Three‬‭",‬‭3‭)‬ ;‬
‭➔‬ ‭Allows null elements.‬
‭System‬‭.‭o
‬ ut‬‭.‬‭println‬‭("‬‭HashMap:‬‭"‬‭+‬‭hashMap‬‭);‬
‭SortedSet Interface‬ ‭}‬
‭➔‬ E‭ xtends Set and maintains elements in sorted order defined by their‬ ‭}‬
‭natural ordering or a comparator.‬
‭➔‬ ‭Provides methods for accessing elements by‬‭their position‬‭in the sorted‬ ‭LinkedHashMap Class‬
‭set.‬ ➔
‭ ‬ E‭ xtends‬‭HashMap‬‭and maintains insertion order of keys.‬
‭➔‬ ‭Implementations include‬‭TreeSet‬‭.‬ ‭➔‬ ‭Iterates over elements in the order they were inserted.‬
‭➔‬ ‭Slower performance for basic operations compared to HashMap due to‬
‭TreeSet‬
‭maintaining order.‬

‭ ‬ I‭mplements SortedSet using a tree structure‬‭(red-black‬‭tree).‬
‭➔‬ ‭Allows null values and one null key.‬
‭➔‬ ‭Maintains elements in sorted order (ascending by default or based on a‬
‭custom comparator).‬ ‭import‬‭java‬‭.‬‭util‬‭.‬‭LinkedHashMap‬‭;‬
‭➔‬ ‭Slower performance for basic operations compared to‬‭HashSet‬‭and‬ ‭import‬‭java‬‭.‬‭util‬‭.‬‭Map‬‭;‬
‭LinkedHashSet‬‭.‬
‭➔‬ ‭Does not allow null elements.‬ ‭public‬‭class‬‭LinkedHashMapExample‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭)‬‭{‬
‭Example Of All Sets interface‬ ‭Map‬‭<‭S‬ tring‬‭,‬‭Integer‬‭>‬‭linkedHashMap‬‭=‬‭new‬‭LinkedHashMap‬‭<>();‬
‭import java.util.*;‬ l‭inkedHashMap‬‭.‬‭put‬‭("‬‭One‬‭",‬‭1‭)‬ ;‬
‭public‬‭class‬‭SetExamples‬‭{‬ ‭linkedHashMap‬‭.‬‭put‬‭("‬‭Two‬‭",‬‭2‭)‬ ;‬
‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭)‬‭{‬ ‭linkedHashMap‬‭.‬‭put‬‭("‬‭Three‬‭",‬‭3‬‭);‬
‭// HashSet example‬
‭Set‬‭<‭S‬ tring‬‭>‬‭hashSet‬‭=‬‭new‬‭HashSet‬‭<>();‬ ‭System‬‭.‭o
‬ ut‬‭.‬‭println‬‭("‬‭LinkedHashMap:‬‭"‬‭+‬‭linkedHashMap‬‭);‬
‭hashSet‬‭.‬‭add‬‭("‬‭B‬‭");‬ ‭}‬
‭hashSet‬‭.‬‭add‬‭("‬‭A‬‭");‬ ‭}‬
‭hashSet‬‭.‬‭add‬‭("‬‭C‬‭");‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭HashSet:‬‭"‬‭+‬‭hashSet‬‭);‬‭// C B A‬
‭TreeMap Class‬ /‭ / Add key-value pairs to the Map‬
‭map‬‭.‭p ‬ ut‬‭("‬‭One‬‭",‬‭1‭)‬ ;‬

‭ ‬ I‭mplements the SortedMap interface using a Red-Black tree.‬
‭map‬‭.‭p‬ ut‬‭("‬‭Two‬‭",‬‭2‭)‬ ;‬
‭➔‬ ‭Maintains keys in ascending order (natural order or custom‬ ‭map‬‭.‭p
‬ ut‬‭("‬‭Three‬‭",‬‭3‭)‬ ;‬
‭Comparator).‬
‭➔‬ ‭Slower performance for basic operations compared to HashMap and‬
‭LinkedHashMap due to sorting.‬ ‭// Iterating over the Map using for-each loop‬
‭➔‬ ‭Does not allow null keys but allows null values.‬ ‭System‬‭.‭o‬ ut‬‭.‭p‬ rintln‬‭("\n‬‭Key-Value pairs in the‬‭Map:‬‭");‬
‭for‬‭(‭M
‬ ap‬‭.‬‭Entry‬‭<‬‭String‬‭,‬‭Integer‬‭>‬‭entry‬‭:‬‭map‬‭.‬‭entrySet‬‭())‬‭{‬
‭import‬‭java‬‭.‬‭util‬‭.‬‭Map‬‭;‬ ‭System‬‭.‭o ‬ ut‬‭.‬‭println‬‭("‬‭Key:‬‭"‬‭+‬‭entry‬‭.‭g‬ etKey‬‭()‬‭+‬‭"‬‭, Value:‬‭"‬‭+‬
‭import‬‭java‬‭.‬‭util‬‭.‬‭TreeMap‬‭;‬ ‭entry‬‭.‬‭getValue‬‭());‬
‭}‬
‭public‬‭class‬‭TreeMapExample‬‭{‬ ‭}‬
‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭)‬‭{‬ ‭}‬
‭Map‬‭<‭S‬ tring‬‭,‬‭Integer‬‭>‬‭treeMap‬‭=‬‭new‬‭TreeMap‬‭<>();‬
‭Sorting‬
t‭ reeMap‬‭.‬‭put‬‭("‬‭Three‬‭",‬‭3‭)‬ ;‬ ‭➔‬ S‭ orting in Java refers to arranging elements in a collection in a specific‬
‭treeMap‬‭.‬‭put‬‭("‬‭One‬‭",‬‭1‬‭);‬ ‭order, typically ascending or descending based on certain criteria.‬
‭treeMap‬‭.‬‭put‬‭("‬‭Two‬‭",‬‭2‭)‬ ;‬ ‭➔‬ ‭Java provides several ways to achieve sorting depending on the data‬
‭structure and requirements:‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭TreeMap:‬‭"‬‭+‬‭treeMap‬‭);‬
‭➔‬ ‭Sorting Arrays ⇒‬‭Arrays.sort()‬
‭}‬
‭}‬ i‭nt‬‭[]‬‭nums‬‭=‬‭{‬‭5‭,‬‬‭2‭,‬‬‭8‭,‬‬‭1‭,‬‬‭3‭}‬ ;‬
‭Arrays‬‭.‭s‬ ort‬‭(‭n
‬ ums‬‭);‬‭// Sorts nums array in ascending‬‭order‬
‭“””‬‭HashMap‬‭: Fastest for basic operations, no order‬‭guarantee.‬
‭LinkedHashMap‬‭: Maintains insertion order, inherits‬‭from HashMap.‬
‭TreeMap‬‭: Maintains keys in sorted order, slower for‬‭basic operations due to sorting.”” “‬
‭➔‬ ‭Sorting Collections‬
‭1.‬‭Collections.sort():‬‭Sorts collections such as lists‬‭using natural‬
‭ordering (if elements implement Comparable) or a specified‬
‭Hashtable Class‬
‭Comparator.‬
‭➔‬ T‭ he Hashtable class in Java provides a basic implementation of a hash‬
‭table, which maps keys to values.‬ L‭ ist‬‭<‬‭String‬‭>‬‭names‬‭=‬‭new‬‭ArrayList‬‭<>();‬
‭➔‬ ‭It inherits from the Dictionary class and implements the Map interface,‬ ‭names‬‭.‭a‬ dd‬‭("‬‭Alice‬‭");‬
‭making it similar to HashMap but with some differences :‬ ‭names‬‭.‭a‬ dd‬‭("‬‭Bob‬‭");‬
‭Collections‬‭.‭s‬ ort‬‭(‭n
‬ ames‬‭);‬‭// Sorts alphabetically (natural‬‭order)‬
‭➔‬ ‭Hashtable is synchronized,‬
‭2.Sorting with Comparator:‬ ‭ascending order use :‬
‭➔‬ ‭Neither keys nor values can be null‬
‭Comparator.naturalOrder() and descending order use :‬
‭import‬‭java‬‭.‬‭util‬‭.‬‭Hashtable‬‭;‬ ‭Comparator.reverseOrder()‬

‭public‬‭class‬‭HashtableExample‬‭{‬ L‭ ist‬‭<‬‭String‬‭>‬‭names‬‭=‬‭new‬‭ArrayList‬‭<>();‬
‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭)‬‭{‬ ‭names‬‭.‭a‬ dd‬‭("‬‭Alice‬‭");‬
‭// Using Hashtable‬ ‭names‬‭.‭a‬ dd‬‭("‬‭Bob‬‭");‬
‭Hashtable‬‭<‭S‬ tring‬‭,‬‭Integer‬‭>‬‭hashtable‬‭=‬‭new‬‭Hashtable‬‭<>();‬ ‭Collections‬‭.‭s‬ ort‬‭(‭n
‬ ames‬‭,‬‭Comparator‬‭.‬‭reverseOrder‬‭());‬‭// Sorts in‬
‭hashtable‬‭.‬‭put‬‭("‬‭One‬‭",‬‭1‭)‬ ;‬ ‭reverse order‬
‭hashtable‬‭.‬‭put‬‭("‬‭Two‬‭",‬‭2‭)‬ ;‬
‭// hashtable.put(null, 3); // Throws NullPointerException‬ ‭Comparable Interface‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Hashtable:‬‭"‬‭+‬‭hashtable‬‭);‬

‭ ‬C ‭ omparable is an interface in the java.lang package.‬
‭}‬ ‭➔‬ ‭It declares one method compareTo() which compares the current object‬
‭}‬ ‭(this) with another object of the same type.‬
‭➔‬ ‭Classes that implement Comparable can be sorted automatically using‬
‭Iterators used in Map and Set‬ ‭methods like‬‭Arrays.sort()‬‭or‬‭Collections.sort().‬
‭import‬‭java‬‭.‬‭util‬‭.‬‭*‭;‬ ‬

‭public‬‭class‬‭SetMapIterationExample‬‭{‬ ‭public‬‭interface‬‭Comparable‬‭<‭T‬ ‭>‬ ‬‭{‬


‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭)‬‭{‬ ‭public‬‭int‬‭compareTo‬‭(‭T‬ ‬‭o‭)‬ ;‬
‭// Create a HashSet‬ ‭}‬
‭Set‬‭<‭S‬ tring‬‭>‬‭set‬‭=‬‭new‬‭HashSet‬‭<>();‬
‭// Add elements to the Set‬ ‭Example of Comparable InterFace‬
‭set‬‭.‬‭add‬‭("‬‭Apple‬‭");‬
‭set‬‭.‬‭add‬‭("‬‭Banana‬‭");‬ ‭public‬‭class‬‭Student‬‭implements‬‭Comparable‬‭<‭S‬ tudent‬‭>‬‭{‬
‭set‬‭.‬‭add‬‭("‬‭Orange‬‭");‬ ‭private‬‭String‬‭name‬‭;‬
‭private‬‭int‬‭age‬‭;‬
/‭ / Iterating over the Set using for-each‬‭loop‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Elements in the Set:‬‭");‬ /‭ / Constructor‬
‭for‬‭(‭S‬ tring‬‭element‬‭:‬‭set‬‭)‬‭{‬ ‭public‬‭Student‬‭(‭S‬ tring‬‭name‬‭,‬‭int‬‭age‬‭)‬‭{‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭(‭e‬ lement‬‭);‬ ‭this‬‭.‬‭name‬‭=‬‭name‬‭;‬
‭}‬ ‭this‬‭.‬‭age‬‭=‬‭age‬‭;‬
‭}‬
/‭ / Create a HashMap‬
‭Map‬‭<‭S‬ tring‬‭,‬‭Integer‬‭>‬‭map‬‭=‬‭new‬‭HashMap‬‭<>();‬
‭public‬‭int‬‭compareTo‬‭(‭S‬ tudent‬‭other‬‭)‬‭{‬ ‭Properties Class‬
‭// Compare students based on age‬
‭return‬‭Integer‬‭.‬‭compare‬‭(‭t‬ his‬‭.‭a‬ ge‬‭,‬‭other‬‭.‬‭age‬‭);‬ ➔
‭ ‬P ‭ roperties stores key-value pairs where both keys and values are strings.‬
‭}‬ ‭➔‬ ‭It supports loading from and saving to files using‬‭load‬‭() and‬‭store‬‭()‬
‭// Example usage in main method‬
‭methods.‬
‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭)‬‭{‬
‭List‬‭<‭S‬ tudent‬‭>‬‭students‬‭=‬‭new‬‭ArrayList‬‭<>();‬ ‭➔‬ ‭Default values can be set and queried if a property is not found in the‬
‭students‬‭.‬‭add‬‭(‭n ‬ ew‬‭Student‬‭("‬‭Alice‬‭",‬‭20‬‭));‬ ‭current instance.‬
‭students‬‭.‬‭add‬‭(‭n‬ ew‬‭Student‬‭("‬‭Bob‬‭",‬‭18‬‭));‬ ‭➔‬ ‭Java system properties (‬‭System.getProperties())‬‭are‬‭accessible through‬
‭students‬‭.‬‭add‬‭(‭n
‬ ew‬‭Student‬‭("‬‭Charlie‬‭",‬‭22‬‭));‬ ‭a Properties object.‬
‭➔‬ ‭Example usage involves setting, saving to file, loading, and accessing‬
‭// Sorting using Collections.sort() (uses Comparable)‬
‭properties.‬
‭Collections‬‭.‬‭sort‬‭(‭s‬ tudents‬‭);‬
‭➔‬ ‭It provides persistence for application settings like‬‭database‬
/‭ / Printing sorted students‬ ‭connections‬‭and‬‭UI configurations‬‭.‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Sorted Students by Age:‬‭");‬ ‭➔‬ ‭methods‬‭:‬
‭for‬‭(‭S‬ tudent‬‭student‬‭:‬‭students‬‭)‬‭{‬ ‭➔‬ ‭setProperty(String key, String value):‬‭Sets a key-value‬‭pair in the‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭(‭s‬ tudent‬‭);‬ ‭Properties object.‬
‭}‬
‭➔‬ ‭getProperty(String key):‬‭Retrieves the value associated‬‭with a‬
‭}‬
‭specified key.‬
‭}‬
‭➔‬ ‭store(OutputStream out, String comments)‬‭: Saves properties‬‭to‬
‭Comparator Interface‬ ‭an output stream, with optional comments.‬

‭ ‬ T‭ he Comparator interface in Java is located in the java.util package.‬ ‭➔‬ ‭load(InputStream in)‬‭: Loads properties from an input‬‭stream.‬
‭➔‬ ‭It defines two methods:‬‭compare(T o1, T o2‬‭) and‬‭equals(Object‬‭obj)‬‭.‬ ‭➔‬ ‭stringPropertyNames()‬‭: Returns keys where both the‬‭key and‬
‭➔‬ ‭You typically create an instance of Comparator either as an anonymous‬ ‭value are strings.‬
‭class.‬
i‭mport‬‭java‬‭.‬‭io‬‭.‬‭*‭;‬ ‬
‭➔‬ ‭Comparator is commonly used with sorting methods like‬ ‭import‬‭java‬‭.‬‭util‬‭.‬‭*‭;‬ ‬
‭Collections.sort()‬‭for lists or‬‭Arrays.sort()‬‭for‬‭arrays to define the order‬
‭in which elements should be sorted.‬ ‭public‬‭class‬‭Student‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‬‭String‬‭[]‬‭args‬‭)‬‭{‬
‭Example of Comparator Interface‬ ‭Properties‬‭prop‬‭=‬‭new‬‭Properties‬‭();‬

‭import‬‭java‬‭.‬‭util‬‭.‭*‬ ‬‭;‬ /‭ / Setting properties‬


‭prop‬‭.‬‭setProperty‬‭("‬‭database.url‬‭",‬‭"‬‭jdbc:mysql://localhost:3306/mydb‬‭");‬
‭public‬‭class‬‭Student‬‭{‬
‭private‬‭String‬‭name‬‭;‬
‭prop‬‭.‬‭setProperty‬‭("‬‭database.user‬‭",‬‭"‭r‬ oot‬‭");‬
‭private‬‭int‬‭age‬‭;‬ ‭prop‬‭.‬‭setProperty‬‭("‬‭database.password‬‭",‬‭"‬‭password‬‭");‬

‭public‬‭Student‬‭(‭S‬ tring‬‭name‬‭,‬‭int‬‭age‬‭)‬‭{‬ /‭ / Saving properties to a file‬


‭this‬‭.‬‭name‬‭=‬‭name‬‭;‬ ‭try‬‭(‭O ‬ utputStream‬‭output‬‭=‬‭new‬
‭this‬‭.‬‭age‬‭=‬‭age‬‭;‬ ‭FileOutputStream‬‭("‬‭config.properties‬‭"))‬‭{‬
‭}‬ ‭prop‬‭.‬‭store‬‭(‭o ‬ utput‬‭,‬‭"‬‭Database Configuration‬‭");‬
‭System‬‭.‭o ‬ ut‬‭.‬‭println‬‭("‬‭Properties saved successfully.‬‭");‬
‭int‬‭getAge‬‭()‬‭{‬‭return‬‭this‬‭.‬‭age‬‭;}‬
‭}‬‭catch‬‭(‭I‬OException‬‭e‭)‬ ‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‬‭String‬‭[]‬‭args‬‭)‬‭{‬ ‭e‭.‬‬‭printStackTrace‬‭();‬
‭List‬‭<‭S‬ tudent‬‭>‬‭students‬‭=‬‭new‬‭ArrayList‬‭<>();‬ ‭}‬
‭students‬‭.‭a‬ dd‬‭(‭n ‬ ew‬‭Student‬‭("‬‭Alice‬‭",‬‭20‬‭));‬
‭students‬‭.‭a‬ dd‬‭(‭n‬ ew‬‭Student‬‭("‬‭Bob‬‭",‬‭18‬‭));‬ /‭ / Loading properties from a file‬
‭students‬‭.‭a‬ dd‬‭(‭n
‬ ew‬‭Student‬‭("‬‭Charlie‬‭",‬‭22‬‭));‬ ‭try‬‭(‭I‬nputStream‬‭input‬‭=‬‭new‬‭FileInputStream‬‭("‬‭config.properties‬‭"))‬‭{‬
‭prop‬‭.‬‭load‬‭(‭i‬nput‬‭);‬
/‭ / Using Comparator to sort by age in descending‬‭order‬
‭System‬‭.‭o
‬ ut‬‭.‬‭println‬‭("‬‭Properties loaded‬‭successfully.‬‭");‬
‭Comparator‬‭<‬‭Student‬‭>‬‭ageComparator‬‭=‬‭new‬‭Comparator‬‭<‭S‬ tudent‬‭>()‬‭{‬
‭@‭O ‬ verride‬
‭public‬‭int‬‭compare‬‭(‬‭Student‬‭s1‬‭,‬‭Student‬‭s2‬‭)‬‭{‬ /‭ / Display properties‬
‭return‬‭Integer‬‭.‬‭compare‬‭(‬‭s2‬‭.‬‭getAge‬‭(),‬‭s1‬‭.‬‭getAge‬‭());‬‭// Descending order‬ ‭prop‬‭.‬‭forEach‬‭((‬‭key‬‭,‬‭value‬‭)‬‭->‬‭{‬
‭}‬ ‭System‬‭.‬‭out‬‭.‬‭println‬‭(‭k‬ ey‬‭+‬‭"‭:‬ ‬‭"‬‭+‬‭value‬‭);‬
‭};‬ ‭});‬

/‭ / Sorting students list using ageComparator‬ /‭ / Accessing individual property‬


‭Collections‬‭.‬‭sort‬‭(‭s‬ tudents‬‭,‬‭ageComparator‬‭);‬
‭String‬‭dbUrl‬‭=‬‭prop‬‭.‬‭getProperty‬‭("‬‭database.url‬‭");‬
/‭ / Printing sorted students‬
‭System‬‭.‭o ‬ ut‬‭.‬‭println‬‭("‬‭Database URL:‬‭"‬‭+‬‭dbUrl‬‭);‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Sorted Students by Age‬‭(Descending):‬‭");‬ ‭}‬‭catch‬‭(‭I‬OException‬‭e‭)‬ ‬‭{‬
‭for‬‭(‭S‬ tudent‬‭student‬‭:‬‭students‬‭)‬‭{‬ ‭e‭.‬‬‭printStackTrace‬‭();‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭(‭s‬ tudent‬‭);‬ ‭}‬
‭}‬ ‭}‬
‭}‬ ‭}‬
‭}‬

You might also like