Stream API. 将Model类的String字段的所有值收集到一个集合中,打印。

huangapple go评论58阅读模式
英文:

Stream API. Collect all the values ​of the String field of the Model class into one collection, print

问题

以下是翻译好的代码部分:

// 一个任务:
// 有一个 Model 类,它有一个 String 类型的字段。有两个容器类。类 A 包含一个特定类型 Model 的对象列表和一个类型为 B 的字段。类 B 也包含一个 Model 对象列表。创建一个包含 A 对象的集合,填充它们的内部状态(列表、对象 B),类似于 B。收集 Model 类的 String 字段的所有值到一个集合中,并打印出来。
// 可以这样写:

public class Task_2 {

    public static void main(String[] args) {

        // 创建 B 对象
        List<Model> modelsForB = new ArrayList<>();
        modelsForB.add(new Model("B1 "));
        modelsForB.add(new Model("B2 "));
        B b = new B(modelsForB);

        // 创建 A 对象
        List<Model> modelsForA = new ArrayList<>();
        modelsForA.add(new Model("A1 "));
        modelsForA.add(new Model("A2 "));
        A a = new A(modelsForA, b);

        // 收集 Model 类的 String 字段值
        final List<String> collected = new ArrayList<>();

        Stream.of(a)
                .flatMap(item -> Stream.of(item.b))
                .flatMap(itemB -> itemB.models.stream())
                .map(model -> model.string)
                .forEach(collected::add);

        Stream.of(a)
                .flatMap(itemA -> itemA.models.stream())
                .map(model -> model.string)
                .forEach(collected::add);
        collected.forEach(System.out::println);
    }

    static class Model {

        String string;

        public Model(String string) {
            this.string = string;
        }
    }

    static class A {

        private List<Model> models;
        B b;

        public A(List<Model> models, B b) {
            this.models = models;
            this.b = b;
        }
    }

    static class B {
        private List<Model> models;

        public B(List<Model> models) {
            this.models = models;
        }
    }
}

请注意,上述代码已经尽可能简洁地编写,但仍然保持了功能。如果需要更详细的说明或其他更改,请提出具体的需求。

英文:

//A task:
There is a Model class that has a field of type String. There are two container classes. Class A contains a list of objects of a particular type Model and a field of type B. Class B also contains a list of objects Model. Create a collection of objects A, fill in their internal state (list, object B), similarly to B. Collect all values ​​of the String field of the Model class into one collection, print.
wrote like this:
How to write it shorter?

public class Task_2 {
public static void main(String[] args) {
List&lt;Model&gt; modelsForB = new ArrayList&lt;&gt;();
modelsForB.add(new Model(&quot;B1 &quot;));
modelsForB.add(new Model(&quot;B2 &quot;));
B b = new B(modelsForB);
List&lt;Model&gt; modelsForA = new ArrayList&lt;&gt;();
modelsForA.add(new Model(&quot;A1 &quot;));
modelsForA.add(new Model(&quot;A2 &quot;));
A a = new A(modelsForA, b);
final List&lt;String&gt; collected = new ArrayList&lt;&gt;();
Stream.of(a)
.flatMap(item -&gt; Stream.of(item.b))
.flatMap(itemB -&gt; itemB.models.stream())
.map(model -&gt; model.string)
.forEach(collected::add);
Stream.of(a)
.flatMap(itemA -&gt; itemA.models.stream())
.map(model -&gt; model.string)
.forEach(collected::add);
collected.forEach(System.out::println);
}
static class Model {
String string;
public Model(String string) {
this.string = string;
}
}
static class A {
private List&lt;Model&gt; models;
B b;
public A(List&lt;Model&gt; models, B b) {
this.models = models;
this.b = b;
}
}
static class B {
private List&lt;Model&gt; models;
public B(List&lt;Model&gt; models) {
this.models = models;
}
}

how to write it shorter?

答案1

得分: 2

静态的 concat 方法可以用于这个任务:

List<String> collected = Stream
        .concat(a.models.stream(), a.b.models.stream())
        .map(model -> model.string)
        .collect(Collectors.toList());
英文:

The static concat method could be used for that task:

    List&lt;String&gt; collected = Stream
.concat(a.models.stream(), a.b.models.stream())
.map(model -&gt; model.string)
.collect(Collectors.toList());

答案2

得分: 0

根据您的问题,您想要"创建一个对象A的集合",所以我会确保您的答案能够实现这一点,而不仅仅是单个A。

我还添加了使用Arrays.asList(用于额外的条目),只是为了简洁起见

public static void main(String[] args) {
    List<Model> modelsForB = new ArrayList<>();
    modelsForB.add(new Model("B1 "));
    modelsForB.add(new Model("B2 "));
    B b = new B(modelsForB);
    B b1 = new B(Arrays.asList(new Model("B1.1"), new Model("B1.2")));
    List<Model> modelsForA = new ArrayList<>();
    modelsForA.add(new Model("A1 "));
    modelsForA.add(new Model("A2 "));
    A a = new A(modelsForA, b);
    A a1 = new A(Arrays.asList(new Model("A1.1"), new Model("A1.2")), b1);
    List<A> listOfA = Arrays.asList(a, a1);

    // 如果您需要根据要求的集合,否则只需使用forEach来打印,而不是collect
    final List<String> collected = listOfA.stream()
            .flatMap(x -> Stream.concat(x.models.stream(), x.b.models.stream()))
            .map(m -> m.string)
            .collect(Collectors.toList());
    collected.forEach(System.out::println);
}

请注意,这是您提供的代码的中文翻译。

英文:

Per your question, you want to "Create a collection of objects A", so I would make sure your answer provides for this, and not just a single A.

I also threw in using Arrays.asList (for the added entries), just for brevity

public static void main(String[] args) {
List&lt;Model&gt; modelsForB = new ArrayList&lt;&gt;();
modelsForB.add(new Model(&quot;B1 &quot;));
modelsForB.add(new Model(&quot;B2 &quot;));
B b = new B(modelsForB);
B b1 = new B(Arrays.asList( new Model(&quot;B1.1&quot; ), new Model(&quot;B1.2&quot; )));
List&lt;Model&gt; modelsForA = new ArrayList&lt;&gt;();
modelsForA.add(new Model(&quot;A1 &quot;));
modelsForA.add(new Model(&quot;A2 &quot;));
A a = new A(modelsForA, b);
A a1 = new A(Arrays.asList( new Model(&quot;A1.1&quot; ), new Model( &quot;A1.2&quot; )), b1 );
List&lt;A&gt; listOfA = Arrays.asList( a, a1 );
// If you need the collection, per the requirement, otherwise just use forEach to print instead of collect
final List&lt;String&gt; collected = listOfA.stream()
.flatMap( x -&gt; Stream.concat( x.models.stream(), x.b.models.stream() ))
.map(m -&gt; m.string )
.collect(Collectors.toList());
collected.forEach(System.out::println);
}

huangapple
  • 本文由 发表于 2020年8月5日 03:51:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63254164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定