有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

为一个Atribut java设置多个值以筛选附加列表

我使用的是java8,我想为添加到列表中设置很多值,我编写了这些代码,并且工作正常,但我认为它们更好

private String value;
public List<Entity> finalList;
public List<Entity> getList() {

        value= "AB";
        List<Entity> list2;
        List<Entity> list3;

        finalList= EntityService.filtermethod(value);
        list2= EntityService.filtermethod("BC");
        for (Entity val : list2) {
            finalList.add(val);

        }
        list3= EntityService.filtermethod("CD");
        for (Reservation val : list3) {
            finalList.add(reser);

        }

        return finalList;
    }

谢谢大家


共 (1) 个答案

  1. # 1 楼答案

    I make this code and it worked, but I think they are things much better.

    对!!有数以千计的方法使代码更好,但请考虑学习基本的东西,

    • 学习使用variablesaccess-modifiers
    • 如果{{CD1}}不是NULL安全,请考虑在添加NULL之前检查NULL(否则您需要面对^ {A3}),其中一个很好的方法是使用Optional

    我缩小了你的方法getList()

    public List<Entity> getList() {
        return Stream.of(
                EntityService.filtermethod("AB"),
                EntityService.filtermethod("BC"),
                EntityService.filtermethod("CD")
            )
            .flatMap(Collection::stream)
            .collect(Collectors.toList());
    }