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

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

问题描述

我是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.

这解决方案也几乎是我需要的,但我不确定这个对象的复制和返回的List类型不是 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).

p.s。也许它可能重复,但我真的找不到使用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使用相同对象填充arraylist的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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