 # rootulp's solution

## to Strain in the Java Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Implement the `keep` and `discard` operation on collections. Given a collection and a predicate on the collection's elements, `keep` returns a new collection containing those elements where the predicate is true, while `discard` returns a new collection containing those elements where the predicate is false.

For example, given the collection of numbers:

• 1, 2, 3, 4, 5

And the predicate:

• is the number even?

Then your keep operation should produce:

• 2, 4

• 1, 3, 5

Note that the union of keep and discard is all the elements.

The functions may be called `keep` and `discard`, or they may need different names in order to not clash with existing functions or concepts in your language.

## Restrictions

Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.

# Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

## Source

Conversation with James Edward Gray II https://twitter.com/jeg2

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### StrainTest.java

``````import org.junit.Assert;
import org.junit.Test;
import org.junit.Ignore;

import java.util.Arrays;
import java.util.List;

public class StrainTest {

@Test
public void emptyKeep() {
Assert.assertEquals(expectedOutput, Strain.keep(input, x -> x < 10));
}

@Ignore("Remove to run test")
@Test
public void keepEverything() {
List<Integer> input = Arrays.asList(1, 2, 3);
List<Integer> expectedOutput = Arrays.asList(1, 2, 3);
Assert.assertEquals(expectedOutput, Strain.keep(input, x -> x < 10));
}

@Ignore("Remove to run test")
@Test
public void keepFirstAndLast() {
List<Integer> input = Arrays.asList(1, 2, 3);
List<Integer> expectedOutput = Arrays.asList(1, 3);
Assert.assertEquals(expectedOutput, Strain.keep(input, x -> x % 2 != 0));
}

@Ignore("Remove to run test")
@Test
public void keepNeitherFirstNorLast() {
List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> expectedOutput = Arrays.asList(2, 4);
Assert.assertEquals(expectedOutput, Strain.keep(input, x -> x % 2 == 0));
}

@Ignore("Remove to run test")
@Test
public void KeepStrings() {
List<String> words = Arrays
.asList("apple zebra banana zombies cherimoya zelot".split(" "));
List<String> expectedOutput = Arrays.asList("zebra", "zombies", "zelot");
Assert.assertEquals(expectedOutput,
Strain.keep(words, x -> x.startsWith("z")));
}

@Ignore("Remove to run test")
@Test
public void KeepArrays() {
List<List<Integer>> actual = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList(5, 5, 5),
Arrays.asList(5, 1, 2),
Arrays.asList(2, 1, 2),
Arrays.asList(1, 5, 2),
Arrays.asList(2, 2, 1),
Arrays.asList(1, 2, 5));
List<List<Integer>> expectedOutput = Arrays.asList(
Arrays.asList(5, 5, 5),
Arrays.asList(5, 1, 2),
Arrays.asList(1, 5, 2),
Arrays.asList(1, 2, 5));
Assert.assertEquals(expectedOutput,
Strain.keep(actual, col -> col.contains(5)));
}

@Ignore("Remove to run test")
@Test
Assert.assertEquals(expectedOutput, Strain.discard(input, x -> x < 10));
}

@Ignore("Remove to run test")
@Test
List<Integer> input = Arrays.asList(1, 2, 3);
List<Integer> expectedOutput = Arrays.asList(1, 2, 3);
Assert.assertEquals(expectedOutput, Strain.discard(input, x -> x > 10));
}

@Ignore("Remove to run test")
@Test
List<Integer> input = Arrays.asList(1, 2, 3);
List<Integer> expectedOutput = Arrays.asList(2);
Assert.assertEquals(expectedOutput, Strain.discard(input, x -> x % 2 != 0));

}

@Ignore("Remove to run test")
@Test
List<Integer> input = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> expectedOutput = Arrays.asList(1, 3, 5);
Assert.assertEquals(expectedOutput, Strain.discard(input, x -> x % 2 == 0));
}

@Ignore("Remove to run test")
@Test
List<String> words = Arrays
.asList("apple zebra banana zombies cherimoya zelot".split(" "));
List<String> expectedOutput = Arrays.asList("apple", "banana", "cherimoya");
Assert.assertEquals(expectedOutput,
}

@Ignore("Remove to run test")
@Test
List<List<Integer>> actual = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList(5, 5, 5),
Arrays.asList(5, 1, 2),
Arrays.asList(2, 1, 2),
Arrays.asList(1, 5, 2),
Arrays.asList(2, 2, 1),
Arrays.asList(1, 2, 5));
List<List<Integer>> expectedOutput = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList(2, 1, 2),
Arrays.asList(2, 2, 1));
Assert.assertEquals(expectedOutput,
}
}``````
``````import java.util.List;
import java.util.ArrayList;
import java.util.function.Predicate;

public class Strain {

public static <T> List<T> keep(List<T> list, Predicate<T> p) {
List<T> results = new ArrayList<>();

for (T item : list) {
if (p.test(item)) {
}
}

return results;
}

public static <T> List<T> discard(List<T> list, Predicate<T> p) {
List<T> results = new ArrayList<>();

for (T item : list) {
if (!p.test(item)) {
}
}

return results;
}

}``````