Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method for java8 to sort and filter duplicate data by specified attributes in List?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the method for java8 to sort and filter duplicate data by specified attributes in List". In daily operation, it is believed that many people have doubts about the method of sorting and filtering duplicate data in List by specified attributes in java8. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the question of "what is the way for java8 to sort and filter duplicate data by specified attributes in List?" Next, please follow the editor to study!

We often encounter such a problem in java. In practical application, we always encounter the problem of sorting List and filtering repetition. If only a simple String type is put in List to filter so easy, but in practical application, it is not so easy. Often there is a class in List, and there are multiple attributes in the class. To filter duplicate data, and the duplicate data should be filtered according to the attributes specified by yourself. But if you want to filter in the sort order of other attributes, sort it first, and then filter by an attribute.

The entity class is shown below. As long as you create the following entity class, you do not need to inherit the parent class. If you do not know the annotated style, please add your own getter/setter method.

First of all, let's take a look at the data in the List of graphResults. The data is shown as follows. Due to the large amount of data, the following only shows the number of items to be processed, as well as the general structure. You can take a closer look at the data at 17:30, and the following mainly deals with this point in time. Now I'm going to do the following for this List

Sort in chronological order on the basis of chronological sorting, and then filter the invalid fields with repetitive time and power of 0 for pv1Power and pv2Power from the largest to the smallest, leaving only one valid field, and only one piece of data with that time if all zero.

In order to meet the above requirements, use jdk1.8 's new feature steam stream to do, the overall idea is to put List into the stream stream, and then sort in turn, to filter the data of repeated time periods, of course, to enter the stage by the Set collection, to filter by the specified attributes, of course, to pass a time, and finally return the List collection.

/ / sort by date from small to large, then by power from large to small, and then put it into the collection to filter repetition. Return List graphResults = graphResults.stream () .sorted (Comparator.comparing (GraphResult::getDate) .thencomparing ((o1, O2)-> Integer.compare (o2.getPv1Power (), o1.getPv1Power () .thencomparing ((o1, O2)-> Integer.compare (o2.getPv2Power (), o1.getPv2Power () .compare (Collectors.collectingAndThen (Collectors.toCollection (())-> new TreeSet (Comparator.comparing (GraphResult::getDate)), ArrayList::new) / / filter repeat time points

Through the above code running the results are obvious, found that 17:30 this time is not repeated, and invalid data does not exist, the perfect realization of the above requirements

At this point, the study on "what is the method for java8 to sort and filter duplicate data by specified attributes in List" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report