java 如果列表为空,使用 lombok getter 初始化列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38522092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:28:35  来源:igfitidea点击:

Initialize list if list is null with lombok getter?

javajava-8lombok

提问by L42

I am currently replacing all my standard POJO's to use Lombok for all the boilerplate code. I find myself keeping getters for lists because I want to return an empty list if the list has not been initialized. That is, I don't want the getter to return null. If there some lombok magic that I'm not aware of that can help me avoid doing this?

我目前正在替换我所有的标准 POJO,以将 Lombok 用于所有样板代码。我发现自己为列表保留了 getter,因为如果列表尚未初始化,我想返回一个空列表。也就是说,我不希望 getter 返回 null。如果有一些我不知道的龙目岛魔法可以帮助我避免这样做吗?

Example of generated code

生成的代码示例

private List<Object> list;
public Object getList(){ return list; }

What I would like instead:

我想要的是:

private List<Object> list;
public Object getList(){
    if (list == null) {
        return new ArrayList();
    }
    return list;
}

采纳答案by stacker

You can achieve this by declaring and initializing the fields. The initialization will be done when the enclosing object is initialized.

您可以通过声明和初始化字段来实现这一点。初始化将在初始化封闭对象时完成。

private List<Object> list = new ArrayList();

Lomboks @Getterannotation provides an attribute lazywhich allows lazy initialization.

Lomboks@Getter注释提供了一个lazy允许延迟初始化的属性。

 @Getter(lazy=true) private final double[] cached = expensiveInitMethod();

Documentation

文档

回答by ACloudRoamer

I had the same questions as of this one. Though, the above answers are useful in some ways, the exact solution is to use @Builderand @Singularannotations of Lombok API like below given code.

我和这个有同样的问题。虽然,上述的答案是在某些方面是有用的,准确的解决方案是使用@Builder@Singular类似如下的代码龙目岛API的注解。

It worked superb for me.

它对我来说效果很好。

@Builder
class MyClass{
    @Singular
    private List<Type> myList;
}

This will initialize myList with a non-null List object. Though, this questions is an old one. But, still posting this answer to help someone like me who will refer this question in future.

这将用一个非空的 List 对象初始化 myList。虽然,这个问题是一个古老的问题。但是,仍然发布此答案以帮助像我这样将来会提及此问题的人。

回答by Jude Niroshan

That is, I don't want the getter to return null. If there some lombok magic that I'm not aware of that can help me avoid doing this?

也就是说,我不希望 getter 返回 null。如果有一些我不知道的龙目岛魔法可以帮助我避免这样做吗?

You don't need any magic to be happen. Just initialize the list.

你不需要任何魔法就可以发生。只需初始化list.

回答by tkruse

You can override the getter with anything you like by using AccessLevel.NONE on the field.

您可以通过在字段上使用 AccessLevel.NONE 用您喜欢的任何内容覆盖 getter。

Note that merely Initializing the field does not protect you from clients calling a constructor with nulls, or calling a setter with null (Still may be okay, depending on what you want).

请注意,仅初始化该字段并不能保护您免受客户端调用具有空值的构造函数或调用具有空值的 setter 的影响(仍然可以,这取决于您想要什么)。

E.g.

例如

@Getter(AccessLevel.NONE)
private Map<String, String> params;

public Map<String, String> getParams() {
    return (params == null) ? new HashMap<>() : params;
}