When we use a collection in your code, we typically do following.
- Declare a variable for a temporary collection.
- Create a new empty collection and store a reference to it in the variable.
- Put things into the collection.
- Pass the collection to the method.
[one, two, three]
Above are normal steps we all follow in our coding practices. Don’t you feel that Java should have a more convenient syntax for collections (lists, maps, sets, etc.)?
Let’s see another easy way of doing it. This is know as double brace initialization .
[one, two, three]
How does above code work?
The first brace creates a new Anonymous Inner Class. These inner classes are capable of accessing the behavior of their parent class. So, in our case, we are actually creating a subclass of HashSet class, so this inner class is capable of using add() method.
This article is contributed by Saumya Mishra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Instance Initialization Block (IIB) in Java
- Initialization of local variable in a conditional block in Java
- Order of execution of Initialization blocks and Constructors in Java
- Java.Lang.Double class in Java
- Convert Double to Integer in Java
- Double floatValue() in Java with Examples
- Convert String to Double in Java
- Double longValue() in Java with Examples
- Double doubleValue() method in Java with examples
- Double intValue() method in Java with examples
- DoubleStream.Builder add(double t) in Java with Examples
- Double hashCode() method in Java with examples
- Double shortValue() method in Java with examples
- Double isInfinite() method in Java with examples
- Double parseDouble() method in Java with examples