Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Performance Indexing In SQL Server

Authors: Jason Strate, Grant Fritchey

2nd Edition

1484211189, 9781484211182

More Books

Students also viewed these Databases questions

Question

3. Is there opportunity to improve current circumstances? How so?

Answered: 1 week ago

Question

2. What do you believe is at the root of the problem?

Answered: 1 week ago