List.stream (). maptodouble (User::getheight). SUM ()// and List.stream (). Maptodouble (User::getheight ). Max ()// max list.stream (). maptodouble (user::getheight). Min ()// min . list.stream (). maptodouble (User::getheight). Average ()// average
Of course, in addition to the statistics double type, there are also int and long
BigDecimal need to use reduce sum
Double example:
Public classHelloWorld {Private Static FinalDecimalFormat DF =NewDecimalFormat ("0.00");//two decimal places reserved Public Static voidMain (string[] args) {random random=NewRandom (); List<User> list =NewArraylist<>(); for(inti=1;i<=5;i++) { DoubleWeight = random.nextdouble () * 100 + 100;//Random Height: 100-200User U =NewUser (I, "users-" +I, weight); List.add (U); } System.out.println ("User:" +list); Doublesum =List.stream (). maptodouble (User::getheight). sum (); System.out.println ("Height sum:" +Df.format (sum)); DoubleMax =List.stream (). maptodouble (User::getheight). Max (). getasdouble (); System.out.println ("Height Max:" +Df.format (max)); DoubleMin =List.stream (). maptodouble (user::getheight). Min (). getasdouble (); System.out.println ("Min. Height:" +df.format (min)); DoubleAverage =List.stream (). maptodouble (User::getheight). Average (). getasdouble (); System.out.println ("Height Average:" +Df.format (average)); } Private Static classuser{Integer ID; String name; DoubleHeight//Height PublicUser (Integer ID, String name,Doubleheight) { This. ID =ID; This. Name =name; This. Height =height; } PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public Doublegetheight () {returnheight; } Public voidSetHeight (Doubleheight) { This. Height =height; } @Override PublicString toString () {return"user{" + "id=" + ID + ", name= '" + name + ' \ ' + ", height=" + H Eight + '} '; } }}
Execution Result:
User:
user{id=5, name= ' user-5 ', height=106.27720636757154}] Height sum:707.58 height: 196.35 height min.:101.96 height Average:141.52
BigDecimal Example:
Public classHelloWorld {Private Static FinalDecimalFormat DF =NewDecimalFormat ("0.00");//two decimal places reserved Public Static voidMain (string[] args) {random random=NewRandom (); List<User> list =NewArraylist<>(); for(inti=1;i<=5;i++) { DoubleWeight = random.nextdouble () * 100 + 100;//Random Height: 100-200List.add (NewUser (I,NewBigDecimal (weight). Setscale (BIGDECIMAL.ROUND_HALF_UP, 2))); } System.out.println ("List:" +list); BigDecimal Add=List.stream (). Map (user::getheight). Reduce (Bigdecimal.zero, bigdecimal::add); System.out.println ("Height sum:" +Df.format (add)); Optional<User> max = List.stream (). Max ((U1, U2),u1.getheight (). CompareTo (U2.getheight ())); System.out.println ("Height Max:" +Df.format (Max.get (). GetHeight ())); Optional<User> min = List.stream () min ((U1, U2)u1.getheight (). CompareTo (U2.getheight ())); System.out.println ("Min. Height:" +Df.format (Min.get (). GetHeight ())); } Private Static classuser{Integer ID; BigDecimal height;//Height PublicUser (Integer ID, BigDecimal height) { This. ID =ID; This. Height =height; } PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicBigDecimal getheight () {returnheight; } Public voidsetheight (BigDecimal height) { This. Height =height; } @Override PublicString toString () {return"user{" + "id=" + ID + ", height=" + Height + '} '; } }}
Execution Result:
List
user{id=5, height=177.7596}] Height sum:711.8550 height max: 177.76 height min.:101.54
JAVA8 list statistics (sum, Maximum, minimum, average)