0

If I had the following declaration of the list:

List< Integer> b = new ArrayList< Integer>();

and

List< HashSet < Integer>> c = new ArrayList< HashSet< Integer>>();

I was wondering how would I be able to add b to c?

I've tried c.add(b); but this doesn't work.

Thanks for any help!

6
  • not at all. c holds (many) HashSets of Integers, b just Integers Commented Dec 4, 2012 at 19:02
  • They are different types, it is not possible. Compiler complains. Generics are for the purpose of making sure compile time compatibility. Commented Dec 4, 2012 at 19:02
  • In this case, what does it mean to add b to c? Did you want to have a HashSet<List<Integer>> for c instead? As it is, c is a list of sets of integers, so all you can add to it are sets of integers, not lists of integers. Commented Dec 4, 2012 at 19:03
  • I think it is better to sit back and think what you really want there, you want to directly add to integers (or) create a set and add it? Commented Dec 4, 2012 at 19:04
  • You have c as a List containing HashSets. b is a List, not a HashSet. Do you want c to be a List containing Lists or a HashSet containing lists? Or do you want b to be a HashSet? Commented Dec 4, 2012 at 19:04

5 Answers 5

1

You cant add a HashSet of integers to a list of integers. They're two different datatypes. If you would have a HashSet of integers instead of a List of integers, then you would be able to add them to your list.

like this:

HashSet< Integer> b = new HashSet< Integer>();
List< HashSet < Integer>> c = new ArrayList< HashSet< Integer>>();
c.add(b);
Sign up to request clarification or add additional context in comments.

Comments

1

You've declared c as a list of sets (specifically "hash-sets"), not a list of lists; so it doesn't make sense to add a list to it. If you want c to be a list of lists, you can write:

List<List<Integer>> c = new ArrayList<List<Integer>>();

c.add(b);

Comments

1

You are using generics and defining that b will be a list of Integers and c will be a list of HashSets that contain Integers, so no, you cannot add b to c

Comments

1

You can first create a HashSet and set elements from b to it and then add the HashSet to c as below:

    List< Integer> b = new ArrayList< Integer>();
    List< HashSet < Integer>> c = new ArrayList< HashSet< Integer>>();
    HashSet < Integer> b1= new HashSet<Integer>();
    b1.addAll(b);
    c.add(b1);

Comments

1

yes, because first list of integers the second one list of hashsets of integers There are ywo variants,

  • Add all values from first list to first value of secod list like that

HashSet<Integer> hs = new HashSet();
hs.addAll(b);
c.add(hs);
  • create new instances of HashSet for each value from first list and put one value from first list , for this solution use loop through first list
 for (Integer value: b){
     c.add(new HashSet<Integer>(){{add(value);}};);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.