I need a function which returns a set containing {XXX11, XXX12, XXX13, XXX14, XXX21, ... , XXX43, XXX44}
where XXX
is a integer argument of the function. What is a elegant way of Java 8 style? I've done in case of prefix is one digit as following:
/**
* I need to get {<base>11, <base>12, ..., <base>44} but can't...
* @return {<base>1, <base>2, <base>3, <base>4}
*/
Set<Integer> addOnlyOnePrefix(int base){
return IntStream.range(1, 4).map(prefix -> base*10 + prefix)
.boxed().collect(Collectors.toSet());
}
The simplest way is probably to just generate 16 values:
Set<Integer> addOnlyTwoPrefixes(int base) {
return IntStream.range(0, 16)
.map(prefix ->
base * 100 + // Leading digits
10 + 10 * (prefix / 4) + // Penultimate digit
1 + (prefix % 4)) // Last digit
.boxed()
.collect(Collectors.toSet());
}
The 10+ and 1+ are to take account of prefix / 4
and prefix % 4
being in the range 0-3 rather than 1-4.
See more on this question at Stackoverflow