Working with Collections in Java, Groovy, and C#

Different programming languages have their own ways of performing common operations on lists, such as transforming each element in the list or finding a particular element. To help when switching back and forth between languages, I wanted to create a quick guide to show the syntax of these operations in various languages.

Transform list items

In this example, we’ll transform each number in a list by doubling it.

Java

List<Integer> numbers = Arrays.asList(1, 2, 3);

List<Integer> doubledNumbers = numbers.stream().map(num -> num * 2).collect(Collectors.toList());

assertEquals(Arrays.asList(2, 4, 6), doubledNumbers);

Groovy

List<Integer> numbers = [1, 2, 3]

List<Integer> doubledNumbers = numbers.collect { num -> num * 2 }

assert doubledNumbers == [2, 4, 6]

C#

var numbers = new List<int> { 1, 2, 3 };

var doubledNumbers = numbers.Select(num => num * 2).ToList();

CollectionAssert.AreEqual(new List<int> { 2, 4, 6 }, doubledNumbers);

Filtering list items

For this example, we’ll find all the even numbers in a list.

Java

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

List<Integer> evenNumbers = numbers.stream().filter(num -> num % 2 == 0).collect(Collectors.toList());

assertEquals(Arrays.asList(2, 4), evenNumbers);

Groovy

List<Integer> numbers = [1, 2, 3, 4, 5]

List<Integer> evenNumbers = numbers.findAll { num -> num % 2 == 0 }

assert evenNumbers == [2, 4]

C#

var numbers = new List<int> { 1, 2, 3, 4, 5 };

var evenNumbers = numbers.Where(num => num % 2 == 0).ToList();

CollectionAssert.AreEqual(new List<int> { 2, 4 }, evenNumbers);

Find single item in list

Next we’ll find the first item in the list that matches our criteria, which in this case is the first even number.

Java

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

Integer firstEvenNumber = numbers.stream().filter(num -> num % 2 == 0).findFirst().get();

assertEquals(2, firstEvenNumber.intValue());

Groovy

List<Integer> numbers = [1, 2, 3, 4, 5]

Integer firstEvenNumber = numbers.find { num -> num % 2 == 0 }

assert firstEvenNumber == 2

C#

var numbers = new List<int> { 1, 2, 3, 4, 5 };

var firstEvenNumber = numbers.Find(num => num % 2 == 0);

Assert.AreEqual(2, firstEvenNumber);

Sorting a list

Now we’ll sort a list of numbers from smallest to largest. One thing to note, there are multiple ways to sort collections in each of the languages, I just chose one way for these examples.

Java

List<Integer> numbers = Arrays.asList(5, 1, 4, 2, 3);

List<Integer> sortedNumbers = numbers.stream().sorted().collect(Collectors.toList());

assertEquals(Arrays.asList(1, 2, 3, 4, 5), sortedNumbers);

Groovy

List<Integer> numbers = [2, 5, 3, 1, 4]

List<Integer> sortedNumbers = numbers.toSorted()

assert sortedNumbers == [1, 2, 3, 4, 5]

C#

var numbers = new List<int> { 2, 5, 3, 1, 4 };

numbers.Sort();

CollectionAssert.AreEqual(new List<int> { 1, 2, 3, 4, 5 }, numbers);

Sorting by field

The above sorts are on simple numbers, what if we want to sort on a specific field of an object? In this example we’ll sort a list of people by their age.

Java

Person youngest = new Person(30);
Person middle = new Person(45);
Person oldest = new Person(60);

List<Person> people = Arrays.asList(middle, oldest, youngest);

List<Person> sortedPeople = people.stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());

assertEquals(30, sortedPeople.get(0).getAge().intValue());
assertEquals(45, sortedPeople.get(1).getAge().intValue());
assertEquals(60, sortedPeople.get(2).getAge().intValue());

Groovy

Person youngest = new Person(30)
Person middle = new Person(45)
Person oldest = new Person(60)

List<Person> people = [middle, oldest, youngest]

List<Person> sortedPeople = people.toSorted { person -> person.getAge() }

assert sortedPeople[0].getAge() == 30
assert sortedPeople[1].getAge() == 45
assert sortedPeople[2].getAge() == 60

C#

Person youngest = new Person(30);
Person middle = new Person(45);
Person oldest = new Person(60);

var people = new List<Person> { middle, oldest, youngest };

var sortedPeople = people.OrderBy(p => p.Age).ToList();

Assert.AreEqual(30, sortedPeople[0].Age);
Assert.AreEqual(45, sortedPeople[1].Age);
Assert.AreEqual(60, sortedPeople[2].Age);

With only some differences in syntax, each language has many of the same capabilities when working with collections. Hopefully these examples help you when switching back and forth between these languages.