SetOperation.java
Program Plan:
- Include the class name named “SetOperation”.
- Import java array class from util package.
- Import java linkedhash set class from util package.
- Import the set interface.
- Define class.
- Declare the main()method.
- Create linked list Hash set set1 and assign name of person as values.
- Create another linked list Hash set set2 and assign name of person as values.
- Display the union of the two sets set1 and set2 using addAll() function and print the result.
- Display the difference of the two sets by calling the removeAll function and print the result.
- Display the intersection of the two sets and print the result.
- Close the main method.
- Close the class “SetOperation”.
The below java code to create two sets and assign person’s name to them and then perform set union, difference and intersection of the two sets.
Explanation of Solution
Program:
//import java array class from util package
import java.util.Arrays;
//import java linkedlist hash set class from util package
import java.util.LinkedHashSet;
//import the set interface
import java.util.Set;
//class Definition
public class SetOperation {
// main method
public static void main(String[] args) {
/* create linked list hash set Set1 and assign name of person as values */
Set<String> set1 = new LinkedHashSet<>(Arrays.asList("George", "Jim",
"John", "Blake", "Kevin", "Michael"));
/*Create another linked list hash set Set2 and assign name of person as values */
Set<String> set2 = new LinkedHashSet<>(Arrays.asList("George", "Katie","Kevin", "Michelle", "Ryan"));
/* Display the union of the two sets set1 and set2 using addAll function */
Set<String> union = new LinkedHashSet<>(set1);
union.addAll(set2);
// print the union results
System.out.println("Union of the two sets: " + union);
/* Display the difference of the two sets by calling the removeAll function
*/
Set<String> difference = new LinkedHashSet<>(set1);
difference.removeAll(set2);
// print the difference of two sets
System.out.println("Difference of the two sets: " + difference);
// Display the intersection of the two sets
Set<String> intersection = new LinkedHashSet<>();
/*
Check for the elements that are present in set 2 and also present in
* set 1
*/
for (String e : set2) {
if (set1.contains(e))
intersection.add(e);
}
// print the intersection of two sets
System.out.println("Intersection of the two sets: " + intersection);
}
}
Union of the two sets: [George, Jim, John, Blake, Kevin, Michael, Katie, Michelle, Ryan]
Difference of the two sets: [Jim, John, Blake, Michael]
Intersection of the two sets: [George, Kevin]
Want to see more full solutions like this?
Chapter 21 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
- Course: Data Structure and Algorithms Language: Java Kindly something and Answer in 1 hour. Read Carefully and give answer with all necesary details. See the image for askii codes. Question6: In this Problem, you are required to insert some keys into a hash table, using given hash functions. You have to Draw a Hash table with the inserted keys. Write total number of collisions encountered when a particular collision resolution technique is used. size= 13, H(X) = sum of Ascii codes of key % HTSIZE Keys: Mia, Bea, Zoe, Jan, Ada, Sam, Leo, Meo, Ben, Tim, Ted, Zod Use Linear probing: H’(X) = [H(X) +f(i))] % HTSIZE where f(i)=i where i=0,1,2,….arrow_forward3. Double hashing is one of the methods to resolve collision. Write a function to implement this method. The equations used in this method are given below. Note: implement everything within the double hash function. P = (P + INCREMENT(Key)) mod TABLE_SIZE INCREMENT(Key) = 1 + (Key mod INCR)arrow_forwardOur hash map data are all saved in a single LinkedList because of the shoddy way the map was built. The hash map's utility is reduced as a consequence.arrow_forward
- please follow instructions correctly. You are required to complete the LinkedList class. This class is used as a linked list that has many methods to perform operations on the linked list. To create a linked list, create an object of this class and use the addFirst or addLast or add(must be completed) to add nodes to this linked list. Your job is to complete the empty methods. For every method you have to complete, you are provided with a header, Do not modify those headers(method name, return type or parameters). You have to complete the body of the method. package chapter02; public class LinkedList { protected LLNode list; public LinkedList() { list = null; } public void addFirst(T info) { LLNode node = new LLNode(info); node.setLink(list); list = node; } public void addLast(T info) { LLNode curr = list; LLNode newNode = new LLNode(info); if(curr == null) { list = newNode; } else…arrow_forwardC++ Design a HashMap without using any built-in hash table libraries. Implement the MyHashMap class: MyHashMap() initializes the object with an empty map. void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corresponding value. int get(int key) returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key. void remove(key) removes the key and its corresponding value if the map contains the mapping for the key. Example 1: Input ["MyHashMap", "put", "put", "get", "get", "put", "get", "remove", "get"] [[], [1, 1], [2, 2], [1], [3], [2, 1], [2], [2], [2]] Output [null, null, null, 1, -1, null, 1, null, -1]arrow_forwardAll of these statements are related to hash function, hash table.arrow_forward
- TOPIC: hash table with chaining * For the insert method, we use table[hash(new_entry)].insert(0, new_entry); Why do we insert the new entry at position 0 of the list, not at the end of the list? * When we search for key 100001, the time for each search methods are displayed. Rank the four methods (sequential, binary, hash table 1 and hash table 2) from the slowest to the fastest. * Explain the ranking of the search methods based on time complexity and load factor (for hashing).arrow_forwardDevelop a procedure that will allow you to remove from a linked list all of the nodes that share a key?arrow_forwardThe putIfAbsent function in HashMap seems useless.arrow_forward
- We use LinkedLists to store all of our hash map data due to the sloppy manner in which the map was originally constructed. This substantially reduces the efficacy of a hash map.arrow_forwardTask on Hashing Given an array containing Strings, you need to write a code to store them in a hashtable. Assume that the Strings contain a combination of capital letters and numbers, and the String array will contain no more than 9 values.Use the hash function to be the (total number of consonants*24 + summation of the digits) %9. In case of a collision, use linear probing.For a String “ST1E89B8A32”, it’s hash function will produce the value=(3*24+(1+8+9+8+3+2))%9=4, hence it will be stored in index 4 of the hash table. Instructions Complete the following problem using concepts of Key index searching,sorting and hashing You may use JAVA or PYTHON to complete the tasks. If you are using JAVA, you must include the main method as well which should test your other methods and print the outputs according to the tasks. If you are using PYTHON, make sure your code has the methods invoked and proper printing statements according to the tasks.arrow_forwardTask on Hashing Given an array containing Strings, you need to write a code to store them in a hashtable. Assume that the Strings contain a combination of capital letters and numbers, and the String array will contain no more than 9 values.Use the hash function to be the (total number of consonants*24 + summation of the digits) %9. In case of a collision, use linear probing. For a String "ST1E89B8A32", it's hash function will produce the value=(3*24+(1+8+9+8+3+2))%9=4, hence it will be stored in index 4 of the hash table.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education