Currently, I am learning the function of distinct() using in same lambda expression: The first one is used for finding the distinct digit of an integer
Integer intVar = 12341234;
long c1 = intVar.toString().chars().distinct().count(); // (1,2,3,4)
System.out.println(c1);//result = 4
Then i want to try find the same thing within a range, My thought is:
1.loop from 1 to 1_000_000
2.filter isSquareNumber && <= 9_999_999 && 1_000_000
3.convert to char
4.distinct
5.store as list(key,value)
6.findfirst key with value=7
Because the original code works but looks awful
int inte = IntStream.iterate(1, i-> i + 1)
.filter(i->isSquareNumber(i) && i<= 9999999 && i>=1000000)
.filter(i-> i%10!=i/10%10 && i%10!=i/100%10 && i%10!=i/1000%10 && i%10!=i/10000%10 && i%10!=i/100000%10 && i%10!=i/1000000%10)
.filter(i-> i/10%10!=i/100%10 && i/10%10!=i/1000%10 && i/10%10!=i/10000%10 && i/10%10!=i/100000%10 && i/10%10!=i/1000000%10)
.filter(i-> i/100%10!=i/1000%10 && i/100%10!=i/10000%10 && i/100%10!=i/100000%10 && i/100%10!=i/1000000%10)
.filter(i-> i/1000%10!=i/10000%10 && i/1000%10!=i/100000%10 && i/1000%10!=i/1000000%10)
.filter(i-> i/10000%10!=i/10%10 && i/10000%10!=i/100000%10)
.filter(i-> i/100000%10!=i/1000000%10)
.findFirst()
.getAsInt();
System.out.println(inte);//result = 1034289
WJS helped version
IntStream
.iterate(1, i-> i + 1)
.filter(i->isSquareNumber(i) && i<= 9999999 && i>=1000000)
.mapToObj(i->new String[] {Integer.toString(i), Arrays
.stream(Integer.toString(i).split(""))
.filter(a -> a[1].length() >= 7)
.findFirst()
.ifPresent(a -> System.out
.println(a[1] + " --> " + a[1].length()));
It works wonderfully. And now I am trying to move one more step that creates a method in which the return type is an integer and returns the most distinct digit integer, but not a boolean.
Try this. I limited it to the first 10 values.
IntStream.range(100000, 1000000).limit(10)
.mapToObj(i->new String[] {Integer.toString(i), Arrays
.stream(Integer.toString(i).split(""))
.distinct().collect(Collectors.joining(""))})
.forEach(a->System.out.println(a[0] + " --> " + a[1]));
prints
100000 --> 10
100001 --> 10
100002 --> 102
100003 --> 103
100004 --> 104
100005 --> 105
100006 --> 106
100007 --> 107
100008 --> 108
100009 --> 109
Find the first number that has 7 distinct digits.
After creating the arrays in the previous example, replace the forEach with a filter, followed by a findFirst, and then print the value if present.
IntStream.range(1000000, 10000000)
.mapToObj(i -> new String[] { Integer.toString(i),
Arrays.stream(Integer.toString(i).split(""))
.distinct()
.collect(Collectors.joining("")) })
.filter(a -> a[1].length() >= 7).findFirst()
.ifPresent(a -> System.out
.println(a[1] + " --> " + a[1].length()));
Prints
1023456 --> 7
To find all numbers that are squares that have at least 7 distinct digits:
int[] all = IntStream.range(1000, (int) Math.sqrt(10_000_000))
.map(i -> i * i)
.filter(i -> Integer.toString(i).chars().distinct().count() > 6)
.toArray();
To find just the first one (or explode trying, but there are 123):
int first = IntStream.range(1000, (int) Math.sqrt(10_000_000))
.map(i -> i * i)
.filter(i -> Integer.toString(i).chars().distinct().count() > 6)
.findFirst()
.orElseThrow(RuntimeException::new);
Rather than iterating over every number, I am iterating on the square root then squaring it, so it much more efficient.