Home > Back-end >  Filter data Stream flow, urgent
Filter data Stream flow, urgent

Time:11-04

Known data, provide data
1. List InputData
2. ListArticle 3. The first key corresponding to the id, the value corresponding to the name

Require screening of the filter and there are no data in inputData, operated by stream flow and lambda expressions

CodePudding user response:

Period of the sample code to you

 public class Sample {
Static class SomeEntity {
String id;
String name;
Public SomeEntity (String id, String name) {this. Id=id; This. Name=name; }
Public String toString () {return the String. Format (" [] id: % s, name: % s ", id, name); }
}
Public static void main (String [] args) {
List InputData=https://bbs.csdn.net/topics/new ArrayList <> ();
For (int I=0; i<10; I +=2) {//id=0,2,4,6,8
Map M=new HashMap<> (a);
M.p ut (" id "+ I," the name - "+ I);
InputData. Add (m);
}

List The filter=new ArrayList<> (a);
For (int I=0; i<5; I++) {//id=0,1,2,3,4
Filter. The add (new SomeEntity (" id "+ I," the name - "+ I));
}

For (SomeEntity e: filter1 (inputData, filter)) {//from the filter screen 1, 3
System. The out. Println (e);
}

For (Map M: filter2 (inputData, filter)) {//select 6, 8 from inputData
System. The out. Println (m);
}
}

Public static List Filter1 (List InputData List The filter) {
The return filter. The stream (). The filter (e - & gt;
(inputData. Stream (). The filter (the in - & gt;
(in either containsKey (e.i d) & amp; & In the get (e.i d.) the equals (e.n ame))). The count ()==0)
). Collect (Collectors. ToList ());
}

Public static List Filter2 (List InputData List The filter) {
Return inputData. Stream (). The filter (in - & gt;
(filter. The stream (). The filter (e - & gt;
(in either containsKey (e.i d) & amp; & In the get (e.i d.) the equals (e.n ame))). The count ()==0)
). Collect (Collectors. ToList ());
}
}