简体   繁体   English

如何 Java Map Stream 多个条件从 int Array 到 String Array

[英]How to Java Map Stream multiple Condition from int Array to String Array

Hi guys can u help me i have a problem here,嗨,你们能帮我吗,我这里有问题,

i have list book:我有清单书:

1.Math Rp.15.000 1.数学 Rp.15.000

2.Sains Rp.18.000 2.Sains Rp.18.000

3.Geologi Rp.16.000 3.地质学 Rp.16.000

4.Biologi Rp.12.000 4.Biologi Rp.12.000

The user enters the book number he chooses, then he got the array用户输入他选择的书号,然后他得到了数组

int[] arrayNumberBookTake = {1,2,4};

Then I want to change the value of the array to a new value and it depends on the price in the list然后我想将数组的值更改为新值,这取决于列表中的价格

int[] listBooktoPrice= Arrays.stream(arrayNumberBookTake).map(i -> i == 1 ? 15000 : i == 2 ? 18000 : i == 3 ? 16000 : i == 4 ? 12000 : i).toArray();

it working and the output of array listBooktoPrice:它工作和数组listBooktoPrice的output:

[15000, 18000, 12000]

now i want to make a new array it convert from arrayNumberBookTake to an String现在我想创建一个新数组,将它从 arrayNumberBookTake 转换为 String

String[] nameBookTake = Arrays.stream(arrayNumberBookTake).map(i -> i == 1 ? "Math" : i == 2 ? "Sains " : i == 3 ? "Geologi" : i == 4 ? "Biologi" : i).toArray();

but that code not work, i want array nameBookTake like this:但该代码不起作用,我想要这样的数组 nameBookTake:

{"Math", "Sains", "Geologi"}

array nameBookTake should depends from the array arrayNumberBookTake and match with list book on top.数组 nameBookTake 应该取决于数组 arrayNumberBookTake 并与顶部的列表书匹配。

I hope u guys understand what i means, thx u guys:)我希望你们明白我的意思,谢谢你们:)

Your last conditional ends with the parameter i .您的最后一个条件以参数i结束。 This is an int , not a String .这是一个int ,而不是一个String Therefor the array is not uniform;因此阵列不均匀; adding ints to an Array of String is not permitted.不允许将整数添加到String Array

Solution: delete the last conditional surrounding "Biologi" .解决方案:删除"Biologi"周围的最后一个条件。

Furthermore, Java streams differ between primitives (like int ) and objects (like String ).此外,Java 流在原语(如int )和对象(如String )之间有所不同。 To convert the IntStream into the Stream of String , one can use mapToObj , and collect with passing the constructor of the String[] to toArray() .要将IntStream转换为StringStream ,可以使用mapToObj ,并通过将String[]的构造函数传递给toArray()来收集。

like so:像这样:

        String[] nameBookTake = Arrays.stream(arrayNumberBookTake)
                .mapToObj(i -> i == 1 ? "Math" : i == 2 ? "Sains " : i == 3 ? "Geologi" : "Biologi" )
                .toArray(String[]::new);

This answers your question.这回答了你的问题。

However: as Andy Turner pointed out, using conditionals in this manner is a bad practice.但是:正如 Andy Turner 指出的那样,以这种方式使用条件是一种不好的做法。 Better practice is to use a switch, like so:更好的做法是使用开关,如下所示:

        String[] nameBookTake = Arrays.stream(arrayNumberBookTake)
                .mapToObj( i -> switch( i )
                        {
                            case 1 -> "Math";
                            case 2 -> "Sains";
                            case 3 -> "Geologi";
                            case 4 -> "Biologi";
                            default -> "Unknown book!";
                        } )
                .toArray(String[]::new);

Better still is to bundle the properties of a Book into its separate class, in this case an Enum , yielding a complete solution:更好的办法是将 Book 的属性捆绑到其单独的 class 中,在本例中为Enum ,从而产生完整的解决方案:

@Log4j2
public class Main
{
    public static void main( String[] args )
    {
        int[] arrayNumberBookTake = {1,2,4};
        int[] listBooktoPrice= Arrays.stream(arrayNumberBookTake)
                .map( i -> Books.values()[ i ].price )
                .toArray();

        String[] nameBookTake = Arrays.stream(arrayNumberBookTake)
                .mapToObj( i -> Books.values()[ i ].name  )
                .toArray(String[]::new);
    }
}

enum Books
{
    Math("Math", 15_000 ),
    Sains("Sains", 18_000 ),
    Geologi( "Geologi", 16_000 ),
    Biologi( "Biologi", 12_000 );

    String name;
    int price;

    Books( String name, int price )
    {
        this.name = name;
        this.price = price;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM