Student number 0-999 randomly generates test scores for each student (0-100 points), finds 10 best scores, and the student obtains the highest number of 5 points for the same score, and lists the student numbers for which the scores are obtained.

Source: Internet
Author: User

Package com.cn;

Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Comparator;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Map.Entry;
Import Java.util.Random;

/**
* Created by Sam on 2017/1/9.
*/
public class Toolclasspractice {
public static void Main (string[] args) {
Int[] score = new INT[101];
The number of occurrences of the collection---key appears, value is the number of occurrences
Map<integer, integer> map_times = new Hashmap<integer, integer> ();
The collection---Key for the Student object is the number, and the value is the fraction
Map<integer, integer> map_stu = new Hashmap<integer, integer> ();
Generate 1000 students
for (int i = 0; i <; i++) {
Random random = new random ();
Randomly generate fractions between 0-100 points
Int J = Random.nextint (101);
Count the number of occurrences of each score
score[j]++;
Map_stu.put (i, j);
}

for (int q = 0; q <=; q++) {
Map_times.put (q, Score[q]);
}

Toolclasspractice t = new Toolclasspractice ();
Student's map collection into the list collection
List<map.entry<integer, integer>> list_stu = new Arraylist<map.entry<integer, Integer>> ();
Number of occurrences Map collection converted to list collection
List<map.entry<integer, integer>> list_times = new Arraylist<map.entry<integer, Integer>> ();

To sort fractions
List_stu = T.sortmapbyvalue (Map_stu);
System.out.println ("10 Highest score:");
for (int a = 0; a <; a++) {
System.out.println (List_stu.get (a). GetValue ());
}

Sort the collection of times
List_times = T.sortmapbyvalue (map_times);

Get the top 5 number of times set
List<map.entry<integer, integer>> list_timestop5 = new Arraylist<map.entry<integer, Integer> > ();
for (int p = 0; p < 5; p++) {
List_timestop5.add (List_times.get (p));
}
Get the top five scores of students with the highest number of points in a collection
System.out.println ("The top five students with the highest scores:");
For (Map.entry<integer, integer> entrytimes:list_timestop5) {
int count = 0;
For (Map.entry<integer, integer> entry:list_stu) {
count++;
if (count = = 1) {
SYSTEM.OUT.PRINTLN ("---" + entrytimes.getkey () + "score has" + entrytimes.getvalue () + "person" + "---");
}

The scores of the Times set are equal to the scores of the students ' collections
if (entrytimes.getkey () = = Entry.getvalue ()) {
System.out.println ("School Number:" + entry.getkey () + "score:" + entry.getvalue ());
}
}
}
}

Sort by value for the map collection
Public List<map.entry<integer, Integer>> sortmapbyvalue (Map<integer, integer> Map) {
Convert map to list collection, list using ArrayList
List<map.entry<integer, integer>> list_data = new ArrayList <map.entry<integer, Integer>> ( Map.entryset ());
Sort by Collections.sort (List i,comparator C) method
Collections.sort (List_data, New Comparator<map.entry<integer, integer>> () {

@Override
public int Compare (Entry<integer, integer> O1, Entry<integer, integer> O2) {
Return (O2.getvalue ()-o1.getvalue ());
}
});
return list_data;
}
}

Student number 0-999 randomly generates test scores for each student (0-100 points), finds 10 best scores, and the student obtains the highest number of 5 points for the same score, and lists the student numbers for which the scores are obtained.

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.