天天看点

Students

package step1;
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 Students {
	private List<Student> stuList;
	private Map<Integer,Student> stuMap;
	public Students(List<Student> stuList) {
		super();
		this.stuList = stuList;
	}
	public Students(Map<Integer, Student> stuMap) {
		super();
		this.stuMap = stuMap;
	}
	public Students() {
		super();
	}
	public List<Student> getByAvgScore(double score) {
		List<Student> reList = new ArrayList<Student>();
		Student students[] = new Student[stuList.size()];
		int cont=0;
		for(Student s:stuList) {
			if(s.getAvgScore()==score) {
				students[cont]=s;
				cont++;
			}
		}
		for(int i=0;i<cont-1;i++)
        {
            for(int j=0;j<cont-1-i;j++)
            {
                if(students[j].getAge()>students[j+1].getAge())
                {
                    Student temp=students[j];
                    students[j]=students[j+1];
                    students[j+1]=temp;
                }
            }
        }
		for(int i=0;i<cont;i++) {
			reList.add(students[i]);
		}
		return reList;
	}
	public Map<Double, List<Student>> getByAvgScores(){
		Map<Double,List<Student>> reMap=new HashMap<>();
		double avgArray[] = new double[stuList.size()];
		int cont=0;
		Set<Map.Entry<Integer, Student>> entrySet = stuMap.entrySet(); 
		for(Map.Entry<Integer, Student> item : entrySet) {
			double temp = item.getValue().getAvgScore();
			boolean flag=true;
			for(int i = 0;i<cont;i++) {
				if(avgArray[i]==temp) flag=false;
			}
			if(flag) {
				avgArray[cont]=temp;
				cont++;
			}
		}
		for(double temp:avgArray) {
			reMap.put(temp, getByAvgScore(temp));
		}
		return reMap;
	}
}