http://blog.jooq.org/2015/09/09/how-to-use-java-8-functional-programming-to-generate-an-alphabetic-sequence/
JDK8
’m looking for a way of generating an alphabetic sequence:
A, B, C, ..., Z, AA, AB, AC, ..., ZZ.
private
static
String getString(
int
n) {
char
[] buf =
new
char
[(
int
) floor(log(
25
* (n +
1
)) / log(
26
))];
for
(
int
i = buf.length -
1
; i >=
0
; i--) {
n--;
buf[i] = (
char
) (
'A'
+ n %
26
);
n /=
26
;
}
return
new
String(buf);
}
JDK8
public
static
void
main(String[] args) {
int
max =
3
;
List<String> alphabet = Seq
.rangeClosed(
'A'
,
'Z'
)
.map(Object::toString)
.toList();
Seq.rangeClosed(
1
, max)
.flatMap(length ->
Seq.rangeClosed(
1
, length -
1
)
.foldLeft(Seq.seq(alphabet), (s, i) ->
s.crossJoin(Seq.seq(alphabet))
.map(t -> t.v1 + t.v2)))
.forEach(System.out::println);
}