For a given Student class, random 10 students, according to their grades group statistical proficiency, good rate and pass rate,
Student class implementation is as follows:
Public class Student implements Comparable{
private String id;
private String name;
Private String gender;
private String dept;
Private int score;
Public Student () {};
Public Student (String id, String name, String gender, String dept, int score) {
this.id=id;
this.name=name;
This. Gender=gender;
This. Dept=dept;
This. Score=score;
}
Public String getId () {
return id;
}
Public String getName () {
return name;
}
Public String getDept () {
Return dept.
}
Public String getGender () {
Return the gender;
}
Public int getScore () {
Return score;
}
Public void showInfo () {
System. The out. Println (" student id: "+ id);
System. The out. Println (" name: "+ name);
System. The out. Println (" gender: "+ gender);
System. The out. Println (", "professional + dept);
System. The out. Println (" result: "+ score);
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ");
}
@ Override
Public int compareTo (Student arg0) {
//Ordered by score desc
Return arg0. Score - score;
}
}
I will student information reads coexist in an ArrayList
ArrayListArrlist=new ArrayList<> (a);
Adopts the following method to generate the ArrayList of 10 different subscript, used for sampling
HashSetMySet=new HashSet (a);
Int size=arrlist. The size ();
While (MySet. The size ()!=10) {
MySet. Add ((int) (Math. The random () * (size - 1)));
}
Now I want to by the stream on the 10 samples according to the group rules, rule is
- if grades & gt; Good=85
- else if grades & gt; Good=80
- else if grades & gt;=70 to pass the
- the else fail
I defined Map
I want to use the following ways to
MapTypeToScore=
MySet. Stream ()
The map (o1 - & gt; Arrlist. Get (o1). GetScore ())
Collect (Collectors. GroupingBy (o1 - & gt; {
If (o1 & gt; {
=85)return 0;
} else if (o1 & gt; {
=80)return 1;
} else if (o1 & gt; {
=70)return 2;
} else {
Return 3;
}
}));
But doing so
when you make a mistake
Local variable arrlist defined in an enclosing scope must be final or effectively final
I try not to use Lambda expressions, instead of an anonymous inner class, then switch to external to define new class to implement the Function interface, can appear the same error message,
Excuse me how to solve this problem?
CodePudding user response:
TheArrayList
Change
Final ArrayList
Try
Because the lambda or anonymous classes using external local variables, the external local variables must be final,