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)
- The hash table array has capacity of 10. Capacity is the number of slots present in the array that is used to build the hashtable. The hash function returns the absolute value of the key mod the capacity of the hash table. a) Insert these keys in the hash table: 3,23,11,21,1,7,77,8 where the hash table uses quadratic probing to resolve collisions. b) Search and Delete 3 and 11 from the table. Be careful about changing the status of the table slot to “deleted” after deleting each item. c)Search 23 and 21 from the table and print their position.arrow_forwardUse following LinkedList code as a reference, add Find the average data values of the linked list. Find the node with largest key, and then delete the node. (Note, you must return the node, not just the largest key)Test this operations in the Main method. (display the average of the data values of the linked list, the largest key, the linked list before and after deleting the node with the largest key. Show screenshot import java.util.*; public class LinkedList{ public Node header; public LinkedList() { header = null; } public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; } public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; } public final Node Remove() { Node x = header; if (header != null)…arrow_forwardCourse: 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_forward
- 3. 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_forwardClass HashTable: Implement a hash table to store integers (including negative ones). stored in the table int[] data. Use the hash function: h(x) = (x · 701) mod 2000. The table size is 2000. Ensure non-negative indices between 0 and 1999. Implement the following methods: insert(int key): Inserts the integer into the table. Returns true if successful, false if the element is already in the table. search(int key): Searches for the integer in the table. Returns true if found, false otherwise. delete(int key): Deletes the integer from the table. Returns true if successful, false otherwise. Class HashTable2: Implement a second hash table using a different hash function and collision resolution strategy. Keys are integers (including negative ones). Use the hash function: ℎ(�)=(�⋅53)mod 100h(x)=(x⋅53)mod100. The table size is 100. Ensure non-negative indices between 0 and 99. Implement the following methods: insert(int key): Inserts the integer into the table. Returns true if…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
- Need help with this computer scinece questionarrow_forwardplease 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_forward
- All of these statements are related to hash function, hash table.arrow_forwardTOPIC: 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_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