The following code calculates the biggest number of consecutive ones in a binary representation of a number. For example 6 -> 2 (110), 5 -> 1 (101):
String numberAsBinaryString = Integer.toBinaryString(number); List<String> listOfConsecutiveOneStrings = Arrays.asList(numberAsBinaryString.split("0")); System.out.println(listOfConsecutiveOneStrings.stream() .max(Comparator.comparing(String::length)) .map(onesSequence -> onesSequence.length()) .get());
I wonder if there is a more elegant way of writing this especially since this solution involves quite a few conversions. Any opinions are appreciated.