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
Instructor Solutions Manual For Introduction To Java Programming And Data Structures, Comprehensive Version, 11th Edition
- 6. See the code below and solve the following. import java.io.*; public class DataStream { } public static void main(String[] args) } DataOutputStream output = new DataOutputStream(new FileOutputStream("temp.dat")); output.writeUTF("Book1"); output.writeInt(85); output.writeUTF("Book2"); output.writeInt(125); output.writeUTF("Book3"); output.writeInt(70); output.close(); // ToDo: Read all data from temp.dat and print the data to the standard output (monitor) 6-1. This program has a compile error, and the message is “Unhandled exception type FileNotFoundException". How do you fix this error? (1 point) 6-2. Is FileNotFoundException a checked exception or an unchecked exception? (1 point) 6-3. What is the difference between checked exception and unchecked exception? (1 point) 6-4. Please complete the above program by reading all data from temp.dat and print the data to the standard output (monitor) by using System.out.print, System.out.println or System.out.printf method. (2 points)arrow_forwardWrite a program that reads a list of integers from input and determines if the list is a palindrome (values are identical from first to last and last to first). The input begins with an integer indicating the length of the list that follows. Assume the list will contain a maximum of 20 integers. Output "yes" if the list is a palindrome and "no" otherwise. The output ends with a newline. Hints: - use a for loop to populate the array based on the specified size (the first number entered) - use a for loop to check first value with last value, second value with second from end, etc. - if the values do not match, set a Boolean variable to flag which statement to output (yes or no) Ex: If the input is (remember to include spaces between the numbers): 6 1 5 9 9 5 1 the output is: yes Ex: If the input is: 5 1 2 3 4 5 the output is: C++ codingarrow_forwardDesign and draw a high-level "as-is" process diagram that illustrates a current process related to a product or service offered through the SSDCI.gov database.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningNew Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage LearningSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage



