CS & IT
ENGINEERING
Java With OOPS
Java Collections
Lecture No-02 By- Aditya sir
Recap of Previous Lecture
Topic
Arraylist
Topics to be Covered
Topic Java Collections
Arraylist
linkedlist
3
Tuestions
Topic : Linked List
• Definition : A LinkedList is a doubly-linked list implementation of the List and Deque interfaces in
Java.
• Advantages:
o Efficient insertions and deletions at the beginning or end.
o Useful when frequent modifications occur.
11
• Disadvantages:
o Slower random access compared to ArrayList.
• Common Methods:
TE
o add(), addFirst(), addLast()
o remove(), removeFirst(), removeLast()
o get(), getFirst(), getLast(), set()
o size(), contains(), clear()
Key Point:LinkedList is optimal for scenarios that require many insertions and deletions.
Topic : Linked List
Example 1 Creating and Adding Elements
• Problem Statement: Create a LinkedList of names and print the list.
• Hint: Use the simple add() method to append elements.
Topic : Linked List
• Code:
import [Link];
public class LL_Example1 {
public static void main(String[] args) {
// Easy: Create and add elements [Link]
LinkedList<String> names = new LinkedList<>();
[Link]("Alice");
151
[Link]("Bob");
[Link]("Charlie");
[Link]("Names: " + names);
}
F
}
Topic : Linked List
Example 2 : Inserting at the Beginning and End
• Problem Statement: Create a LinkedList of integers and insert elements at the
beginning and end.
• Hint: Use addFirst() for the beginning and addLast() for the end.
Topic : Linked List
• Code:
import [Link];
public class LL_Example2 {
public static void main(String[] args) {
// Moderate: Insert at both ends
LinkedList<Integer> numbers = new LinkedList<>();
[Link](20);
[Link](30);
[Link]
[Link](40);
[Link](10);
[Link](50);
[Link]("Numbers: " + numbers);
}
}
Topic : Linked List
Example 3: Accessing and Updating Elements
• Problem Statement: Create a LinkedList of characters, retrieve the first and
last elements, then update the first element.
• Hint: Use getFirst(), getLast(), and set(index, element).
Topic : Linked List
• Code:
import [Link];
public class LL_Example3 {
public static void main(String[] args) {
// Moderate/Advanced: Access and update elements
LinkedList<Character> letters = new LinkedList<>();
[Link]('A');
[Link]('B'); 0
[Link]('C'); p
[Link]('D');
A
[Link]("First: " + [Link]());
[Link]("Last: " + [Link]());
[Link](0, 'Z');
D
[Link]("After update: " + letters);
2B CD
}
}
Topic : Linked List
Example 4: Removing Elements
• Problem Statement: Remove the first and last elements from a LinkedList of
strings and print the updated list.
• Hint: Use removeFirst() and removeLast() to remove items from the beginning
and end.
Topic : Linked List
• Code:
import [Link];
public class LL_Example4 {
public static void main(String[] args) {
// Advanced: Remove elements from both ends
LinkedList<String> items = new LinkedList<>();
[Link]("One");
[Link]("Two");
[Link]("Three");
[Link]("Four");
[Link]("Five");
One
[Link]();
[Link](); Fine
[Link]("After removals: " + items);
}
}
Topic : Linked List
Example 5: Iterating Through a LinkedList
• Problem Statement: Iterate over a LinkedList of weekdays and print each day
using a for-each loop.
• Hint: The enhanced for loop is ideal for iterating over collections without index
management.
Topic : Linked List
• Code:
import [Link];
public class LL_Example5 {
public static void main(String[] args) {
// Advanced: Iterate through the list
LinkedList<String> days = new LinkedList<>();
[Link]("Monday");
[Link]("Tuesday");
say
[Link]("Wednesday");
[Link]("Thursday");
[Link]("Friday");
for (String day : days) {
[Link](day);
}
}
}
Topic : List Sorting
• Sorting Basics:
o Sorting arranges elements in a specified order (natural or custom).
o Natural order for numbers is ascending; for strings, lexicographical.
• Using [Link]():
o [Link](list) sorts a list in natural order.
• Custom Sorting:
o Provide a Comparator for custom order (e.g., descending order).
o Lambda expressions can be used for concise custom comparators.
• Considerations:
o Sorting an ArrayList is generally fast using merge sort.
o For LinkedList, sorting is less efficient due to sequential access.
Key Point:
List sorting is essential for data organization; Java provides built-in methods for both natural and custom
ordering.
Topic : List sorting
Example 1 :Sorting Integers in Ascending Order
• Problem Statement: Create an ArrayList of integers and sort it in ascending
order.
• Hint: Use [Link]() for natural ordering
Topic : List sorting
Code:
import [Link];
import [Link];
public class SortIntegers_E1 {
public static void main(String[] args) {
// Easy: Natural ascending order
ArrayList<Integer> numbers = new ArrayList<>();
[Link](50);
[Link](10);
[Link](40);
[Link](20);
[Link](30);
[Link](numbers);
[Link]("Sorted Ascending: " + numbers);
}
}
Topic : List sorting
Example 2 : Sorting Strings Alphabetically
• Problem Statement: Create an ArrayList of strings and sort them
alphabetically.
• Hint: Use [Link]() on a list of strings.
Topic : List sorting
• Code:
import [Link];
import [Link]; Aditya aditya
public class SortStrings_E2 {
public static void main(String[] args) {
// Moderate: Alphabetical order
ArrayList<String> names = new ArrayList<>();
1 2
[Link]("Zara");
B C
[Link]("Alice");
A
[Link]("Charlie");
[Link]("Bob");
[Link]("Diana");
[Link](names);
[Link]("Alphabetically Sorted: " + names);
}
} lexicographical ASCIlvalue
Topic : List sorting
Example 3: Sorting in Descending Order (Custom Comparator)
• Problem Statement: Create an ArrayList of integers and sort it in descending
order.
• Hint: Use [Link]() with a custom Comparator or lambda expression.
Topic : List sorting
• Code:
import [Link];
import [Link];
import [Link];
public class DescendingSort_E3 {
public static void main(String[] args) {
// Moderate/Advanced: Descending order
ArrayList<Integer> numbers = new ArrayList<>();
[Link](10);
[Link](50);
[Link](30);
[Link](20);
[Link](40);
Topic : List sorting
to do
6
[Link](numbers, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return b - a;
so
at }
});
of
[Link]("Sorted Descending: " + numbers);
}
}
adtoTCnumner Comp
1
pub
11
I
Topic : List sorting
Example 4: Sorting with Lambda Expressions
Boms HI
• Problem Statement: Sort an ArrayList of integers in ascending order using a
lambda expression.
• Hint: Replace the custom comparator with a concise lambda.
Topic : List sorting
• Code:
import [Link];
import [Link];
public class LambdaSort_E4 {
public static void main(String[] args) {
// Advanced: Using lambda for sorting
ArrayList<Integer> numbers = new ArrayList<>();
[Link](100);
[Link](50);
[Link](150);
[Link](25);
Topic : List sorting
[Link](75);
[Link](numbers, (a, b) -> a - b);
[Link]("Sorted using Lambda: " + numbers);
}
}
a
b
Recap and Best Practices for LinkedList and List Sorting
Recap:
• LinkedList:
o Use for efficient insertions and deletions; not ideal for random access.
o Common operations: add, addFirst, addLast, remove, get, set, and iteration.
• List Sorting:
o Natural ordering via [Link]().
o Custom ordering requires a Comparator or lambda expression.
Best Practices:
• Use Generics to ensure type safety.
• For custom objects, define a clear Comparator based on key attributes.
• Test edge cases (e.g., empty lists, null values) when sorting.
• Choose the appropriate collection type based on your access and modification needs.
Conclusion
Summary:
• LinkedList and List Sorting:
o LinkedList is efficient for modifications; sorting is key for data organization.
o Mastery of both enhances your ability to handle dynamic data in real-world
applications.
• Final Tips:
o Practice these examples and modify them to explore different scenarios.
o Experiment with combining various operations for more complex problems.
o 0
p
I
THANK - YOU