62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;
public class Demo32 {
public static void main(String[] args) {
// TEST DATA
List<PNode> list = new ArrayList<PNode>();
list.add(new PNode("1234", 1));
list.add(new PNode("1234", 1));
list.add(new PNode("1234", 1));
list.add(new PNode("1991", 1));
list.add(new PNode("1234", 2));
list.add(new PNode("1991", 10));
list.add(new PNode("1991", 10));
list.add(new PNode("1234", 3));
list.add(new PNode("a", 2));
list.add(new PNode("b", 12));
list.add(new PNode("a", 1));
TreeSet<PNode> tsNode = list.parallelStream().collect(Collectors.toCollection(() -> new TreeSet<PNode>((x, y) -> {
if (x == y) return 0;
int _t = x.getNo().compareTo(y.getNo());
if (_t == 0) {
int t = x.getTotal() + y.getTotal();
x.setTotal(t);
y.setTotal(t);
}
return _t;
})));
tsNode.forEach(System.out::println);
}
}
class PNode {
private String no;
private int total;
public PNode() { };
public PNode(String no, int total) {
this.no = no;
this.total = total;
}
public String getNo() {
if (no == null) {
no = "";
}
return no;
}
public void setTotal(int total) {
this.total = total;
}
public int getTotal() {
return total;
}
@Override
public String toString() {
return String.format("{\"no\": \"%s\", \"total\": %d}", getNo(), total);
}
}