TreeMap中键为Student,值为何类型字符串?
- 内容介绍
- 文章标签
- 相关推荐
本文共计287个文字,预计阅读时间需要2分钟。
javapackage day18;import java.util.Comparator;import java.util.TreeMap;
/** * 描述: * TreeMap集合的键是Student值是String的示例 * @author 71948 * @create 2017-10-06 22:37 */public class Demo7_TreeMap { public static void main(String[] args) { TreeMap map=new TreeMap(new Comparator() { @Override public int compare(Student o1, Student o2) { return o1.getName().compareTo(o2.getName()); } });
map.put(new Student(Alice), Student A); map.put(new Student(Bob), Student B); map.put(new Student(Charlie), Student C);
for (Student key : map.keySet()) { System.out.println(key.getName() + : + map.get(key)); } }}
class Student { private String name;
public Student(String name) { this.name=name; }
public String getName() { return name; }
@Override public boolean equals(Object o) { if (this==o) return true; if (o==null || getClass() !=o.getClass()) return false; Student student=(Student) o; return name !=null ? name.equals(student.name) : student.name==null; }
@Override public int hashCode() { return name !=null ? name.hashCode() : 0; }}
gistfile1.txtpackage day18;
import java.util.Comparator;
import java.util.TreeMap;
/**
* 描述:
* TreeMap集合键是Student值是String的案例
*
* @outhor 71948
* @create 2017-10-06 22:37
*/
public class Demo7_TreeMap {
public static void main(String[] args) {
TreeMap
本文共计287个文字,预计阅读时间需要2分钟。
javapackage day18;import java.util.Comparator;import java.util.TreeMap;
/** * 描述: * TreeMap集合的键是Student值是String的示例 * @author 71948 * @create 2017-10-06 22:37 */public class Demo7_TreeMap { public static void main(String[] args) { TreeMap map=new TreeMap(new Comparator() { @Override public int compare(Student o1, Student o2) { return o1.getName().compareTo(o2.getName()); } });
map.put(new Student(Alice), Student A); map.put(new Student(Bob), Student B); map.put(new Student(Charlie), Student C);
for (Student key : map.keySet()) { System.out.println(key.getName() + : + map.get(key)); } }}
class Student { private String name;
public Student(String name) { this.name=name; }
public String getName() { return name; }
@Override public boolean equals(Object o) { if (this==o) return true; if (o==null || getClass() !=o.getClass()) return false; Student student=(Student) o; return name !=null ? name.equals(student.name) : student.name==null; }
@Override public int hashCode() { return name !=null ? name.hashCode() : 0; }}
gistfile1.txtpackage day18;
import java.util.Comparator;
import java.util.TreeMap;
/**
* 描述:
* TreeMap集合键是Student值是String的案例
*
* @outhor 71948
* @create 2017-10-06 22:37
*/
public class Demo7_TreeMap {
public static void main(String[] args) {
TreeMap

