/EnumerateJava

A Java construct allowing iteration over a collection similar to the enumerate() method in Python, providing access to each element and it's index.

Primary LanguageJavaMIT LicenseMIT

Enumerate for Java

The Enumerate java class provides a generic method to iterate over an iterable collection, providing access to each element and it's index within the collection. It allows enumeration over a collection similar to the enumerate() function in Python.

Usage

The following pseudo control structure is introduced to enumerate over a collection with minimal complexity when compared to a regular for-each loop:

Enumerate.over(iterable, (item, i) -> { 
  ... 
});

The above example uses a lambda expession to define the task to be performed each iteration. This also allows for the item and i parameters to be renamed as required.

This can also be used to iterate over an array as shown in the below example:

String[] arr = ["Zero", "One"]

Enumerate.over(arr, (str, i) -> { 
  System.out.println(i+": "str);
});

This would produce the following output:

0: Zero
1: One

It may also be written using the full interface to demonstrate how this construct works:

List<String> list = new ArrayList<>();
list.add("Zero");
list.add("One");

Enumerate.over(list, new Iteration<String>() {
  @Override
  public void accept(String item, int i) {
    System.out.println(i+": "item);
  }
});

Installation

Enumerate for Java is available via the Jitpack.io repository. You can find instructions to add it as a dependency to your project here.

Alternatively the jar files for the latest release can be downloaded here.