Created
June 23, 2017 10:26
-
-
Save nanomad/bd2c06cb80bb9b1ae7e97a6addeb427f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.test; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
public class FixMe { | |
public static void main(String[] args) { | |
Integer[] integers = new Integer[]{1, 2, 2, 4, 5, -1, -2, 23, 42, -1}; | |
final Collection<Integer> result = duplicateSome(new ArrayList<>(Arrays.asList(integers)), 2); | |
System.out.println(result); | |
final Collection<Integer> result2 = duplicateSome(new ArrayList<>(Arrays.asList(integers)), null); | |
System.out.println(result2); | |
} | |
/** | |
* Returns a new Collection of Integers where all the numbers that are multiple of test are duplicated | |
* @param numbers A list of Integers | |
* @param test The MCD. null means "do nothing" | |
* @return A new {@link Collection} that satisfies the requirements | |
*/ | |
private static Collection<Integer> duplicateSome(Collection<Integer> numbers, Integer test) { | |
for (Integer number : numbers) { | |
if (matches(number, test)) { | |
numbers.add(number); | |
} | |
} | |
return numbers; | |
} | |
private static boolean matches(Integer number, Integer test) { | |
return number % test == 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment