TreeMap中键为Student,值为何类型字符串?

2026-04-16 14:583阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计287个文字,预计阅读时间需要2分钟。

TreeMap中键为Student,值为何类型字符串?

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.txt

package 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 tm = new TreeMap<>(new Comparator () { @Override public int compare(Student o1, Student o2) { int num = o1.getName().compareTo(o2.getName()); return num == 0 ? o1.getAge()- o2.getAge() : num; } }); tm.put(new Student("卓胜达",21),"陆丰"); tm.put(new Student("datou",22),"中国"); System.out.println(tm); } public static void demo1(){ TreeMap tm = new TreeMap<>(); tm.put(new Student("zjhpng",12),"背景"); tm.put(new Student("z",12),"zsd"); System.out.println(tm); } }

TreeMap中键为Student,值为何类型字符串?

本文共计287个文字,预计阅读时间需要2分钟。

TreeMap中键为Student,值为何类型字符串?

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.txt

package 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 tm = new TreeMap<>(new Comparator () { @Override public int compare(Student o1, Student o2) { int num = o1.getName().compareTo(o2.getName()); return num == 0 ? o1.getAge()- o2.getAge() : num; } }); tm.put(new Student("卓胜达",21),"陆丰"); tm.put(new Student("datou",22),"中国"); System.out.println(tm); } public static void demo1(){ TreeMap tm = new TreeMap<>(); tm.put(new Student("zjhpng",12),"背景"); tm.put(new Student("z",12),"zsd"); System.out.println(tm); } }

TreeMap中键为Student,值为何类型字符串?