Java 8:stream API Exercises

Source: Internet
Author: User
Tags stream api

//Store attribute class public class Property {String name;    Distance, Unit: M Integer distance;    Sales, monthly sales of Integer sales;    Price, here's the simple point of writing a level representing the price segment Integer PriceLevel;        Public Property (String name, int distance, int sales, int pricelevel) {this.name = name;        This.distance = distance;        This.sales = sales;    This.pricelevel = PriceLevel; }public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Integer getdistance () {return distance;} public void Setdistance (Integer distance) {this.distance = distance;} Public Integer Getsales () {return sales;} public void Setsales (Integer sales) {this.sales = sales;} Public Integer Getpricelevel () {return pricelevel;} public void Setpricelevel (Integer pricelevel) {this.pricelevel = PriceLevel;} @Overridepublic String toString () {return "property [name=" + name + ", distance=" + Distance + ", sales=" + sales + ", PR    Icelevel= "+ pricelevel+"] ";} }

Java8stream it provides enhancements to the set operation unlike I/O public class Testjava8stream {public static void main (string[] args) {/*//stream operation list row Order System.out.println ("=====java8stream operation ============ Method 1 Find the nearest store");    Property P1 = new Property ("Cask Rice", 1000, 500, 2);    Property P2 = new Property ("Braised chicken in yellow sauce", 2300, 1500, 3);    Property P3 = new Property ("McDonald", 580, 3000, 1);    Property P4 = new Property ("KFC", 6000, 200, 4); list<property> properties = Arrays.aslist (P1, p2, p3, p4); String name2 = Properties.stream (). Sorted (Comparator.comparingint (x-x.distance)). Fin DFirst (). get (). Name; System.out.println ("Distance from my nearest shop is:" + name2); *//*system.out.println ("====== traditional Operations List Sort =========== method two find my nearest shop");    Property P1 = new Property ("Cask Rice", 1000, 500, 2);    Property P2 = new Property ("Braised chicken in yellow sauce", 2300, 1500, 3);    Property P3 = new Property ("McDonald", 580, 3000, 1);    Property P4 = new Property ("KFC", 6000, 200, 4); list<property> properties = Arrays.aslist (P1, p2, p3, p4);    Collections.sort (Properties, (x, y), X.distance.compareto (y.distance));    String name = Properties.get (0). Name; System.out.println ("Distance to my nearest store is:" + name); */system.out.println ("===== Iteration =======");    Property P1 = new Property ("Cask Rice", 1000, 500, 2);    Property P2 = new Property ("Braised chicken in yellow sauce", 2300, 1500, 3);    Property P3 = new Property ("WoI McDonald", 580, 3000, 1);    Property P4 = new Property ("KFC", 6000, 200, 4); list<property> properties = Arrays.aslist (P1, p2, p3, p4); int count = 0;//The number of stores sold more than 1000 for the month for (property Property:pro    Perties) {if (Property.sales >) {count++; }}system.out.println ("Number of stores with monthly sales greater than 1000:" +count);//Use internal iterations to calculate long Count2 = Properties.stream (). Filter (P-p.sales >). Count (); System.out.println ("Number of stores with monthly sales greater than 1000:" +count2), Long Count3 =properties.stream (). Filter (P.distance < 1000, p). Count ();//filter out my store within 1000 meters long count4 =properties.stream (). Filter (P, P.name.length () > 5). Count ();// Filter out a store property with a name greater than 5 characters = PropertiEs.stream (). Max (Comparator.comparingint (P-p.pricelevel)). get ();//filter out the lowest price store//get distance to my nearest 2 stores List<property > properties_s = Properties.stream (). Sorted (Comparator.comparingint (x-X.distance)). Limit ( 2). Collect (Collectors.tolist ());//Get the name of all stores list<string> names = Properties.stream (). Map (P-& Gt        p.name). Collect (Collectors.tolist ());//Get the price level of each store map<string, integer> map = Properties.stream () . Collect (Collectors.tomap (Property::getname, property::getpricelevel);//Shop list for all price levels Map<integer, list< property>> Pricemap = Properties.stream (). Collect (Collectors.groupingby (Property::getpricelevel));}}

  

Java 8:stream API Exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.