Java 8.使用收集器将值列表分组到范围列表中

时间:2016-08-02 15:24:35

标签: java collections lambda java-8 java-stream

我需要你的帮助,

在Java 8中使用收集器分组我需要将这样的列表分组

ValueObject {id=1, value=2.0}
ValueObject {id=2, value=2.0}
ValueObject {id=3, value=2.0}
ValueObject {id=4, value=3.0}
ValueObject {id=5, value=3.0}
ValueObject {id=6, value=4.0}
ValueObject {id=7, value=4.0}
ValueObject {id=8, value=4.0}
ValueObject {id=9, value=4.0}
ValueObject {id=10, value=4.0}

在另一个像这样的

GroupedObject {from=1, to=3, value=2.0}
GroupedObject {from=4, to=5, value=3.0}
GroupedObject {from=6, to=10, value=4.0}

这些是我正在使用的对象的定义

public class ValueObject {

  private int id;
  private double value;

  public String getId() {
    return id;
  }

  public float getValue() {
    return value;
  }

  public void setValue(float value) {
    this.value = value;
  }

}

public class GroupedObject {

    private int from;
    private int to;
    private double value;

    public int getFrom() {
        return from;
    }

    public void setFrom(int from) {
        this.from = from;
    }

    public int getTo() {
        return to;
    }

    public void setTo(int to) {
        this.to = to;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

}

这就是我以编程方式进行的方式。

public class Service {

    public List<GroupedObject> groupToRange(List<ValueObject> list) {

        List<GroupedObject> filtered = new ArrayList<>();

        if (list.size() > 0) {

            ValueObject current = list.get(0);
            GroupedObject dto = new GroupedObject();
            dto.setValue(current.getValue());
            dto.setFrom(current.getId());

            for (int i = 0; i < list.size(); i++) {
                ValueObject vo = list.get(i);
                if (vo.getValue() != current.getValue()) {

                    dto.setTo(current.getId());
                    filtered.add(dto);

                    dto = new GroupedObject();
                    dto.setValue(vo.getValue());
                    dto.setFrom(vo.getId());
                    current = vo;

                } else {
                    current = vo;
                }
                if (i == list.size() - 1) {
                    dto.setTo(vo.getId());
                    filtered.add(dto);
                }
            }
        }
        return filtered;
    }

}

这是单元测试

public class ServiceTest {

    Service service = new Service();

    @Test
    public void testgGoupToRange() {

        List entryList = new ArrayList<>();

        entryList.add(new ValueObject(1, 2.0));
        entryList.add(new ValueObject(2, 2.0));
        entryList.add(new ValueObject(3, 2.0));
        entryList.add(new ValueObject(4, 3.0));
        entryList.add(new ValueObject(5, 3.0));
        entryList.add(new ValueObject(6, 4.0));
        entryList.add(new ValueObject(7, 4.0));
        entryList.add(new ValueObject(8, 4.0));
        entryList.add(new ValueObject(9, 4.0));
        entryList.add(new ValueObject(10, 4.0));

        List responseList = service.groupToRange(entryList);

        responseList.forEach(obj-> System.out.println(obj.toString()));

        assertNotNull(responseList);
        assertEquals(3, responseList.size());

    }

}

我找不到使用java 8和收藏家

的方法

1 个答案:

答案 0 :(得分:2)

这就是我提出的

List<ValueObject> values = Arrays.asList(new ValueObject(1, 2.0),
                                         new ValueObject(2, 2.0),
                                         new ValueObject(3, 3.0),
                                         new ValueObject(4, 4.0),
                                         new ValueObject(5, 4.0),
                                         new ValueObject(6, 4.0));
Map<Double, IntSummaryStatistics> groupedValues = values.stream()
                                                        .collect(Collectors.groupingBy(ValueObject::getValue,
                                                                                       Collectors.summarizingInt(ValueObject::getId)));

List<GroupedObject> groupedObjects = groupedValues.entrySet()
                                                  .stream()
                                                  .map(groupedValue -> new GroupedObject(groupedValue.getValue().getMin(),
                                                                                  groupedValue.getValue().getMax(),
                                                                                  groupedValue.getKey()))
                                                  .collect(Collectors.toList());
System.out.println(groupedObjects);

我非常有信心有办法避免中间人Map<Double, IntSummaryStatistics,但我还没有想出来。如果我这样做会更新。

编辑:请参阅@Tunaki的答案,获得1次答案。

这是输出

[GroupedObject{from=4, to=6, value=4.0}, GroupedObject{from=1, to=2, value=2.0}, GroupedObject{from=3, to=3, value=3.0}]