Recently re-learned and learned the functional interface of Java8 and lambda expression, found that the previous written code is mostly pseudo-function expression, so re-refining this part of the code, for reference only, if there is a better way, welcome comments in the comment area.
1. Calculate the total amount of the order
The total amount of the order is generally in the background loop overlay the amount of each purchased item has been acquired, usually in the following way
New BigDecimal (0);
for (venditionproduct venditionproduct: venditionvo.getvenditionproductlist ()) { = Totalamount.add (Venditionproduct.getprice ()); }
Use lambda in the following way
BigDecimal totalprice = Venditionvo.getvenditionproductlist (). Stream (). Map (Venditionproduct::getprice). Reduce (Bigdecimal::add). OrElse (Bigdecimal.one);
2. Cyclic collection assembly New class
The scenario is to update the field, and an update method, Updatenotnull, will appear below, which is a generic method that only updates properties that are not null for the field. Because only the price and Sellamount properties of the product are allowed in venditionproduct, it is not possible to directly update the collection that is passed by the front end directly, and the two properties need to be extracted to reassemble into the new class.
The procedure for comparing low is as follows (pseudo-function expression)
Venditionproductlist.foreach (Venditionproduct, { new venditionproduct (); Newvenditionproduct.setid (Venditionproduct.getid ()); Newvenditionproduct.setsellamount (Venditionproduct.getsellamount ()); Newvenditionproduct.setprice (Venditionproduct.getprice ()); Updatenotnull (newvenditionproduct); });
The following is a true-function expression
Venditionproductlist.stream (). Map (Venditionproduct- venditionproduct.build (Venditionproduct.getid (), Venditionproduct.getsellamount (), Venditionproduct.getprice ()) . ForEach (this:: Updatenotnull);
3. Applications in enumerations
We have the following enumeration classes
Public enumRecordtypeenum {/*** 0 for manual warehousing 1 Production Warehousing 2 manual out of the library 4 return storage*/Manual_store (0), Produce_store (1), Manual_out (2), Vendition_out (3), refund_in (4),; Private Final inttype; Recordtypeenum (inttype) { This. Type =type; } }
The scenario you are encountering may be that you want to find the corresponding enumeration value by type
The usual way is
Public Static Recordtypeenum getrecordtypeenum (int type) { for (recordtypeenum typeenum: Recordtypeenum.values ()) { if (typeenum.gettype () = = type) {return typeenum; } } return NULL ; }
Java8, we can do this like this.
Public Static Recordtypeenum getRecordTypeEnum1 (int type) { return Arrays.stream ( Recordtypeenum.values ()). Filter (Recordtypeenum, recordtypeenum.gettype () = = type). FindFirst (). OrElse (null); }
And we might need to get the corresponding array
Public string[] Getenumstringarray () { return arrays.stream (Recordtypeenum.values ()). Map (Enum :: Name). ToArray (string[]::new); }
4. Writing a line of lambda syntax helps to improve the robustness of your code (the focus here is not on the use of lambda, but I think it's worth recording)
The business scenario is to increase the production schedule (plan), a schedule can have multiple products, a product needs one or more ingredients, so the relationship is a pair of many, the following look at my first write code (pseudo-function expression)
、
Focus on the last approach, the initial implementation is this
Here are two questions, one is that a short lambda expression can improve code readability, which is one of the important benefits of functional programming, which is completely overlooked, and the second is that planproduct (products) and planmaterial (raw materials) are linked together, In other words, the operation of adding raw materials should be encapsulated in the operation of adding products, the revised code is as follows
The reason for the record here is that while the lambda expression is streamlined into a single line of code, it is necessary to constantly adjust the structure of the code to move toward a more stable and robust direction, the original code logic, although the process is understandable, but ugly and long, as too many tasks.
5. Conversion from a set of known types to a collection of another type
The approximate logic is to obtain the raw material associated with the production product (including specifications), which is actually required to convert to list<planmaterial> according to List<productmaterail>
The practice here is to make the same mistake as above, the following adjustment
Not to be continued ...
Java8 functional interfaces and lambda expression practices