Home > Back-end >  Java p6 - collection flow - programming problem (a big help.
Java p6 - collection flow - programming problem (a big help.

Time:09-25

The following Student class can be used directly,

Public class Student {

Private int number;

private String name;

Private String clazz;

Private int score;



Public Student (int number, a String name, String clazz, int score) {

This. Number=number;

this.name=name;

Enclosing clazz=clazz;

this.score=score;

}

//omit getter/setter methods

}


Write method in the following class, according to the demand, finish to the operation of the STUDENTS set

Public class StreamTest {

Private static final List STUDENTS=the create ();

Private static final String CLAZZ1=class 1, "software";

Private static final String CLAZZ2="software 2 class";

Private static List Create () {

Student s1=new Student (2018008, "Yang", CLAZZ2, 66);

Student s2=new Student (2018005, "vlon CLAZZ1, 92);

Student s3=new Student (2018007, "li Ming", CLAZZ2, 42);

Student s4=new Student (2018006, "zhao yong," CLAZZ2, 56);

Student s5=new Student (2018002, "wang lei" CLAZZ1, 81);

Student s6=new Student (2018010, "NiuNa CLAZZ1, 78);

List Students=new ArrayList<> (a);

Students. The add (s1); Students. The add (s2); Students. The add (s3);

Students. The add (s4); Students. The add (s5); Students. The add (s6);

Return students;

}



Public static void main (String [] args) {

//call the method test

}



//implementation method

}



Description:

Requirements described in the specified X, refers to the method of parameter

All methods have a return value, try to return statements directly programming, based on the stream flow required results returned directly

If return to the collection, use the List collection type

Try to use shorthand

Pay attention to the filter code format

Method 1, get scores less than or equal to the specified band, all the students

Answer:

Method 2, access to the specified class, scores less than or equal to the specified band, all the students

Answer:

Methods 3, access to the specified class, scores less than or equal to the specified band, all the names of the students, pay attention to the return type

Answer:

Methods 4, according to the result from low to high order, return all student

Answer:

Methods 5, access to the specified class, scores less than or equal to the specified band, scores from high to low order, all the students

Answer:

Methods 6, access to the specified class, scores less than or equal to the specified band, scores from high to low sorting, all of the student's student id, note that the return type

Answer:

Methods 7, access to the specified class, scores less than or equal to the specified band, all the students

Student id as the key to the student, the student score for the value, the Map groups, return

Answer:

CodePudding user response:

Is not operating API, this to yourself

CodePudding user response:

 
Public static void main (String [] args) {
//method 1, get scores less than or equal to the specified band, all the students
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ");
List Scoredayu=STUDENTS. Stream (). The filter (student - & gt; Student. GetScore () & gt; 75). Collect (Collectors. ToList ());
Scoredayu. Stream (). The forEach (student - & gt; System. The out. Println (student));
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ");
//method 2, access to the specified class, scores less than or equal to the specified band, all the students
List Collect2=STUDENTS. Stream (). The filter (student - & gt; CLAZZ2. Equals (student. GetClazz ()) & amp; & Student. GetScore () & lt; 75). Collect (Collectors. ToList ());
Collect2. Stream (). The forEach (student - & gt; System. The out. Println (student));
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ");
//method 3, access to the specified class, scores less than or equal to the specified band, all the names of the students, pay attention to the return type
List Collect3=STUDENTS. Stream (). The filter (student - & gt; CLAZZ2. Equals (student. GetClazz ()) & amp; & Student. GetScore () & lt; 75). The map (student - & gt; Student. The getName ()). Collect (Collectors. ToList ());
Collect3. Stream (). The forEach (student - & gt; System. The out. Println (student));
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ");
//method 4, according to the result from low to high order, return all student
List Collect4=STUDENTS. Stream (.) sorted (new Comparator () {
@ Override
Public int the compare (Student Student o1, o2) {
Return o1. GetScore () - o2. GetScore ();
}
}).collect(Collectors.toList());
Collect4. Stream (). The forEach (student - & gt; System. The out. Println (student));
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ");
//method 5, access to the specified class, scores less than or equal to the specified band, scores from high to low order, all the students
List Collect5=STUDENTS. Stream (). The filter (student - & gt; CLAZZ2. Equals (student. GetClazz ()) & amp; & Student. GetScore () & lt; 75). Sorted (new Comparator () {
@ Override
Public int the compare (Student Student o1, o2) {
Return o1. GetScore () - o2. GetScore ();
}
}).collect(Collectors.toList());
Collect5. Stream (). The forEach (student - & gt; System. The out. Println (student));
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ");
//method 6, access to the specified class, scores less than or equal to the specified band, scores from high to low sorting, all of the student's student id, note that the return type
List Collect6=STUDENTS. Stream (). The filter (student - & gt; CLAZZ2. Equals (student. GetClazz ()) & amp; & Student. GetScore () & lt; 75). Sorted (new Comparator () {
@ Override
Public int the compare (Student Student o1, o2) {
Return o2. GetScore () - o1. GetScore ();
}
}). The map (student - & gt; Student. GetNumber ()). Collect (Collectors. ToList ());
Collect6. Stream (). The forEach (student - & gt; System. The out. Println (student));
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ");
//methods 7, access to the specified class, scores less than or equal to the specified band, all the students
List nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related