从对象获取列表,修改并设置为一行

发布于 2025-01-28 14:30:31 字数 383 浏览 3 评论 0 原文

我需要从对象中获取 list<示例> ,向其添加一个元素,然后将修改后的列表附加到对象。在一行中,有什么明智的方法?现在看起来如下:

List<Example> examples = user.getExamples();
examples.add(example);
user.setExamples(examples);

我想到了这样的SMTH:

user.setExamples(user.getExamples().add(example));

但是由于那不起作用 添加返回<代码> true

I need to get List<Example> from the object, add an element to it, and attach the modified list to the object. Is there a wise way to do it in one line? Right now it looks like the following:

List<Example> examples = user.getExamples();
examples.add(example);
user.setExamples(examples);

I thought about smth like this:

user.setExamples(user.getExamples().add(example));

but it does not work due to that
add returns true

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久伴你 2025-02-04 14:30:31

面向对象的编程,您应该考虑一个要求对象的“做”它的事情“而不是您试图从外部操纵其内部。

因此,不要提取,操纵和重新注射,而只要求该对象添加一个项目。您甚至都不知道该对象内部的列表是否是。让对象照料物品可以看到合适。

user.addExample( myExample ) ;

换句话说,拥有和固定器通常是面向对象的设计差的标志。

如果您坚持外部修改:

  • 如果在某些情况下知道getter返回的列表是(a)对象中维护的相同列表(通常是一个差的设计),并且是(b)可修改,则可以使用在中看到的方法 patil 回答您在哪里称呼 list#add add add 返回的对象通过getter。如果测试以验证您返回 true ,我将添加,因为 false 表示添加失败。
  • 如果您想将修改后的列表传递给设置器方法,那么如您所见,由于方法返回boolean,因此不能在一行中使用。对于a fluent-style style语法本身。

要使用设置器,您必须进行多个语句。

List< Example > list = user.getExamples() ;
if( list.add( myExample ) ) 
{
    user.setExamples( list ) ;
} else 
{
    … handle failure …
}

如果该返回的列表不可修改,则需要制作一个新列表。将无法解码的列表传递给构造函数。

List< Example > list = new ArrayList<>( user.getExamples() ) ;
if( list.add( myExample ) ) 
{
    user.setExamples( list ) ;
} else 
{
    … handle failure …
}

In object-oriented programming, you should think in terms of asking an object to "do its thing" rather than you trying to manipulate its innards from outside.

So rather than extract, manipulate, and re-inject, simply ask that object to add an item. You don't even know if there is a list inside that object. Let the object take care of tracking items as it sees fit.

user.addExample( myExample ) ;

In other words, having getters and setters is often a sign of poor object-oriented design.

If you insist on external modifications:

  • If you know for certain the list returned by the getter is (a) the same list maintained within the object (generally a poor design) and is (b) modifiable, then you can use the approach seen in the Answer by Patil where you call List#add on the object returned by the getter. I would add an if test to verify you got back true, as a false means the addition failed.
  • If you want to pass the modified list to a setter method, then as you have seen, you cannot do so in a single line because the method return boolean. For a fluent-style syntax, you would need an addition method that returned a reference to the list itself.

To use the setter, you must make multiple statements.

List< Example > list = user.getExamples() ;
if( list.add( myExample ) ) 
{
    user.setExamples( list ) ;
} else 
{
    … handle failure …
}

If that returned list is not modifiable, you'll need to make a new one. Pass the unmodifiable list to the constructor.

List< Example > list = new ArrayList<>( user.getExamples() ) ;
if( list.add( myExample ) ) 
{
    user.setExamples( list ) ;
} else 
{
    … handle failure …
}
非要怀念 2025-02-04 14:30:31

如果您真的想在一行中使用它,那么这是一种可能适合您要求的方法。:

 user.getExamples().add(example);

If you really want to use it in one line, then this is one of approach which might suits your requirement.:

 user.getExamples().add(example);
怪异←思 2025-02-04 14:30:31

如果您想加入一个元素(或特别是列表),并将串联集合返回一行,则可以执行以下操作:

Stream.concat([Collection].stream(), Stream.of([element to add])).toList()

或在您的具体情况下:

user.setExamples(Stream.concat(user.getExamples().stream(), Stream.of(example)).toList());

请注意,这可以使用 .tolist() 流中在Java 16中介绍的方法。对于早期版本,您可以使用 .collect(collectors.tolist())

这可能不是最干净的方法(当然比集合上的简单 .add()更详细),但是对于您想在一行中声明串联集合的情况,这可能是很好的,与其将其初始化为一个(也许具有误导性变量名称),而是将其添加到另一个。

希望这有帮助!

If you're looking to join a Collection (or specifically a List) with a single element and have the concatenated Collection returned in one line, you could do the following:

Stream.concat([Collection].stream(), Stream.of([element to add])).toList()

Or in your specific case:

user.setExamples(Stream.concat(user.getExamples().stream(), Stream.of(example)).toList());

Note that this makes use of the .toList() method on Stream introduced in Java 16. For earlier versions you can use .collect(Collectors.toList()).

This may not be the cleanest approach (it's certainly more verbose than a simple .add() on a Collection), but it can be nice for situations where you want to declare the concatenated collection in one line, rather than initializing it in one (with perhaps a misleading variable name), and adding to it in another.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文