Over a million developers have joined DZone.

Groovy Goodness: Combine Elements Iterable with Index

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

Since Groovy 2.4.0 we can get the indices from the elements in a collection with the indices method. In addition to this method we can also use the withIndex to combine an Iterable with the indices directly. The output is a List of tuples where the first item is the value of the Iterable and the second the index value. We can pass an optional argument to the withIndex which is the starting point for the index values.
Another alternative is the indexed method. The indexed method returns a Map, where the key of the entry is the index value and the entry value is theIterable value.

In the following example we use the withIndex method. The sample of the alphabet is the same as in the blog post about indices, but rewritten with thewithIndex method:

def list = [3, 20, 10, 2, 1]
assert list.withIndex() == [[3, 0], [20, 1], [10, 2], [2, 3], [1, 4]]


def alphabet = 'a'..'z'

// Combine letters in alphabet
// with position and start at 1.
def alphabetIndices = alphabet.withIndex(1)

// alphabetIndices = [['a', 1], ['b', 2], ...]
assert alphabetIndices[0..2] == [['a', 1], ['b', 2], ['c', 3]]

// Find position of each letter
// from 'groovy' in alphabet.
def positionInAlphabet = 'groovy'.inject([]) { result, value ->
    result << alphabetIndices.find { it[0] == value }[1]
    result
}

assert positionInAlphabet == [7, 18, 15, 15, 22, 25]

In the next example we use the indexed method:

def list = [3, 20, 10, 2, 1]
assert list.indexed() == [0: 3, 1: 20, 2: 10, 3: 2, 4: 1]


def alphabet = 'a'..'z'

// Combine letters in alphabet
// with position and start at 1.
def alphabetIndices = alphabet.indexed(1)

// alphabetIndices = [1: 'a', 2: 'b', ...]
assert alphabetIndices.findAll { key, value -> key < 4} == [1: 'a', 2: 'b', 3: 'c']

// Find position of each letter
// from 'groovy' in alphabet.
def positionInAlphabet = 'groovy'.inject([]) { result, value ->
    result << alphabetIndices.find { it.value == value }.key
    result
}

assert positionInAlphabet == [7, 18, 15, 15, 22, 25]

Written with Groovy 2.4.1.



The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}