1

One of the questions I have been given asks:

All the lines should be stored in an object of type List<Set<Integer>>.

How do you write this in Java, as in how do you initialise this list? I've never seen this before.

Please provide a link to an explanation as i'm not sure what this is called in Java so have no idea about how to learn about it. Thank You.

adarshr
  • 61,315
  • 23
  • 138
  • 167
Kingsta1993
  • 25
  • 1
  • 1
  • 5
  • I've edited the question for you, but in the future, you can use backticks ( ` ) to specify code snippets, so that angle brackets work. – yshavit Dec 03 '14 at 21:56
  • 1
    It's using [generics](https://docs.oracle.com/javase/tutorial/java/generics/why.html) The given is a list of sets that are collections of integers. – scrappedcola Dec 03 '14 at 21:57

5 Answers5

5

Its a List of Sets where each Set can hold only Integers.

Set<Integer> singlesSet = new HashSet<>();
singlesSet.add(1);
singlesSet.add(2);

Set<Integer> tensSet = new HashSet<>();
tensSet.add(10);
tensSet.add(20);

List<Set<Integer>> list = new ArrayList<>();
list.add(singlesSet);
list.add(tensSet);

System.out.println(list);
Jerome Anthony
  • 7,823
  • 2
  • 40
  • 31
  • 1
    Note that in Java 7 and above you can use the "diamond operator" in the `new` statements: `new HashSet<>()` and `new ArrayList<>()`. – yshavit Dec 03 '14 at 22:14
  • that is the very same as my example, and you get +4 and I only +1 :(. And I was quicker :/ – martijnn2008 Dec 03 '14 at 22:23
  • SO is a harsh master, @martijnn2008! :-P I'd +1'd Jerome because I just looked at the "answered" timestamp, not noticing the history/edit timestamps. So I just +1'd you, since you had the full answer first. But the diamond operator holds for your code, too. :) – yshavit Dec 03 '14 at 22:31
  • I don't think diamond operator is the way to go for beginners. But still you have a good point. ty btw :) – martijnn2008 Dec 03 '14 at 22:37
3

Example of usages of Set and List. Note that elements in a TreeSet are always sorted.

List<Set<Integer>> listofsets = new ArrayList<Set<Integer>>();
Set<Integer> set1 = new TreeSet<Integer>();
set1.add(1);
set1.add(2);

Set<Integer> set2 = new TreeSet<Integer>();
set2.add(6);
set2.add(4);

listofsets.add(set);

// listofsets = {{1,2}, {4,6}}
martijnn2008
  • 3,552
  • 5
  • 30
  • 40
0

In Java, the List interface represents an abstract list of things. Any class the implements List (for example, LinkedList) must implement its methods and behave according to its contract.

You can essentially think of it as an array, but keep in mind that arrays are only one kind of list, and that implementations of List do no have to use arrays internally.

The Set also represents a collection of elements, but with no relationship or connection between them. Visually, you can think of a set as a sort of bag of things. You can add and remove things from the bag, but none of the items need to be related.

An Integer, of course, is just an object wrapper around Java's int primitive.

As such, a List<Set<Integer>> object would be similar to a two-dimensional array, only without a defined order in the second dimension.

You would initialize a List<Set<Integer>> as follows:

    List<Set<Integer>> myList = new ArrayList<HashSet<Integer>>();

Where ArrayList and HashSet can be any classes that implement List and Set, respectively.

yossarian
  • 1,537
  • 14
  • 21
0

Like this List<Set<Integer>> yourList = new ArrayList<Set<Integer>>();?

You may want to take a look at https://docs.oracle.com/javase/7/docs/api/java/util/List.html

MeetTitan
  • 3,383
  • 1
  • 13
  • 26
0

The short way:

    List<Set<Integer>> list = new ArrayList<Set<Integer>>();
    Set<Integer> set = new HashSet<Integer>();
    list.add(set);
    set.add(1);
    set.add(2);
   ....

What is the difference between Set and List?

Community
  • 1
  • 1
Gren
  • 1,850
  • 1
  • 11
  • 16