Ad
Code
Diff
  • import java.util.*;
    import java.util.stream.Collectors;
    
    public class MaxNumber {
        public static long print(long number) {
          return Long.parseLong(
            String.valueOf(number)
                .chars()
                .mapToObj(ch -> String.valueOf((char) ch))
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.joining())
        );
      }
    }
    • import java.util.*;
    • import java.util.stream.Collectors;
    • public class MaxNumber {
    • public static long print(long number) {
    • return Long.parseLong(
    • String.valueOf(number)
    • .chars()
    • .mapToObj(ch -> String.valueOf(Character.getNumericValue(ch)))
    • .mapToObj(ch -> String.valueOf((char) ch))
    • .sorted(Comparator.reverseOrder())
    • .collect(Collectors.joining())
    • );
    • }
    • }
Code
Diff
  • import java.util.Arrays;
    
    public class MaxNumber {
        public static long print(long number) {
          String numeroString = Long.toString(number);
          StringBuilder numeroOrdenadoStr = new StringBuilder();
          
          numeroString.chars()
                      .mapToObj(c -> (char) c)
                      .sorted()
                      .forEach(c -> numeroOrdenadoStr.append(c));
          
          String result = numeroOrdenadoStr.reverse().toString();
          return Long.parseLong(result);
        }
    }
    • import java.util.Arrays;
    • import java.util.Scanner;
    • public class MaxNumber {
    • public static long print(long number) {
    • String numeroString = Long.toString(number);
    • StringBuilder numeroOrdenadoStr = new StringBuilder();
    • char[] digitos = numeroString.toCharArray();
    • Arrays.sort(digitos);
    • numeroString.chars()
    • .mapToObj(c -> (char) c)
    • .sorted()
    • .forEach(c -> numeroOrdenadoStr.append(c));
    • StringBuilder numeroOrdenadoStr = new StringBuilder(new String(digitos));
    • numeroOrdenadoStr.reverse();
    • return Long.parseLong(numeroOrdenadoStr.toString());
    • String result = numeroOrdenadoStr.reverse().toString();
    • return Long.parseLong(result);
    • }
    • public static void main(String[] args) {
    • Scanner scanner = new Scanner(System.in);
    • System.out.print("Ingrese un numero: ");
    • long number = scanner.nextLong();
    • long maxNumber = print(number);
    • System.out.println("El número más alto que se puede formar con estos dígitos es: " + maxNumber);
    • }
    • }