Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 82d6fd2

Browse files
author
Rajeev Kumar Singh
committed
Java Stack, Queue, Comparator and Comparable
1 parent 8f5b91c commit 82d6fd2

File tree

9 files changed

+155
-7
lines changed

9 files changed

+155
-7
lines changed

‎Readme.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
6. [Java HashSet Tutorial with Examples](https://www.callicoder.com/java-hashset/)
1414

1515
7. [Java TreeSet Tutorial with Examples](https://www.callicoder.com/java-treeset/)
16+
17+
8. [Java Stack Class Tutorial with Examples](https://www.callicoder.com/java-stack/)
18+
19+
9. [Java Queue Interface Tutorial with Examples](https://www.callicoder.com/java-queue/)

‎java-comparable-comparator-examples/src/ComparableExample.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ public static void main(String[] args) {
1010

1111
employees.add(new Employee(1010, "Rajeev", 100000.00, LocalDate.of(2010, 7, 10)));
1212
employees.add(new Employee(1004, "Chris", 95000.50, LocalDate.of(2017, 3, 19)));
13-
employees.add(new Employee(1007, "David", 134000.00, LocalDate.of(2017, 9, 28)));
13+
employees.add(new Employee(1015, "David", 134000.00, LocalDate.of(2017, 9, 28)));
1414

1515
System.out.println("Employees (Before Sorting) : " + employees);
1616

1717
// This will use the `compareTo()` method of the `Employee` class to compare two employees and sort them.
1818
Collections.sort(employees);
1919

20-
System.out.println("Employees (After Sorting) : " + employees);
20+
System.out.println("\nEmployees (After Sorting) : " + employees);
2121

2222
// This will use the same `compareTo()` method of the `Employee` class but will reverse the order
2323
Collections.sort(employees, Comparator.reverseOrder());
24-
System.out.println("Employees (After Sorting with reverseOrder)" + employees);
24+
System.out.println("\nEmployees (After Sorting with reverseOrder)" + employees);
2525
}
2626

2727
}

‎java-comparable-comparator-examples/src/ComparatorExample.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public static void main(String[] args) {
2727
Collections.sort(employees, Comparator.comparing(Employee::getJoiningDate));
2828
System.out.println("\nEmployees (Sorted by JoiningDate) : " + employees);
2929

30-
// Sort employees by descending order of Name
30+
// Sort employees by Name in descending order
3131
Collections.sort(employees, Comparator.comparing(Employee::getName).reversed());
32-
System.out.println("\nEmployees (Sorted by Name Descending Order) : " + employees);
32+
System.out.println("\nEmployees (Sorted by Name in descending order) : " + employees);
3333

3434
// Chaining multiple Comparators
3535
// Sort by Salary. If Salary is same then sort by Name

‎java-comparable-comparator-examples/src/Employee.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import java.time.LocalDate;
2+
import java.util.Objects;
23

34
class Employee implements Comparable<Employee> {
45
private int id;
@@ -56,6 +57,20 @@ public int compareTo(Employee anotherEmployee) {
5657
return this.getId() - anotherEmployee.getId();
5758
}
5859

60+
// Two employees are equal if their IDs are equal
61+
@Override
62+
public boolean equals(Object o) {
63+
if (this == o) return true;
64+
if (o == null || getClass() != o.getClass()) return false;
65+
Employee employee = (Employee) o;
66+
return id == employee.id;
67+
}
68+
69+
@Override
70+
public int hashCode() {
71+
return Objects.hash(id);
72+
}
73+
5974
@Override
6075
public String toString() {
6176
return "Employee{" +

‎java-linkedhashmap-examples/src/RemoveEntriesFromLinkedHashMapExample.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ public static void main(String[] args) {
1111

1212
System.out.println("husbandWifeMapping : " + husbandWifeMapping);
1313

14-
// Remove the entry with the given key from the LinkedHashMap
14+
// Remove a key from the LinkedHashMap
1515
String wife = husbandWifeMapping.remove("John");
1616
System.out.println("Removed John and his wife " + wife + " from the mapping. New husbandWifeMapping : " + husbandWifeMapping);
1717

18-
// Remove the entry with the given key from the LinkedHashMap only if the key is mapped to the given value
18+
// Remove a key from the LinkedHashMap only if it is mapped to the given value
1919
boolean isRemoved = husbandWifeMapping.remove("John", "Susan");
2020
System.out.println("Did John get removed from the mapping? : " + isRemoved);
2121
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
public class QueueExample {
5+
public static void main(String[] args) {
6+
// Create and initialize a Queue using a LinkedList
7+
Queue<String> waitingQueue = new LinkedList<>();
8+
9+
// Adding new elements to the Queue (The Enqueue operation)
10+
waitingQueue.add("Rajeev");
11+
waitingQueue.add("Chris");
12+
waitingQueue.add("John");
13+
waitingQueue.add("Mark");
14+
waitingQueue.add("Steven");
15+
16+
System.out.println("WaitingQueue : " + waitingQueue);
17+
18+
// Removing an element from the Queue using remove() (The Dequeue operation)
19+
// The remove() method throws NoSuchElementException if the Queue is empty
20+
String name = waitingQueue.remove();
21+
System.out.println("Removed from WaitingQueue : " + name + " | New WaitingQueue : " + waitingQueue);
22+
23+
// Removing an element from the Queue using poll()
24+
// The poll() method is similar to remove() except that it returns null if the Queue is empty.
25+
name = waitingQueue.poll();
26+
System.out.println("Removed from WaitingQueue : " + name + " | New WaitingQueue : " + waitingQueue);
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
public class QueueSizeSearchFrontExample {
5+
public static void main(String[] args) {
6+
Queue<String> waitingQueue = new LinkedList<>();
7+
8+
waitingQueue.add("Jennifer");
9+
waitingQueue.add("Angelina");
10+
waitingQueue.add("Johnny");
11+
waitingQueue.add("Sachin");
12+
13+
System.out.println("WaitingQueue : " + waitingQueue);
14+
15+
// Check is a Queue is empty
16+
System.out.println("is waitingQueue empty? : " + waitingQueue.isEmpty());
17+
18+
// Find the size of the Queue
19+
System.out.println("Size of waitingQueue : " + waitingQueue.size());
20+
21+
// Check if the Queue contains an element
22+
String name = "Johnny";
23+
if(waitingQueue.contains(name)) {
24+
System.out.println("WaitingQueue contains " + name);
25+
} else {
26+
System.out.println("Waiting Queue doesn't contain " + name);
27+
}
28+
29+
// Get the element at the front of the Queue without removing it using element()
30+
// The element() method throws NoSuchElementException if the Queue is empty
31+
String firstPersonInTheWaitingQueue = waitingQueue.element();
32+
System.out.println("First Person in the Waiting Queue (element()) : " + firstPersonInTheWaitingQueue);
33+
34+
// Get the element at the front of the Queue without removing it using peek()
35+
// The peek() method is similar to element() except that it returns null if the Queue is empty
36+
firstPersonInTheWaitingQueue = waitingQueue.peek();
37+
System.out.println("First Person in the Waiting Queue : " + firstPersonInTheWaitingQueue);
38+
39+
}
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Stack;
2+
3+
public class StackExample {
4+
public static void main(String[] args) {
5+
// Creating a Stack
6+
Stack<String> stackOfCards = new Stack<>();
7+
8+
// Pushing new items to the Stack
9+
stackOfCards.push("Jack");
10+
stackOfCards.push("Queen");
11+
stackOfCards.push("King");
12+
stackOfCards.push("Ace");
13+
14+
System.out.println("Stack => " + stackOfCards);
15+
System.out.println();
16+
17+
// Popping items from the Stack
18+
String cardAtTop = stackOfCards.pop(); // Throws EmptyStackException if the stack is empty
19+
System.out.println("Stack.pop() => " + cardAtTop);
20+
System.out.println("Current Stack => " + stackOfCards);
21+
System.out.println();
22+
23+
// Get the item at the top of the stack without removing it
24+
cardAtTop = stackOfCards.peek();
25+
System.out.println("Stack.peek() => " + cardAtTop);
26+
System.out.println("Current Stack => " + stackOfCards);
27+
28+
}
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.Stack;
2+
3+
public class StackSizeSearchExample {
4+
public static void main(String[] args) {
5+
Stack<String> stackOfCards = new Stack<>();
6+
7+
stackOfCards.push("Jack");
8+
stackOfCards.push("Queen");
9+
stackOfCards.push("King");
10+
stackOfCards.push("Ace");
11+
12+
System.out.println("Stack : " + stackOfCards);
13+
14+
// Check if the Stack is empty
15+
System.out.println("Is Stack empty? : " + stackOfCards.isEmpty());
16+
17+
// Find the size of Stack
18+
System.out.println("Size of Stack : " + stackOfCards.size());
19+
20+
// Search for an element
21+
// The search() method returns the 1-based position of the element from the top of the stack
22+
// It returns -1 if the element was not found in the stack
23+
int position = stackOfCards.search("Queen");
24+
25+
if(position != -1) {
26+
System.out.println("Found the element \"Queen\" at position : " + position);
27+
} else {
28+
System.out.println("Element not found");
29+
}
30+
31+
}
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /