The method of sorting the list collection in Java the use of the comparator is sorted according to the sum of the Chinese English scores of the students ' object sequence

Source: Internet
Author: User

 PackageCom.swift;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List;ImportJava.util.Scanner; Public classtest2_objectio {@SuppressWarnings ("Unchecked")     Public Static voidMain (string[] args) {/** 2nd: There are five students, each student has 3 courses (Chinese, maths, English) scores, write a program to receive information from the keyboard input students, * The input format is: name,30,30,30 (name, three lessons), and then the input of the student information according to the total score from the high         To a low order written to a name "Stu.txt" file.         * Requirements: Stu.txt file format to be more intuitive, open this file, you can see the student's information very clearly. */List<Student> list =NewArraylist<student>(); Scanner Scan=NewScanner (system.in);  for(inti = 1; I <=5; i++) {System.out.println ("Please enter" +i+ "Student Information: format name 60 60 60"); String name=Scan.next (); Float Yuwen=scan.nextfloat (); Float Shuxue=scan.nextfloat (); Float Yingyu=scan.nextfloat (); Student Stu=NewStudent (name, Yuwen, Shuxue, Yingyu);        List.add (Stu); }                 for(Student stu:list) {System.out.println (stu.tostring ()); } collections.sort (List,NewComparator<student>() {@Override Public intCompare (Student O1, Student O2) {floatNum= (O2.getshuxue () +o2.getyingyu () +o2.getyuwen ())-(O1.getshuxue () +o1.getyingyu () +O1.getyuwen ()); intI= (int) num; returni;                }        });  for(Student stu:list) {System.out.println (stu.tostring ()); }    }}classStudent {PrivateString name; Private floatYuwen; Private floatShuxue; Private floatYingyu;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public floatGetyuwen () {returnYuwen; }     Public voidSetyuwen (floatYuwen) {         This. Yuwen =Yuwen; }     Public floatGetshuxue () {returnShuxue; }     Public voidSetshuxue (floatShuxue) {         This. Shuxue =Shuxue; }     Public floatGetyingyu () {returnYingyu; }     Public voidSetyingyu (floatYingyu) {         This. Yingyu =Yingyu; }     PublicStudent (String name,floatYuwen,floatShuxue,floatYingyu) {        Super();  This. Name =name;  This. Yuwen =Yuwen;  This. Shuxue =Shuxue;  This. Yingyu =Yingyu; }     PublicStudent () {Super(); } @Override PublicString toString () {return"Student [name=" + name + ", yuwen=" + Yuwen + ", shuxue=" + Shuxue + ", yingyu=" + Yingyu + "]"; }}

The comparator is an anonymous inner class, Collections.sort (List,comparator<student> () {});

The Compare method is then implemented, returning a value of type int, positive negative 0 representing greater than or equal to

Add the content code of IO stream as follows;

 PackageCom.swift;ImportJava.io.BufferedWriter;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStreamWriter;Importjava.io.UnsupportedEncodingException;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List;ImportJava.util.Scanner; Public classtest2_objectio {@SuppressWarnings ("Unchecked")     Public Static voidMain (string[] args) {/** 2nd: There are five students, each student has 3 courses (Chinese, maths, English) scores, write a program to receive information from the keyboard input students, * The input format is: name,30,30,30 (name, three lessons), and then the input of the student information according to the total score from the high         To a low order written to a name "Stu.txt" file.         * Requirements: Stu.txt file format to be more intuitive, open this file, you can see the student's information very clearly. */List<Student> list =initiatelist ();  for(Student stu:list) {System.out.println (stu.tostring ()); } collections.sort (List,NewComparator<student>() {@Override Public intCompare (Student O1, Student O2) {floatNum= (O2.getshuxue () +o2.getyingyu () +o2.getyuwen ())-(O1.getshuxue () +o1.getyingyu () +O1.getyuwen ()); intI= (int) num; returni;                }        });                    Initiate_io (list); }    Private Static voidInitiate_io (list<student>list)        {BufferedWriter bw; StringBuffer SB=NewStringBuffer ();  for(Student stu:list) {sb.append (stu.tostring ()); Sb.append ("\ r \ n"); }        Try{BW=NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream ("Src\\stu.txt"), "Utf-8");//need to indicate pathBw.write (sb.tostring ());        Bw.flush (); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }             }    Private StaticList<student>initiatelist () {List<Student> list =NewArraylist<student>(); Scanner Scan=NewScanner (system.in);  for(inti = 1; I <=5; i++) {System.out.println ("Please enter" +i+ "Student Information: format name 60 60 60"); String name=Scan.next (); Float Yuwen=scan.nextfloat (); Float Shuxue=scan.nextfloat (); Float Yingyu=scan.nextfloat (); Student Stu=NewStudent (name, Yuwen, Shuxue, Yingyu);        List.add (Stu); }        returnlist; }}classStudent {PrivateString name; Private floatYuwen; Private floatShuxue; Private floatYingyu;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public floatGetyuwen () {returnYuwen; }     Public voidSetyuwen (floatYuwen) {         This. Yuwen =Yuwen; }     Public floatGetshuxue () {returnShuxue; }     Public voidSetshuxue (floatShuxue) {         This. Shuxue =Shuxue; }     Public floatGetyingyu () {returnYingyu; }     Public voidSetyingyu (floatYingyu) {         This. Yingyu =Yingyu; }     PublicStudent (String name,floatYuwen,floatShuxue,floatYingyu) {        Super();  This. Name =name;  This. Yuwen =Yuwen;  This. Shuxue =Shuxue;  This. Yingyu =Yingyu; }     PublicStudent () {Super(); } @Override PublicString toString () {return"Student [name=" + name + ", yuwen=" + Yuwen + ", shuxue=" + Shuxue + ", yingyu=" + Yingyu + "]"; }}

The method of sorting the list collection in Java the use of the comparator is sorted according to the sum of the Chinese English scores of the students ' object sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.