forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseInteger3.java
More file actions
35 lines (23 loc) · 828 Bytes
/
ReverseInteger3.java
File metadata and controls
35 lines (23 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.zetcode;
// reversing an integer using streams
public class ReverseInteger3 {
public static void main(String[] args) {
int val = 7320300;
var str = String.valueOf(val);
// var val2 = str.chars().mapToObj(i -> (char) i)
// .reduce("", (s, c) -> c + s, (s1, s2) -> s2 + s1);
var val2 = str.chars().mapToObj(i -> (char) i).collect(StringBuilder::new,
(b, c) -> b.insert(0, (char) c), (b1, b2) -> b1.insert(0, b2));
// removing possible leading zeros
int i = 0;
while (val2.charAt(i) == '0') {
// does not work with deleteCharAt
// val2.deleteCharAt(0);
i++;
}
if (i > 0) {
val2.delete(0, i);
}
System.out.println(val2.toString());
}
}