//创建一个类A,里面有这样三个属性
//int id;
//String name;
//int age;
//将N个A的对象放进hashmap中。
//现要求分别按id,name和age从小到大顺序输出。这么个问题 可以用3个TreeMap实现
但是如果id name age 中出现有重复得 id name age 这样放入hashmap键中 就会覆盖其他的
有什么更好的方法实现这个题呢
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;public class Testpa {
public static void main(String[] args) {
test1();
// test2();
//test3();
}
private static void test3() {
Map ma = new HashMap();
Map ma1 = new TreeMap();
Testp a1 = new Testp(21,"tom",22);
Testp a2 = new Testp(33,"jack",23);
Testp a3 = new Testp(44,"peter",44);
Testp a4 = new Testp(55,"anna",34);
Testp a5 = new Testp(66,"jerry",65);
Testp a6 = new Testp(33,"mike",122);
ma.put(a1.getName(),a1.getId()+"\t"+a1.getName()+"\t"+a1.getAge());
ma.put(a2.getName(),a2.getId()+"\t"+a2.getName()+"\t"+a2.getAge());
ma.put(a3.getName(),a3.getId()+"\t"+a3.getName()+"\t"+a3.getAge());
ma.put(a4.getName(),a4.getId()+"\t"+a4.getName()+"\t"+a4.getAge());
ma.put(a5.getName(),a5.getId()+"\t"+a5.getName()+"\t"+a5.getAge());
ma.put(a6.getName(),a6.getId()+"\t"+a6.getName()+"\t"+a6.getAge());
ma1.putAll(ma);
for (Object ss : ma1.values()) {
System.out.println(ss);
}
}
private static void test2() {
Map ma = new HashMap();
Map ma1 = new TreeMap();
Testp a1 = new Testp(21,"tom",22);
Testp a2 = new Testp(33,"jack",23);
Testp a3 = new Testp(44,"peter",44);
Testp a4 = new Testp(55,"anna",34);
Testp a5 = new Testp(66,"jerry",65);
Testp a6 = new Testp(33,"mike",122);
ma.put(a1.getId(),a1.getId()+"\t"+a1.getName()+"\t"+a1.getAge());
ma.put(a2.getId(),a2.getId()+"\t"+a2.getName()+"\t"+a2.getAge());
ma.put(a3.getId(),a3.getId()+"\t"+a3.getName()+"\t"+a3.getAge());
ma.put(a4.getId(),a4.getId()+"\t"+a4.getName()+"\t"+a4.getAge());
ma.put(a5.getId(),a5.getId()+"\t"+a5.getName()+"\t"+a5.getAge());
ma.put(a6.getId(),a6.getId()+"\t"+a6.getName()+"\t"+a6.getAge());
ma1.putAll(ma);
for (Object ss : ma1.values()) {
System.out.println(ss);
}
}
private static void test1() {
Map ma = new HashMap();
Map ma1 = new TreeMap();
Testp a1 = new Testp(21,"tom",22);
Testp a2 = new Testp(33,"jack",23);
Testp a3 = new Testp(44,"peter",44);
Testp a4 = new Testp(55,"anna",34);
Testp a5 = new Testp(66,"jerry",65);
Testp a6 = new Testp(33,"mike",122);
ma.put(a1.getAge(),a1.getId()+"\t"+a1.getName()+"\t"+a1.getAge());
ma.put(a2.getAge(),a2.getId()+"\t"+a2.getName()+"\t"+a2.getAge());
ma.put(a3.getAge(),a3.getId()+"\t"+a3.getName()+"\t"+a3.getAge());
ma.put(a4.getAge(),a4.getId()+"\t"+a4.getName()+"\t"+a4.getAge());
ma.put(a5.getAge(),a5.getId()+"\t"+a5.getName()+"\t"+a5.getAge());
ma.put(a6.getAge(),a6.getId()+"\t"+a6.getName()+"\t"+a6.getAge());
ma1.putAll(ma);
for (Object ss : ma1.values()) {
System.out.println(ss);
}
}
}
--------------public class Testp {
private int id;
private String name;
private int age; public Testp(int a,String n,int i) {
this.age = a;
this.name = n;
this.id = i;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
我暂时只想出这么写
//int id;
//String name;
//int age;
//将N个A的对象放进hashmap中。
//现要求分别按id,name和age从小到大顺序输出。这么个问题 可以用3个TreeMap实现
但是如果id name age 中出现有重复得 id name age 这样放入hashmap键中 就会覆盖其他的
有什么更好的方法实现这个题呢
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;public class Testpa {
public static void main(String[] args) {
test1();
// test2();
//test3();
}
private static void test3() {
Map ma = new HashMap();
Map ma1 = new TreeMap();
Testp a1 = new Testp(21,"tom",22);
Testp a2 = new Testp(33,"jack",23);
Testp a3 = new Testp(44,"peter",44);
Testp a4 = new Testp(55,"anna",34);
Testp a5 = new Testp(66,"jerry",65);
Testp a6 = new Testp(33,"mike",122);
ma.put(a1.getName(),a1.getId()+"\t"+a1.getName()+"\t"+a1.getAge());
ma.put(a2.getName(),a2.getId()+"\t"+a2.getName()+"\t"+a2.getAge());
ma.put(a3.getName(),a3.getId()+"\t"+a3.getName()+"\t"+a3.getAge());
ma.put(a4.getName(),a4.getId()+"\t"+a4.getName()+"\t"+a4.getAge());
ma.put(a5.getName(),a5.getId()+"\t"+a5.getName()+"\t"+a5.getAge());
ma.put(a6.getName(),a6.getId()+"\t"+a6.getName()+"\t"+a6.getAge());
ma1.putAll(ma);
for (Object ss : ma1.values()) {
System.out.println(ss);
}
}
private static void test2() {
Map ma = new HashMap();
Map ma1 = new TreeMap();
Testp a1 = new Testp(21,"tom",22);
Testp a2 = new Testp(33,"jack",23);
Testp a3 = new Testp(44,"peter",44);
Testp a4 = new Testp(55,"anna",34);
Testp a5 = new Testp(66,"jerry",65);
Testp a6 = new Testp(33,"mike",122);
ma.put(a1.getId(),a1.getId()+"\t"+a1.getName()+"\t"+a1.getAge());
ma.put(a2.getId(),a2.getId()+"\t"+a2.getName()+"\t"+a2.getAge());
ma.put(a3.getId(),a3.getId()+"\t"+a3.getName()+"\t"+a3.getAge());
ma.put(a4.getId(),a4.getId()+"\t"+a4.getName()+"\t"+a4.getAge());
ma.put(a5.getId(),a5.getId()+"\t"+a5.getName()+"\t"+a5.getAge());
ma.put(a6.getId(),a6.getId()+"\t"+a6.getName()+"\t"+a6.getAge());
ma1.putAll(ma);
for (Object ss : ma1.values()) {
System.out.println(ss);
}
}
private static void test1() {
Map ma = new HashMap();
Map ma1 = new TreeMap();
Testp a1 = new Testp(21,"tom",22);
Testp a2 = new Testp(33,"jack",23);
Testp a3 = new Testp(44,"peter",44);
Testp a4 = new Testp(55,"anna",34);
Testp a5 = new Testp(66,"jerry",65);
Testp a6 = new Testp(33,"mike",122);
ma.put(a1.getAge(),a1.getId()+"\t"+a1.getName()+"\t"+a1.getAge());
ma.put(a2.getAge(),a2.getId()+"\t"+a2.getName()+"\t"+a2.getAge());
ma.put(a3.getAge(),a3.getId()+"\t"+a3.getName()+"\t"+a3.getAge());
ma.put(a4.getAge(),a4.getId()+"\t"+a4.getName()+"\t"+a4.getAge());
ma.put(a5.getAge(),a5.getId()+"\t"+a5.getName()+"\t"+a5.getAge());
ma.put(a6.getAge(),a6.getId()+"\t"+a6.getName()+"\t"+a6.getAge());
ma1.putAll(ma);
for (Object ss : ma1.values()) {
System.out.println(ss);
}
}
}
--------------public class Testp {
private int id;
private String name;
private int age; public Testp(int a,String n,int i) {
this.age = a;
this.name = n;
this.id = i;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
我暂时只想出这么写
解决方案 »
- 请教高手一个非常基础但是很困惑的问题
- struts+hibernate中文乱码问题,分不够就加!
- 帮忙看看那里错了!!!
- 昨晚在看think in java的内部类的时候,里面介绍了内部类实现钩子,看得一知半解,请问大家对钩子有了解吗?
- 问一个阵列问题
- 請教 OpenPDF 轉換為.NET 使用的DLL後,關于CMap文件夾裏面的字體信息的路徑問題 謝謝!
- 字体有锯齿的问题。。。急!!
- Java里面有没有象delphi中的TeeChart一样的现成的东东,可以同数据库表连接,生成饼、线、柱图?
- 求 java 酷站点
- java类 内部成员类的赋值和引用问题
- protected 和 default 修饰的方法和变量能被不同包的类继承或访问么????
- 关于Math类
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;/**
* @author Easteq China-Offshore
*/
public class TestMapSort {
private static void test3() {
List list = new ArrayList();
Testp a1 = new Testp(21, "tom", 22);
Testp a2 = new Testp(33, "jack", 23);
Testp a3 = new Testp(44, "peter", 44);
Testp a4 = new Testp(55, "anna", 34);
Testp a5 = new Testp(66, "jerry", 65);
Testp a6 = new Testp(33, "mike", 122);
list.add(a1);list.add(a2);list.add(a3);list.add(a4);list.add(a5);list.add(a6);
// for (Object ss : ma1.values()) {
// System.out.println(ss);
// } // sort by id
Collections.sort(list, new Comparator<Testp>() {
/**
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Testp o1, Testp o2) {
return o1.getId() - o2.getId();
}
}
);
print("ID", list);
// sort by name
Collections.sort(list, new Comparator<Testp>() {
/**
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Testp o1, Testp o2) {
return o1.getName().compareTo(o2.getName());
}
}
);
print("NAME" , list);
// sort by age
Collections.sort(list, new Comparator<Testp>() {
/**
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Testp o1, Testp o2) {
return o1.getAge() - o2.getAge();
}
}
); print("Age",list);
}
public static void main(String[] args) {
test3();
}
private static void print(String sortBy, List<Testp> list) {
System.out.println("Sort by " + sortBy + "::::::::::::::::::::::::");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
Testp@14318bb
Testp@ca0b6
Testp@10b30a7
Testp@1a758cb
Testp@1b67f74
Testp@69b332
Sort by NAME::::::::::::::::::::::::
Testp@10b30a7
Testp@ca0b6
Testp@1b67f74
Testp@69b332
Testp@1a758cb
Testp@14318bb
Sort by Age::::::::::::::::::::::::
Testp@14318bb
Testp@ca0b6
Testp@69b332
Testp@1a758cb
Testp@10b30a7
Testp@1b67f74怎么是这样得?
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "[id:" + id + ",name=" + name + ",age=" + age + "]";
}