I have created an ArrayList like the following:
def list = new ArrayList()
But the codenarc report it is warning like following.
ArrayList objects are better instantiated using the form "[] as ArrayList"
What are the better ways to instantiate the collections?
You can do:
def list = [] // Default is ArrayList
def list = [] as ArrayList
ArrayList list = []
And again, for HashMap
:
HashMap map = [:]
def map = [:] as HashMap
The default in this case is a LinkedHashMap
:
def map = [:]
Typical is:
def list = []
other options include
def list = new ArrayList()
def list = new ArrayList<Foo>()
List list = new ArrayList()
def list = [] as ArrayList
List list = [] as ArrayList
and of course:
List<Foo> list = new ArrayList<Foo>();
Similar options exist for HashMap
using [:]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With