常规创建Arraylist和Hashmap有哪些不同的方法 [英] what are the different ways to create an Arraylist and Hashmap in groovy
本文介绍了常规创建Arraylist和Hashmap有哪些不同的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def list = new ArrayList()
$> c $ c>
但是,codenarc报告中显示的警告如下。
ArrayList对象最好使用形式[] as ArrayList
是实例化集合的更好方法吗?
解决方案
您可以这样做:
def list = [] //默认是ArrayList
def list = [] as ArrayList
ArrayList list = []
同样,对于 HashMap
:
HashMap map = [:]
def map = [:] as HashMap
这种情况下的默认值是 LinkedHashMap
:
def map = [:]
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 = [:]
这篇关于常规创建Arraylist和Hashmap有哪些不同的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文