Sunday, June 4, 2023

How to add element at the head and tail of a LinkedList in Java? Example

To add elements at the head and tail of a LinkedList in Java, you can use the addFirst() and addLast() methods provided by the LinkedList class. 

Here's an example:



import java.util.LinkedList;

public class LinkedListExample {
    public static void main(String[] args) {
        // Creating a LinkedList
        LinkedList linkedList = new LinkedList<>();

        // Adding elements at the head using addFirst()
        linkedList.addFirst("Element 1");
        linkedList.addFirst("Element 2");
        linkedList.addFirst("Element 3");

        System.out.println("LinkedList after adding at the head: " + linkedList);

        // Adding elements at the tail using addLast()
        linkedList.addLast("Element 4");
        linkedList.addLast("Element 5");
        linkedList.addLast("Element 6");

        System.out.println("LinkedList after adding at the tail: " + linkedList);
    }
}

In this example, we create a LinkedList called linkedList. We use the addFirst() method to add elements at the head of the list. We add three elements: "Element 1", "Element 2", and "Element 3". Then, we use the addLast() method to add elements at the tail of the list. We add three more elements: "Element 4", "Element 5", and "Element 6". Finally, we print the updated LinkedList. The output of this example will be:


LinkedList after adding at the head: [Element 3, Element 2, Element 1]
LinkedList after adding at the tail: [Element 3, Element 2, Element 1, Element 4, Element 5, Element 6]


As you can see, the elements are added at the head and tail of the LinkedList as expected.

How to Union and Intersection of two Set in Java - Google Guava Example

To perform union and intersection operations on two sets in Java using Google Guava, you can utilize the Sets.union() and Sets.intersection() methods provided by the Guava library. 

Here's an example:


import com.google.common.collect.Sets;
import java.util.HashSet;
import java.util.Set;

public class SetOperationsExample {
    public static void main(String[] args) {
        // Creating two sets
        Set set1 = new HashSet<>();
        Set set2 = new HashSet<>();

        // Adding elements to set1
        set1.add(1);
        set1.add(2);
        set1.add(3);

        // Adding elements to set2
        set2.add(2);
        set2.add(3);
        set2.add(4);

        // Performing union operation using Guava's Sets.union()
        Set union = Sets.union(set1, set2);
        System.out.println("Union: " + union);

        // Performing intersection operation using Guava's Sets.intersection()
        Set intersection = Sets.intersection(set1, set2);
        System.out.println("Intersection: " + intersection);
    }
}


In this example, we create two sets set1 and set2 using the HashSet class. We add elements to both sets. Then, we use Sets.union(set1, set2) to perform the union operation and Sets.intersection(set1, set2) to perform the intersection operation. 

The results are stored in the union and intersection sets, respectively. 

Finally, we print the results. Make sure you have the Guava library added to your project's dependencies for this code to work.

Friday, June 2, 2023

Difference between PriorityQueue and TreeSet in Java? Example

Both PriorityQueue and TreeSet are implementations of the Set interface in Java, but they have some differences in terms of their underlying data structures and the ordering of elements. 

Data structure:

PriorityQueue: It uses a binary heap data structure to store its elements. The elements in a PriorityQueue are ordered based on their natural ordering or a custom comparator. 

TreeSet: It internally uses a self-balancing binary search tree, specifically a red-black tree, to store its elements. The elements in a TreeSet are ordered based on their natural ordering or a custom comparator. 

Ordering of elements: 

PriorityQueue: Elements in a PriorityQueue are ordered based on their priority. The priority can be determined either by the natural ordering of the elements or by a custom comparator. The element with the highest priority will be at the head of the queue. 

TreeSet: Elements in a TreeSet are ordered in a sorted manner. They are stored in a specific order defined by their natural ordering or a custom comparator. The elements are sorted in ascending order by default. 

Duplicates: 

PriorityQueue: It allows duplicate elements. Elements with the same priority can exist in a PriorityQueue. 

TreeSet: It does not allow duplicate elements. Any attempt to add a duplicate element to a TreeSet will be ignored. Here's an example to demonstrate the differences:


import java.util.PriorityQueue;
import java.util.TreeSet;

public class SetExample {
    public static void main(String[] args) {
        // PriorityQueue example
        PriorityQueue priorityQueue = new PriorityQueue<>();
        priorityQueue.add(10);
        priorityQueue.add(5);
        priorityQueue.add(15);
        priorityQueue.add(5); // Duplicate element

        System.out.println("PriorityQueue: " + priorityQueue);
        // Output: PriorityQueue: [5, 5, 15, 10]

        // TreeSet example
        TreeSet treeSet = new TreeSet<>();
        treeSet.add(10);
        treeSet.add(5);
        treeSet.add(15);
        treeSet.add(5); // Duplicate element (ignored)

        System.out.println("TreeSet: " + treeSet);
        // Output: TreeSet: [5, 10, 15]
    }
}


In this example, we create a PriorityQueue and a TreeSet to store integers. We add elements to both collections, including a duplicate element (5) in each case. 

As we can see from the output, the PriorityQueue retains the duplicate element, while the TreeSet ignores it. Additionally, the PriorityQueue arranges the elements based on their priority, whereas the TreeSet arranges them in sorted order.

How to Remove Objects from Collection or List in Java? Iterator remove() method Example

In Java, you can remove objects from a collection or list using the remove() method of the Iterator interface. 

The Iterator interface provides a way to iterate over a collection and perform various operations, including removing elements while iterating. 

Here's an example that demonstrates how to use the remove() method:


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RemoveFromListExample {
    public static void main(String[] args) {
        // Create a list
        List fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");
        fruits.add("Mango");

        // Create an iterator for the list
        Iterator iterator = fruits.iterator();

        // Iterate over the list and remove objects
        while (iterator.hasNext()) {
            String fruit = iterator.next();
            if (fruit.equals("Banana") || fruit.equals("Mango")) {
                iterator.remove(); // Removes the current element from the list
            }
        }

        // Print the updated list
        System.out.println(fruits); // Output: [Apple, Orange]
    }
}

In this example, we create a list of fruits and add several elements to it. Then, we obtain an iterator for the list using the iterator() method. Next, we iterate over the list using a while loop and the hasNext() and next() methods of the iterator.

Inside the loop, we check if the current fruit is either "Banana" or "Mango" and use the remove() method to remove it from the list. After the iteration is complete, we print the updated list to verify that the "Banana" and "Mango" elements have been removed. 

Note that using the Iterator's remove() method is the recommended way to remove elements while iterating over a collection or list in Java, as it avoids potential concurrent modification issues.

Top 5 Microsoft Azure Developer Associate Practice Test in 2023 - Best of Lot

However, I can provide you with a list of popular practice test resources for the Microsoft Azure Developer Associate certification.

Please note that the popularity and availability of practice tests may vary over time, so it's recommended to check the respective platforms and websites for the most up-to-date information. Here are five well-regarded practice test resources:


  • Whizlabs Azure Developer Associate Practice Tests: Whizlabs offers a comprehensive set of practice tests specifically designed for the Azure Developer Associate certification. These tests cover the exam objectives and provide detailed explanations for each question.

  • MeasureUp Azure Developer Associate Practice Test: MeasureUp is a reputable platform that offers official practice tests for various Microsoft certifications. Their Azure Developer Associate practice test can help you assess your knowledge and readiness for the certification exam.

  • Udemy Azure Developer Associate Practice Tests: Udemy hosts a variety of practice test courses created by different instructors. Look for highly rated and up-to-date courses that focus on Azure Developer Associate practice tests to supplement your exam preparation.

  • ExamTopics Azure Developer Associate Practice Tests: ExamTopics is a website that provides free practice tests and study materials for a wide range of certifications. They have practice tests available for the Azure Developer Associate certification, allowing you to assess your knowledge and identify areas for improvement.

  • Azure Developer Associate Practice Exams on Microsoft Learn: Microsoft Learn is an official learning platform that provides free resources for Microsoft certifications. They offer practice exams for the Azure Developer Associate certification, allowing you to test your knowledge of Azure development concepts.


Remember to utilize multiple practice test resources to gain a comprehensive understanding of the exam objectives and increase your chances of success. It's also essential to review the official Microsoft Azure documentation, hands-on labs, and other study materials to strengthen your understanding of Azure development concepts.