我如何根据他们的分数对学生的名字进行排序?

时间:2014-08-15 04:56:49

标签: java sorting hashmap

我如何根据他们的分数对学生的名字进行排序?提供学生的分数。我可以在java中使用Collection类进行排序吗?我应该import什么?

import java.util.*;

class ShortRecord {

   public static void main(String args[]) {

      // Create a hash map
      HashMap hm = new HashMap();
      // Put elements to the map
      hm.put("Zara", new Double(3434.34));
      hm.put("Mahnaz", new Double(123.22));
      hm.put("Ayan", new Double(1378.00));
      hm.put("Daisy", new Double(99.22));
      hm.put("Qadir", new Double(-19.08));

      // Get a set of the entries
      Set set = hm.entrySet();
      // Get an iterator
      Iterator i = set.iterator();
      // Display elements
      while (i.hasNext()) {
         System.out.print(me.getKey() + ": ");
         System.out.println(me.getValue());
      }

   }
}

3 个答案:

答案 0 :(得分:3)

如果您想按地图关键字(即姓名)排序,请使用java.util.TreeMap

如果您想根据值进行排序,请参阅Sort a Map<Key, Value> by values (Java)

答案 1 :(得分:2)

我认为您应该创建一个POJO Student类,并实施Comparable<Student> - 然后将这些Student放入List,然后Collections.sort()。所以,像

static class Student implements Comparable<Student> {
  public Student(String name, double grade) {
    this.name = name;
    this.grade = grade;
  }

  String name;
  double grade;

  @Override
  public int compareTo(Student o) {
    if (o == null) {
      return -1;
    }
    int c = Double.valueOf(grade).compareTo(o.grade);
    if (c != 0) {
      return c;
    }
    return name.compareTo(o.name);
  }

  @Override
  public String toString() {
    return String.format("%s has grade %.2f", name, grade);
  }
}

public static void main(String[] args) {
  List<Student> al = new ArrayList<>();
  al.add(new Student("Zara", 3434.34));
  al.add(new Student("Mahnaz", 123.22));
  al.add(new Student("Ayan", 1378.00));
  al.add(new Student("Daisy", 99.22));
  al.add(new Student("Qadir", -19.08));
  Collections.sort(al);
  System.out.println(al);
}

输出(格式化) -

[Qadir has grade -19.08, Daisy has grade 99.22, 
 Mahnaz has grade 123.22, Ayan has grade 1378.00, 
 Zara has grade 3434.34]

答案 2 :(得分:1)

这次使用自定义比较器的另一个例子:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Marks
{
    static class SortByMark implements Comparator<Student>
    {
        @Override
        public int compare(Student studentOne, Student studentTwo)
        {
            return studentOne.mark.compareTo(studentTwo.mark);
        }
    }

    static class Student
    {
        private final Double mark;
        private final String name;

        public Student(String name, Double mark)
        {
            this.name = name;
            this.mark = mark;
        }

        public Double getMark()
        {
            return mark;
        }

        public String getName()
        {
            return name;
        }

        @Override
        public String toString()
        {
            return name + ": " + mark;
        }
    }

    private final static SortByMark sortByMark = new SortByMark();;

    public static void main(String args[])
    {
        List<Student> students = new ArrayList<Student>();

        students.add(new Student("Zara", new Double(3434.34)));
        students.add(new Student("Mahnaz", new Double(123.22)));
        students.add(new Student("Ayan", new Double(1378.00)));
        students.add(new Student("Daisy", new Double(99.22)));
        students.add(new Student("Qadir", new Double(-19.08)));

        Collections.sort(students, sortByMark);

        for (Student student : students)
        {
            System.out.println(student);
        }
    }
}