Java 8 - 使用 Stream API 使用相同对象填充数组列表的正确方法是什么? [英] Java 8 - what is the correct way to fill an arraylist with the same object using Stream API?

查看:102
本文介绍了Java 8 - 使用 Stream API 使用相同对象填充数组列表的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Stream API 的新手,我正在寻找更优雅和最短的方法来使用与此代码等效的 Stream API 使用相同的对象填充 ArrayList:

I am new with Stream API and I am looking for more elegant and shortest way to fill an ArrayList with the same object using Stream API equivalent to this code:

SomeObject someObject = new SomeObject();
List<SomeObject> someObjectList = new ArrayList<>();

int maxLimitValue = 70; //for example
for(int i=0; i<=maxLimitValue; i++){
   someObjectList.add(someObject);
}

对于我当前的任务,我已经看到了许多不同的解决方案:

I have seen many different solutions for my current task:

这个变体几乎是我想要的,但是有一个对象的自动生成,但我需要使用一次创建的同一个对象.

This variant is almost what I want, but there is an auto-generation of the objects, but I need to use the same object created once.

这个解决方案也几乎是我需要的,但我不确定这个对象的复制和返回的列表type is not ArrayList(它返回的是CopiesList类型,在以后的操作中不能添加到ArrayList中).

This solution is also almost what I need, but I am no sure about this copying of objects and the returned List type is not ArrayList (it returned the CopiesList type, which cannot be added to the ArrayList in the future operations).

附言也许它可能是重复的,但我真的找不到使用 Stream API 执行此操作的正确且简短的方法.

p.s. maybe it's possible duplicate, but I really can't find the correct and short way to do this using Stream API.

更新(Assylias 添加):

是的,我同意你关于这个变体的看法:

Yes, I agree with you about this variant:

List<SomeObject> list = Collections.nCopies(70, someObject);

但是,当我打开这个方法时:

But, when I am opening this method:

public static <T> List<T> nCopies(int n, T o) {
        if (n < 0)
            throw new IllegalArgumentException("List length = " + n);
        return new CopiesList<>(n, o);
    }

正如我们所见——它返回的是 CopiesList 对象,而不是 ArrayList.此外,与其他列表一样,它扩展了 AbstractList:

And as we see - it returning the CopiesList object, not ArrayList. Also, as the other lists, it is extending the AbstractList:

private static class CopiesList<E>
        extends AbstractList<E>
        implements RandomAccess, Serializable
    {
        private static final long serialVersionUID = 2739099268398711800L;

        final int n;
        final E element;

        CopiesList(int n, E e) {
            assert n >= 0;
            this.n = n;
            element = e;
        }
...}

它不完全是 ArrayList,但感谢您的建议和建议,没有空话和离题的评论,我会使用您的解决方案.

It's not quite ArrayList, but thank's for your suggestions and advice whithout an empty words and off-topic comments, I will use your solution.

推荐答案

您可以手动构建流:

List<SomeObject> list = Stream.generate(() -> someObject).limit(70).collect(toList());
//or if you want to make sure you get an ArrayList:
List<SomeObject> list = Stream.generate(() -> someObject).limit(70).collect(toCollection(ArrayList::new));

尽管为此目的创建流可能效率低下,我可能会采用@Eran 的建议:

Although creating a stream just for that purpose is probably inefficient and I would probably go with what @Eran suggested:

List<SomeObject> list = Collections.nCopies(70, someObject);
//or if you want to make sure you get an ArrayList:
List<SomeObject> list = new ArrayList<> (Collections.nCopies(70, someObject));

这篇关于Java 8 - 使用 Stream API 使用相同对象填充数组列表的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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