Home > Back-end >  Please enter Java problem list algorithm (ace)
Please enter Java problem list algorithm (ace)

Time:11-16

There is a list As follows:
The 2020-10-28
The 2020-10-29
The 2020-10-30
The 2020-11-02
The 2020-11-03
The 2020-11-04
The 2020-11-05
The 2020-11-06
The 2020-11-09
The 2020-11-10
The 2020-11-11
The 2020-11-12
The 2020-11-13
The 2020-11-16
The 2020-11-17
The 2020-11-18
The 2020-11-19
The 2020-11-20
The 2020-11-23
The 2020-11-24
The 2020-11-25
The 2020-11-26
The 2020-11-27
The 2020-11-30
The 2020-12-01
The 2020-12-02
With the algorithm to find the maximum value of each month, returns a new list
The 2020-10-30
The 2020-11-30
2020-12-02

CodePudding user response:

For example

 public class Sample {
Public static void main (String [] args) {
List List=new ArrayList<> (arrays.aslist (new String [] {
"2020-10-01", "2020-10-30", "2020-11-01", "2020-11-30", "2020-12-01", "2020-12-02"
}));
List Result=list. Stream (). Collect (Collectors. GroupingBy (e - & gt; E.s ubstring (0, 7))). EntrySet () stream (). The map (e - & gt; {
Return the um participant etValue (). The stream (). Max ((s1, s2) - & gt; S1.com pareTo (s2)). The get ();
}). Collect (Collectors. ToList ());
System. The out. Println (result);
}
}

CodePudding user response:

If the data is not more than 100000, in the database to solve,

CodePudding user response:

reference 1st floor qybao response:
for example

 public class Sample {
Public static void main (String [] args) {
List List=new ArrayList<> (arrays.aslist (new String [] {
"2020-10-01", "2020-10-30", "2020-11-01", "2020-11-30", "2020-12-01", "2020-12-02"
}));
List Result=list. Stream (). Collect (Collectors. GroupingBy (e - & gt; E.s ubstring (0, 7))). EntrySet () stream (). The map (e - & gt; {
Return the um participant etValue (). The stream (). Max ((s1, s2) - & gt; S1.com pareTo (s2)). The get ();
}). Collect (Collectors. ToList ());
System. The out. Println (result);
}
}


How to filter out the week 6 Sunday's data, such as
List List=new ArrayList<> (arrays.aslist (new String [] {" 2020-10-01 ", "2020-10-30", "2020-10-31",
"2020-11-01", "2020-11-30", "2020-12-01", "2020-12-02"}));
Normal output: [2020-12-02, 2020-12-02, 2020-10-31]
But to filter out 6 weeks after Sunday's data should be output [2020-12-02, 2020-12-02, 2020-10-30]

  • Related