Note groovy maps are different to json maps, and proposed Java map literals. Grrovy maps use similar syntax to arrays
1/ Groovy syntax
Map
def map= ['id':'FX-11', 'name':'Radish', 'no':1234, 99:'Y']
assert map == ['name':'Radish', 'id':'FX-11', 99:'Y', 'no':1234] //order of keys irrelevant
def map4= [:]
assert map == java.util.LinkedHashMap
def map2= [id:'FX-11', two:"two"] as Hashtable
assert map2 == java.util.Hashtable
assert map2.two == "two"
assert map2['two'] == "two"
Note: key may be string or not.
Array
a= [ 11, 12, 13, 14 ] as Object[]
List (by default create ArrayList)
def list = [5, 6, 7, 8]
assert list.get(2) == 7
assert list[2] == 7
assert list instanceof java.util.List
assert list instanceof java.util.ArrayList
def emptyList = []
assert emptyList.size() == 0
assert emptyList instanceof java.util.ArrayList
// To force to type other than ArrayList use
numbers3 = new LinkedList(['One', 'Two', 'Three', 'Four', 'Five'])
numbers4 = ['One', 'Two', 'Three', 'Four', 'Five'] as Stack // Groovy 1.6+
Note Groovy also has Range type. This is inherits from java.util.List, but is a pure groovy class (e.g. groovy.lang.IntRange)
2/ Java (8) syntax (Note these produce immutable items)
http://sellmic.com/blog/2011/07/08/7-new-cool-features-in-java-7/
http://blog.joda.org/2007/02/java-7-list-and-map-literals_6278.html
http://code.joejag.com/2009/new-language-features-in-java-7/
Map
Mapmap = {"key" : 1};
int value = map["key"];
Setset = {"item"};
Array
String[] array = {"item1", "item2",}
List
Listlist = ["Abba", "Beatles", "Corrs"];
ListemptyList = [];
3/ Javascript/ Json Syntax
Array
var emptyList = [];
var homogenousList = [1, 2, 3];
var heterogenousList = ["one", 2, 3.0];
Var list = new Array();
list[0] = "item";
list[1] = "item1";
List
Use Array
Map
var emptyMap = {};
var homogenousMap = {"one": 1, "two": 2, "three": 3};
var heterogenousMap = {"one": 1,
"two": "two",
"three": 3.0};
map = {'key':'value'}
map.key == 'value'
map['key'] == 'value'
No comments:
Post a Comment