Question: Here is a Java program that creates a Set using a HashSet implementation: import java.util.*; public class SetDemo { public static void main( String []

Here is a Java program that creates a Set using a HashSet implementation: import java.util.*; public class SetDemo { public static void main( String [] args ) { // Create a set called mySet Set mySet = new HashSet(); // Ensure that this set contains an interesting selection of fruit String fruit1 = "pear", fruit2 = "banana", fruit3 = "tangerine", fruit4 = "strawberry", fruit5 = "blackberry"; mySet.add( fruit1 ); mySet.add( fruit2 ); mySet.add( fruit3 ); mySet.add( fruit2 ); mySet.add( fruit4 ); mySet.add( fruit5 ); // Display contents of mySet System.out.println( "mySet now contains:" ); System.out.println( mySet ); } } Compile and execute this program. Why don't the fruit get listed in the order in which they were added? > Ans: The hash instance does not guarantee the order of a set Extend the program in a stepwise manner using the standard collection interface methods to do the following: > display the cardinality of (i.e. the number of elements in) mySet; > remove the blackberry and strawberry; display contents of mySet again to confirm success; > remove the remaining fruit using a single method invocation; > show that the set is now empty by using the isEmpty() method.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!