1. 程式人生 > 其它 >三、分揀儲存--分組歸類

三、分揀儲存--分組歸類

一、將單詞統計出來
package mypro01; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; //分揀儲存--》分類歸類 public class MapDemo01 { public static void main(String[] args) { //分割字串 String[] arr="this is a cat and that is a nice is dang jing is ".split(" "); //分揀儲存 Map<String,Integer> map=new HashMap<String,Integer>(); for(String key:arr) { if(!map.containsKey(key)) { map.put(key, 1); }else { map.put(key, map.get(key)+1); } } //檢視每個單詞出現的次數 Set<String> ketSet = map.keySet(); Iterator<String> it = ketSet.iterator(); while(it.hasNext()) { String key = it.next(); Integer value=map.get(key); System.out.println("key:"+key+"----->value:"+value); } } }

二、面向物件儲存

 

package mypro01;

public class Student {
    private     String name;
    private  String id;
    private  double score;
    
    public Student(String name, String id, double score) {
        super();
        this.name = name;
        this.id = id;
        this.score = score;
    }
    
public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public double getScore() { return score; }
public void setScore(double score) { this.score = score; } }
package mypro01;

import java.util.ArrayList;
import java.util.List;

public class ClassRoom {
    private String no;
    private List<Student> stuList;
    private double total;
    public ClassRoom(String no, List<Student> stuList, double total) {
        super();
        this.no = no;
        this.stuList = stuList;
        this.total = total;
    }
    public String getNo() {
        return no;
    }
    public void setNo(String no) {
        this.no = no;
    }
    public List<Student> getStuList() {
        return stuList;
    }
    public void setStuList(List<Student> stuList) {
        this.stuList = stuList;
    }
    public double getTotal() {
        return total;
    }
    public void setTotal(double total) {
        this.total = total;
    }
    public ClassRoom() {
        stuList=new ArrayList<Student>();
        
    }
    public ClassRoom(String no) {
        this();
        this.no=no;
    }
}
package mypro01;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MapDemo02 {
    public static void main(String[] args) {
        //靠手
        List<Student> stulist=exam();
        Map<String,ClassRoom> map=count(stulist);
        view(map);
    }
    //計算分數
    public static void view(Map<String,ClassRoom> map) {
        Set<String> keyset=map.keySet();
        Iterator<String> itkey=keyset.iterator();
        while(itkey.hasNext()) {
            String no=itkey.next();
            ClassRoom room=map.get(no);
            double total=room.getTotal();
            double avg=total/room.getStuList().size();
            System.out.println(no+"--->"+total+"--->"+avg);
        }
    }
    
    //分類
    public static Map<String,ClassRoom> count(List<Student> list){
        Map<String,ClassRoom> map=new HashMap<String,ClassRoom>();
        for(Student stu:list) {
            String no=stu.getId();
            double score=stu.getScore();
            ClassRoom room=map.get(no);
            if(null==room) {
                room =new ClassRoom(no);
                map.put(no, room);
            }
            
            room.getStuList().add(stu);
            room.setTotal(room.getTotal()+score);
        }
        
        return map;
    }
    
    //模擬考試成績到其中
    public static List<Student> exam(){
        List<Student> list=new ArrayList<Student>();
        
        list.add(new Student("張三","1",80));
        list.add(new Student("李四","2",40));
        list.add(new Student("王五","1",44));
        list.add(new Student("馬六","2",22));
        list.add(new Student("馬2","3",22));
        
        return list;
    }
}
結果:
1--->124.0--->62.0 2--->62.0--->31.0 3--->22.0--->22.0