-2

I am following link : How to initialize HashSet values by construction? and could be very silly question to ask, but I didn't make it working, please guide.

public class Demo {
    public static void main(String[] args) {
        Set<Double> tempSet = new HashSet<Double>();
        tempSet.add(11.0);
        tempSet.add(22.0);

        // Below both lines not working
        Set<Double> tempSet1 = new HashSet<Double>({11.0, 22.0});
        Set<Double> tempSet1 = new HashSet<Double>(){11.0, 22.0};
    }
}
Kanika
  • 31
  • 2
  • 9

4 Answers4

1

This statement doesn't make any sense: Set<Double> tempSet1 = new HashSet<Double>({11.0, 22.0}); If you're trying to initiallize them in just one line of code try this: Set<String> h = new HashSet<>(Arrays.asList(new Double[] {11.0,22.0})); Select as answer if it works! :D

Alex Cuadrón
  • 638
  • 12
  • 19
1

If you are using Java 9, Then

Set<Double> dblSet5 = Set.of(11.20, 2.0, 32.0, 56.0);

Or

Set<Double> dblSet5 = Set.of(<Array Of Double>);
Ravi
  • 30,829
  • 42
  • 119
  • 173
0
Set<Double> tempSet1 = new Hashset<Double>(Arrays.asList(11.0,22.0));
Rajan Singh
  • 309
  • 2
  • 7
0

Based on the answers in that question, here are some examples:

//List initialization by using an array
Set<Double> h = new HashSet<>(Arrays.asList(11.0, 22.0));
//Java 8
Set<Double> set = Stream.of(11.0, 22.0).collect(Collectors.toSet());
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332