Taught you how to List sorted by java8 new features specified attribute, filter duplicate data

Often encounter such a problem in java, in practical application, there will always encounter on the List Sorting and filtering duplicate questions, if List is simply put in the String type filtering so easy, but the practical application and does not so easy, often in place of List is a class, class has multiple attributes to filter duplicate data, duplicate data and this shall give as he specified attribute filter, however, to filter by other properties sort order, so we have to sort what, then filtered according to a property.

  • As shown in the following entity class, as long as you create the following entity class without inheriting the parent class, we will not comment style, please add getter / setter methods on their own.

  • List graphResults first look inside what is this data, the data shown below, due to the large amount of data shown below is just a few pieces to be processed, as well as about the structure, we can look at the data this time is 17:30, the following mainly deal with this point in time.
  • Now I would like to make the following deal on this List
  1. Chronological order
  2. On the basis of the time-ordered re-sorted in descending order of pv1Power and pv2Power
  3. Filtered and the power of repetition time field 0 Invalid, leaving only a valid field to 0 if all data have reserved to just one of the time
  •  

  • In order to meet these requirements, new steam flow characteristics jdk1.8 to do, the whole idea, is placed in the List flow stream, and then sequentially ordered data repetition period to be filtered, of course, be set to Set stage, to specified attribute filter, of course, to pass a time, and finally return to List collection.
1    // press dates from small to large, then descending power, then filtered into a collection of repeat, return List 
2              graphResults = graphResults.stream (). The sorted (Comparator.comparing (GraphResult :: getDate)
 . 3                      .thenComparing ((O1, O2) -> Integer.compare (o2.getPv1Power (), o1.getPv1Power ()))
 . 4                      .thenComparing ((O1, O2) -> Integer.compare (o2.getPv2Power (), O1. getPv2Power ())))
 . 5                      .collect (Collectors.collectingAndThen (Collectors.toCollection (() ->
 . 6                              new new TreeSet <> (Comparator.comparing (GraphResult :: getDate))), the ArrayList :: new new )); // filter repeat point in time
  •  Result by the code runs on the obvious, 17:30 found this time is not repeated, and the invalid data does not exist, and a perfect realization of the above requirements
  •  

Guess you like

Origin www.cnblogs.com/longmaodaxia/p/11308333.html