Lists of Sets and Sets of Lists
Write a program that reads several lines of integers. Each line of integers should be stored in
an object of type List<Integer>, and all the lines should be stored in an object of type
HashSet<List<Integer>>. Then the program should print out the contents of this set.
Code :
import java.util.*;
class q2
{
public static void main(String[] args) throws Exception
{
String morelines = "Yes";
int j=0;
while (!morelines.equals("No")) {
Scanner input = new Scanner(System.in);
System.out.println("Enter some integers (all on one line):");
String linej = input.nextLine();
String[] numsj = linej.split(" ");
List<Integer> aj = new HashSet<List<Integer>>();
for(int i=0; i<numsj.length; i++)
aj.add(Integer.parseInt(numsj[i]));
System.out.println("Do you want to enter some more lines? Yes or No");
morelines = input.nextLine();
j=j+1;
}
for (int i=0; i<j; i++)
System.out.println("The integers are: "+ai);
}
}
Whats wrong with the code?
Re: Lists of Sets and Sets of Lists
You cannot assign a HashSet to a List
Code :
List<Integer> aj = new HashSet<List<Integer>>();//???
It'd be more appropriate to create a new list for each line, add things, then add the list to a previously created HashSet
Re: Lists of Sets and Sets of Lists
Code :
HashSet< List<Integer> > aj = new HashSet<List<Integer>>();