In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how java customizes the sort () sort in List and is used to sort dates. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
Sort sort () in java custom List 1, problem description
List is an ordered and repeatable collection of java with its own .sort () sorting method, which is effective when sorting for pure numeric types of List collections. But for loading other types of List collections, the native sort () method sorting is difficult for us to control, such as the sorting of a date collection.
2. Solution
List in java allows us to customize the sort () sorting method. The following customizes the sort sorting method of the List collection, which is used to sort a date collection of type string.
/ / collections to be sorted List list=new ArrayList (); list.add ("2019-06"); list.add ("2019-11"); list.add ("2019-02"); list.add ("2019-09"); list.add ("2019-05"); / / Custom list sorting, collection data (months) sorted in ascending order; final SimpleDateFormat sdft = new SimpleDateFormat ("yyyy-MM") Collections.sort (list, new Comparator () {@ Override public int compare (String month2, String month3) {int mark = 1; try {Date date1 = sdft.parse (month2); Date date2 = sdft.parse (month3); if (date1.getTime () < date2.getTime ()) {mark =-1;-1 is the order in which no adjustment is required;} if (month2.equals (month3)) {mark = 0 }} catch (ParseException e) {LOG.error ("date conversion exception", e); e.printStackTrace ();} return mark;} / / compare}); 3.
In addition, objects of the two date types of java can also be compared in the following ways.
Date () date1=new Date (); Date () date2=new SimpleDateFormat ("yyyy-MM-dd"). Parse ("2019-06-11"); Boolean flag;if (date1.before (date2)) {flag=true;}
A.before (b); this method determines whether a date is less than b date and returns a Boolean result.
Java Collections class: sort () ascending sort forward sort
Use the static method sort () of the Collections class to sort the elements in the collection in ascending order. This requires that all elements in the list must implement the Comparable interface, and all elements must be comparable to each other using the specified comparator.
The sort () method mainly has the following two overloaded forms
Void sort (List list): sorts the elements in the collection in ascending order according to their natural order.
Void sort (List list,Comparator comparator): sorts the elements in the collection in the way specified by the comparator parameter.
Public class Test {public static void main (String [] args) {Student Student = new Student ("Zhang 1", "6m"); Student Student1 = new Student ("Zhang 2", "1m"); Student Student2 = new Student ("Zhang 4", "5m"); Student Student3 = new Student ("Zhang 5", "1m"); List list=new ArrayList (); list.add (Student); list.add (Student1) List.add (Student2); list.add (Student3); Collections.sort (list, new Comparator () {@ Override public int compare (Student o1, Student O2) {return o2.getAge () .compareTo (o1.getAge ());}}); System.out.println (list.toString ()) }} above is how java customizes the sort () sorting in List and uses it for date sorting. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.