Java 8 sum list of objects
Ngày đăng:
24/01/2022
Trả lời:
0
Lượt xem:
79
How do java8 stream group by multiple fields and sum over a single fieldthe following types of grouping and summation requirements are common in mysql Show by sql standards ,b,c column does not use the aggregate function in oracle the middle one will report an error, while the middle one will report an error mysql a loose group by mode, b,c the values are random, and now i have a set java what should an object do if it wants to do something similar finally, i want to use the stream stream to achieve the following effect of grouping sum how to use stream ### problem description the background of the problem and the methods i have triedthe relevant code// paste the code text below ( do not use pictures instead of code ) what results do you expect ? what is the actual error message that you see ?first time answer, hope can help you User class :class User { String name; String phone; String address; Long scope; public User(String name, String phone, String address) { this.name = name; this.phone = phone; this.address = address; } public User(String name, String phone, String address, Long scope) { this.name = name; this.phone = phone; this.address = address; this.scope = scope; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", phone='" + phone + '\'' + ", address='" + address + '\'' + ", scope=" + scope + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return Objects.equals(name, user.name) && Objects.equals(phone, user.phone) && Objects.equals(address, user.address) && Objects.equals(scope, user.scope); } @Override public int hashCode() { return Objects.hash(name, phone, address); } }rewrite equals and hashCode it's for grouping. the main body functionpublic class MyTest { public static void main(String[] args) { ArrayListthe resultsUser{name='jerry', phone='dd', address='ee', scope=70} User{name='tom', phone='bb', address='cc', scope=150} |