Student.java
PackageYzhou.map;/*** Student Class * * *@authorOcean **/ Public classstudent{PrivateString name; PrivateString No; Private Doublescore; PublicStudent () {} PublicStudent (string name, String No,Doublescore) { Super(); This. Name =name; This. No =No; This. score =score; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString Getno () {returnNo; } Public voidSetno (String no) { This. No =No; } Public DoubleGetscore () {returnscore; } Public voidSetScore (Doublescore) { This. score =score; } }
Classroom.java
PackageYzhou.map;Importjava.util.ArrayList;Importjava.util.List;/*** Multiple students in a class *@authorOcean **/ Public classclassroom{PrivateString No; PrivateList<student>stulist; Private DoubleTotal ; PublicClassroom () {stulist=NewArraylist<student>(); } PublicClassroom (String no) { This(); This. No =No; } PublicClassroom (String No, list<student> stulist,DoubleTotal ) { Super(); This. No =No; This. stulist =stulist; This. Total =Total ; } PublicString Getno () {returnNo; } Public voidSetno (String no) { This. No =No; } PublicList<student>getstulist () {returnstulist; } Public voidSetstulist (list<student>stulist) { This. stulist =stulist; } Public Doublegettotal () {returnTotal ; } Public voidSettotal (DoubleTotal ) { This. Total =Total ; } }
Mapdemo03.java
PackageYzhou.map;Importjava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set;/*** * Object-oriented + exploded storage *@authorOcean **/ Public classmapdemo03{ Public Static voidMain (string[] args) {List<Student> stulist =exam (); Map<string, classroom> map =count (stulist); View (map); } /** View the total and average score of each class-"Traverse map *"*/ Public Static voidView (map<string, classroom>map) {Set<String> keys =Map.keyset (); //get Iterator ObjectIterator<string> keysit =Keys.iterator (); //First Judge while(Keysit.hasnext ()) {//in gettingString No =Keysit.next (); Classroom=Map.get (NO); //View total score calculation average DoubleTotal =room.gettotal (); DoubleAVG = total/room.getstulist (). Size (); System.out.println (No+ "--->" +total+ "+" +avg); } } /** Statistical analysis **/ Public StaticMap<string, classroom> count (list<student>list) {Map<string, classroom> map =NewHashmap<string,classroom>(); //1. Traverse for(Student stu:list) {//sorting to see if it exists, the number of the classString No =Stu.getno (); DoubleScore =Stu.getscore (); //If it does not exist, create a classClassroom =Map.get (NO); if(NULL==Guest) { the=NewClassroom (NO); Map.put (no, guest); } //exist, put into the studentroom.getstulist (). Add (Stu); Room.settotal (Room.gettotal ()+score); } returnmap; } /** Simulated test data to list **/ Public StaticList<student>Exam () {List<Student> list =NewArraylist<student>(); //Store Student scoresList.add (NewStudent ("Yzhou01", "a", 80)); List.add (NewStudent ("yzhou02", "a", 80)); List.add (NewStudent ("yzhou03", "a", 80)); List.add (NewStudent ("yzhou04", "B", 80)); List.add (NewStudent ("yzhou05", "B", 80)); List.add (NewStudent ("yzhou06", "B", 80)); returnlist; }; }
Object-oriented sortation storage in Java