====== Groovy List / Collection ====== * [[http://groovy.codehaus.org/Collections|Groovy Collections]] * [[http://groovy.codehaus.org/JN1015-Collections|Groovy List and Sets]] * [[http://groovy.codehaus.org/groovy-jdk/java/util/List.html|List GDK]] * [[http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html|Collection GDK]] * [[http://examples.javacodegeeks.com/core-java/groovy-array-example/|Groovy Array Example | Examples Java Code Geeks]] * 문자열도 문자의 리스트로 간주할 수 있음. ===== 기본값 줘서 리스트 생성 ===== * http://mrhaki.blogspot.kr/2012/10/groovy-goodness-create-list-with.html lazy = ['abc', 42].withDefault { 'default' } // Or .withLazyDefault {} 접근전에는 null, 접근시에는 'default' eager = ['abc', 42].withEagerDefault { 'default' } // 접근전부터 'default' sample = [1,2,3].withDefault { index -> // index를 이용해 값 넣기 index % 2 } ===== Iterable의 처음/마지막 값 ===== * http://mrhaki.blogspot.kr/2012/10/groovy-goodness-getting-first-and-last.html list = 0..100 assert list.first() == 0 assert list.last() == 100 ===== sublist ===== list = 0..100 list[0,5] // 0~5 list[1..-1] // 1~100까지 list[1..5,10,20] // 0, 1, 2, 3, 4, 5, 10, 20 ===== each ===== [1, 2, 3].each{ println "Item: $it" } ['a', 'b', 'c'].eachWithIndex{ it, i -> println "$i: $it" } // index 를 가지고 each 돌기. index는 0 부터 시작 // Range는 괄호로 감싸서 each 해야 한다. (0..100).each { println it } ===== 총합과 평균 ===== list = 1..100 // 5050 println list.sum() // 50.5 println list.sum() / list.size() ===== 기타 ===== * 섞기 Collections.shuffle(list, new Random()) * 중복 값 제거 assert [3,5,5,5,2].unique() == [3,5,2] * unmodifiable ['Groovy', 'Gradle', 'Asciidoctor', 'Micronaut'].asUnmodifiable()