避免使用Lambdaj进行循环

时间:2016-04-05 08:05:06

标签: java lambdaj

如何使用lambdaj避免这个循环,我想将List角色中的所有元素添加到另一个列表中

Firefox

我使用Java 7所以,Java 8 lambda表达式无法工作,我需要一个解决方案Lambdaj library

2 个答案:

答案 0 :(得分:0)

使用java 8:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>




  <tr class='skip'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='skip'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='skip'>
    <td>First</td>
    <td>Second</td>
  </tr>

  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>
  <tr class='include'>
    <td>First</td>
    <td>Second</td>
  </tr>

</table>

如果lista已经包含数据:

String tipo = "type_1";
lista = personas.stream()
             .map(person -> new SimpleResultForm(tipo, person))
             .collect(Collectors.toList());

答案 1 :(得分:-1)

在Java 8中,假设您已经创建了列表“lista”,那么这个单行应该可以完成工作。

String tipo = "type_1";
personas.stream().forEach(e -> lista.add(new SimpleResultForm(tipo, e)));