将Int值转换为整数位数列表 [英] Converting Int value to List of Integer digits

查看:77
本文介绍了将Int值转换为整数位数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个容易的任务来绘制三个int值.然后从绘制的值的最高位数建立一个新的int值.有没有一种简单的方法可以将Int转换为Integer列表?我想通过流来解决此问题,但我需要一个从绘制值创建的整数列表来查找maxValue数字.

I have got an easy task to draw three int values. Then build a new int value from the highest digits from drawn values. Is there an easy way to convert an Int to List of Integers? I want to resolve this by streams but I need a List of Integers created from drawn value to find maxValue digit.

例如:123、256、189 = 369

For example: 123, 256, 189 = 369

我已经以一种幼稚的方式做到了这一点,我仍在学习并开始怀疑这是否是一种更简单,更有效的代码方式.

I have done this in a naive way, I'm still learning and start wondering if is an easier and more code efficient way.

public static List<Integer> convertingValue(int value) {
    List<Integer> intList = new ArrayList<>();
    char[] arr = String.valueOf(value).toCharArray();
    for (int i = 0; i < arr.length; i++) {
        intList.add(Character.getNumericValue(arr[i]));
    }
    return intList;
}

public static void main(String[] args) {
    IntSupplier is = () -> new Random().nextInt(999 - 100 + 1) + 100;
    List<Integer> figuresList =
            List.of(is.getAsInt(), is.getAsInt(), is.getAsInt());

    var list = figuresList
            .stream()
            .map(Basic02::convertingValue)
            .map(x -> x.stream().mapToInt(v -> v)
                    .max().orElseThrow(NoSuchElementException::new))
            .collect(Collectors.toList());

    System.out.println(list);
}

推荐答案

  1. 最好使用 Random :: ints(streamSize,origin,bound) 创建随机数流
  2. 将每个数字转换为字符串,然后使用 String :: chars 获取每个数字的字符流并找到每个数字的最大数字
  3. 为了更好地表示结果,值得在 StringBuilder <的基础上使用自定义收集器构建 String 而不是 List< Integer> /code>.然后可以根据需要将此字符串转换为 int/Integer .
  1. It would be better to use Random::ints(streamSize, origin, bound) to create a stream of random numbers
  2. Convert each number into string, then use String::chars to get stream of characters for each number and find maximal digit for each number
  3. To get better representation of the result it is worth to build a String instead of the List<Integer> using custom collector on the basis of StringBuilder. Then this string may be converted then into int/Integer if needed.

String num = new Random().ints(3, 100, 1000)
    .peek(System.out::println) // debug print
    .mapToObj(Integer::toString)
    .map(s -> s.chars().map(Character::getNumericValue).max().getAsInt())
    .collect(Collector.of(StringBuilder::new, StringBuilder::append, StringBuilder::append, StringBuilder::toString));
System.out.println("---\n" + num);

输出:

893
677
436
---
976


也可以将现有代码重构为立即返回最大数字而不是整数列表:


Existing code may also be refactored to return immediately the maximal digit instead of the list of integers:

public static int getMaxDigit(int value) {
    return String.valueOf(value)
        .chars() // IntStream
        .map(Character::getNumericValue)
        .max() // OptionalInt
        .getAsInt(); // int
}

然后将 List< Integer> 收集为:

var list = new Random().ints(3, 100, 1000) // IntStream
    .map(MyClass::getMaxDigit)
    .boxed()
    .collect(Collectors.toList());

String 可以用另一种方式收集:

Or String may be collected in another way:

var str = new Random().ints(3, 100, 1000) // IntStream
    .map(MyClass::getMaxDigit)
    .mapToObj(Integer::toString)
    .collect(Collectors.joining());

这篇关于将Int值转换为整数位数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆