Write a method in the HashIntSet class called retainAll that accepts another hash set as a parameter

Question:

Write a method in the HashIntSet class called retainAll that accepts another hash set as a parameter and removes all elements from this set that are not contained in the other set. For example, if the set stores [−2, 3, 5, 6, 8] and the method is passed [2, 3, 6, 8, 11], your set would store [3, 6, 8].

Answer:

Step: 1 of 6

Method implementation of “retainALL” in HashIntSet class.

• Create a method named, “retainALL” that accepts ‘innerSet’ of type ‘HashSet‘ as a parameter and returns ‘outerSet’ of type ‘HashSet’.

• Loop through the ‘innerSet’ and check if element is contained in the ‘outerSet’. If the element is not contained then add to the ‘outerSet’.

• Return the modified ‘outerSet’.

Method Definition:

Method definition consists of modifier, return type, method name, parameter list, method body. Here the method with name ‘retainALL‘ has a ‘public’ modifier, accepts ’innerSet‘ of type ‘HashSet‘ as a parameter and returns ‘outerSet’ of type ‘HashSet’. It can be defined as:

//Method definition of “retainALL”

public HashSet retainALL(HashSet innerSet)

{

tempSet = new HashSet( innerSet);

//Loop through the ‘outerSet’ and check if the element //is contained in the ‘tempSet’. If the element is //contained then remove it from the ‘tempSet’.

for(int element : outerSet)

{

if(tempSet.contains(element))

{

tempSet.remove(element);

}

}

//Loop through the ‘tempSet’ and check if element is //contained in the ‘innerSet’. If the element is //contained then remove it from ‘innerSet’.

for(int element : tempSet)

{

if(innerSet.contains(element))

{

innerSet.remove(element);

}

}

//Return the modified innerSet

return innerSet;

}//end of Method “retainALL”

Step: 2 of 6

Program Plan:

• Create a class with name HashIntSet and implement the method retainALL () in it.

• Declare and initialize the ‘outerSize’, ‘innerSize’ of type integer with the user input from console using Scanner class.

• Declare and initialize the ‘outerSet’, ‘innerSet’ of type HashSet with the user input from console using Scanner class.

• Print all the elements in the outerSize and innerSize before invoking the method retainALL ().

• Invoke the method retainALL () with the parameter ‘list’ of type List which switches the consecutive elements in the list.

• Print all the elements in the outerSet after invoking the method retainALL ().

Step: 3 of 6

Program:

The class name is HashIntSet.

Code in HashIntSet .java file:

/**********************************************************

* The following program is to retain the elements of innerSet * passed as parameter to the method ‘retainALL’ that are

* contained in the outerSet.

**********************************************************/

//Header Section

import java.util.HashSet;

//to import predefined Hashset class

import java.util.Scanner;

//to import Scanner class that allows to read all types of //data from user

//main class, “HashIntSet”

public class HashIntSet

{

private HashSet outerSet, tempSet;

//Method definition of “retainALL”

public HashSet retainALL(HashSet innerSet)

{

tempSet = new HashSet( innerSet);

Step: 4 of 6

//Loop through the ‘outerSet’ and check if the element //is contained in the ‘tempSet’. If the element is //contained then remove it from the ‘tempSet’.

for(int element : outerSet)

{

if(tempSet.contains(element))

{

tempSet.remove(element);

}

}

//Loop through the ‘tempSet’ and check if element is //contained in the ‘innerSet’. If the element is //contained then remove it from ‘innerSet’.

for(int element : tempSet)

{

if(innerSet.contains(element))

{

innerSet.remove(element);

}

}

//Return the modified innerSet

return innerSet;

}//end of Method “retainALL”inALL”

//main method

public static void main(String args[])

{

//Scanner to get user input from console.

Scanner input = new Scanner(System.in);

// Try block to catch exception to avoid //abnormal termination of the program.

try

{

HashIntSet obj = new HashIntSet();

//while loop to repeatedly ask user to ask user //input after the displaying the result for the //previous input.

while(true)

{

System.out.print(“Enter the size of the Outer HashSet: “);

//Initilialize ‘outersize’ variable with //data inputted from user console using //Scanner class.

int outerSize = input.nextInt();

//if the user input is not within the //range, throw exception

if (outerSize <= 0)

{

throw new IllegalArgumentException();

}

//Create a new HashSet of type integer obj.outerSet = new HashSet();

//use for loop to read ‘outerSize’ elements //using Scanner and add each element to //the outerSet.

for(int i=0; i < outerSize; i++)

{

System.out.print(“Enter the element at HashSet[” + i + “]: “);

obj.outerSet.add(input.nextInt());

}

System.out.print(“Enter the size of the inner HashSet: “);

//Initilialize ‘innersize’ variable with //data inputted from user console using //Scanner class.

int innerSize = input.nextInt();

//if the user input is not within the //range, throw exception

if (innerSize <= 0)

{

throw new IllegalArgumentException();

}

//Create a new HashSet of type integer

HashSet innerSet = new HashSet();

//use for loop to read ‘innerSize’ elements //using Scanner and add each element to //the outerSet.

for(int i=0; i < innerSize; i++)

{

System.out.print(“Enter the element at HashSet[” + i + “]: “);innerSet.add(input.nextInt());

}

//Print all the elements of the outerSet

System.out.println(“Elements of Outer Hash set: “+ obj.outerSet);

Step: 5 of 6

//Print all the elements of the outerSet //after adding the innerSet elements.

System.out.println(“Outer Hash set AFTER removal: “+ obj.retainALL(innerSet) );

} //end of while loop

}//end of try block

catch (IllegalArgumentException e)

{

System.out.println(“<– Illegal Argument Exception –> Value Should be >= Zero”);

}

catch (Exception e)

{

System.out.print(“Error –> ” + e.getMessage());

}

}//end of main method

}//end of class HashIntSet

Step: 6 of 6

Sample Output:

Enter the size of the Outer HashSet: 5

Enter the element at HashSet[0]: -2

Enter the element at HashSet[1]: 3

Enter the element at HashSet[2]: 5

Enter the element at HashSet[3]: 6

Enter the element at HashSet[4]: 8

Enter the size of the inner HashSet: 5

Enter the element at HashSet[0]: 2

Enter the element at HashSet[1]: 3

Enter the element at HashSet[2]: 6

Enter the element at HashSet[3]: 8

Enter the element at HashSet[4]: 11

Outer Hash set BEFORE adding: [3, 5, 6, 8, -2]

Outer Hash set AFTER adding: [3, 6, 8]

Click to Rate This Answer!
[Total: 1 Average: 5]

Leave a Comment

Your email address will not be published. Required fields are marked *