Related Topics

JAVA Programming
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
int top = stack.peek(); // returns 3
int popped = stack.pop(); // removes and returns 3
And here is an example of how to use a Queue in Java:
Queue<String> queue = new LinkedList<>();
queue.offer("apple");
queue.offer("banana");
queue.offer("cherry");
String first = queue.peek(); // returns "apple"
String polled = queue.poll(); // removes and returns "apple"
List<String> list = Arrays.asList("apple", "banana", "cherry");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}
And here is an example of how to use a ListIterator in Java:
List<String> list = Arrays.asList("apple", "banana", "cherry");
ListIterator<String> iterator = list.listIterator(list.size());
while (iterator.hasPrevious()) {
String element = iterator.previous();
System.out.println(element);
}
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
You can use the get() method to look up a value based on its associated key:
int value = map.get("banana"); // returns 2
And you can remove a key-value pair from the Map using the remove() method:
map.remove("cherry");
public class Person implements Comparable<Person> {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public int compareTo(Person other) {
return Integer.compare(this.age, other.age);
}
}
We can use the Collections.sort() method to sort a list of Person objects using the natural ordering defined by the compareTo() method:
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 20));
people.add(new Person("Charlie", 30));
Collections.sort(people);
After sorting, the list will contain the Person objects in the order: Bob, Alice, Charlie.
Comparator:
Comparator is an interface that is used to define an external way of comparing two objects of a class that does not implement the Comparable interface.
It provides two methods, compare() and equals().
The compare() method takes two objects as arguments and returns a negative integer, zero, or a positive integer, indicating whether the first object is less than, equal to, or greater than the second object.
The Comparator interface allows for multiple ways of sorting an object, which can be useful when natural ordering is not sufficient.
For example, if we have a class Student and want to sort based on their grade in descending order, we can create a separate Comparator class:
public class GradeComparator implements Comparator<Student> {
public int compare(Student a, Student b) {
return Integer.compare(b.getGrade(), a.getGrade());
}
}
We can then use the Collections.sort() method with this comparator to sort a list of Student objects:
List<Student> students = new ArrayList<>();
students.add(new Student("Alice", 80));
students.add(new Student("Bob", 90));
students.add(new Student("Charlie", 70));
Collections.sort(students, new GradeComparator());
After sorting, the list will contain the Student objects in the order: Bob, Alice, Charlie.
Map<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
int value = entry.getValue();
System.out.println(key + ": " + value);
}
This will output:
apple: 1
banana: 2
cherry: 3
We can also use the setValue() method of a Map.Entry object to modify the value associated with the key:
Map.Entry<String, Integer> entry = map.entrySet().iterator().next();
entry.setValue(4);
After this, the value associated with the key “apple” will be 4.
Overall, the Map.Entry interface provides a convenient way to work with key-value pairs in a Map, especially when iterating over the entries in the Map.
Set<Integer> set = new HashSet<>();
set.add(2);
set.add(1);
set.add(3);
The order of the elements in the set is not guaranteed, and may be different each time the code is executed.
TreeSet:
TreeSet is implemented using a self-balancing binary search tree (specifically, a red-black tree).
The elements in a TreeSet are stored in sorted order, based on their natural ordering or a specified comparator.
TreeSet provides O(log n) time performance for add(), remove(), and contains() operations.
TreeSet is generally faster than HashSet for iterating over the elements, but slower for adding and removing elements.
For example, we can create a TreeSet of integers and add some elements to it:
Set<Integer> set = new TreeSet<>();
set.add(2);
set.add(1);
set.add(3);
The order of the elements in the set will be sorted in ascending order, i.e., 1, 2, 3.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.