我想问一下如何使用arraylist
打印我的map.get().get()
?输出应为:
"Aguinaldo, Marcos, Quezon"
打印方式应该是:
System.out.println(map.get("Philippines").get("President"));
除了这个之外,我没有对其他输出产生任何问题。
这是我的主要代码:
import java.util.*;
public class PresMain{
public static void main(String args[]){
HashMap<String, HashMap<String, Object>> map = new HashMap<String, HashMap<String, Object>>();
map.put("Philippines", new HashMap<String, Object>());
map.get("Philippines").put("capital", "Manila");
map.get("Philippines").put("continent", "Asia");
System.out.println(map.get("Philippines").get("capital"));
System.out.println(map.get("Philippines").get("continent"));
List<President> list = new ArrayList<President>();
list.add(new President("Aguinaldo", 1));
list.add(new President("Marcos", 10));
list.add(new President("Quezon", 2));
Collections.sort(list);
for(President a: list)
System.out.print(a.getPresName() + ", ");
Collections.sort(list, new President());
System.out.println(" ");
for(President a: list)
System.out.println(a.getPresName() +" , "+ a.getPresYear());
}
}
很抱歉,我们之前没有包括我的其他课程,所以这里是:
enter code here
import java.util.*;
class President implements Comparator<President>, Comparable<President>{
private String name;
private int year;
int x;
President(){
}
President(String n, int a){
name = n;
year = a;
}
public String getPresName(){
return name;
}
public int getPresYear(){
return year;
}
public int getPresX(){
return x;
}
// Overriding the compareTo method; sort(list)
public int compareTo(President d){
//return (this.name).compareTo(d.name);
return this.year - d.year;
}
// Overriding the compare method to sort the age
public int compare(President d, President d1){
//return (d.name).compareTo(d1.name);
// return d.age - d1.age;
return d.x - d1.x;
}
}
答案 0 :(得分:0)
尝试类似:
public class PresMain{
public static void main(String args[]){
HashMap<String, HashMap<String, Object>> map = new HashMap<String, HashMap<String, Object>>();
map.put("Philippines", new HashMap<String, Object>());
map.get("Philippines").put("capital", "Manila");
map.get("Philippines").put("continent", "Asia");
System.out.println(map.get("Philippines").get("capital"));
System.out.println(map.get("Philippines").get("continent"));
List<President> list = new ArrayList<President>();
list.add(new President("Aguinaldo", 1));
list.add(new President("Marcos", 10));
list.add(new President("Quezon", 2));
//Don't know why you're doing this, have to sort them? Then President class needs to implement Comparator Interface.
//Collections.sort(list);
//for(President a: list)
// System.out.print(a.getPresName() + ", ");
//Collections.sort(list, new President());
//System.out.println(" ");
//for(President a: list)
// System.out.println(a.getPresName() +" , "+ a.getPresYear());
// }
map.get("Philippines".put("president", list);
//To get this working you may override the toString method in your presidents list.
System.out.println(map.get("Philippines").get("president").toString());
}
这是你正在寻找的吗?
答案 1 :(得分:0)
这条线甚至不起作用:
Collections.sort(list, new President());
如果有的话,President
应该实施Comparable<T>
http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
How to implement the Java comparable interface?
或者你应该给它一个Comparator<T>
实现,如下所示:
http://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
How to use Comparator in Java to sort
之后,代码应按预期工作。
答案 2 :(得分:0)
如果你想打印总统姓名和总统年份排序:
List<President> list = new ArrayList<President>();
list.add(new President("Aguinaldo", 1));
list.add(new President("Marcos", 10));
list.add(new President("Quezon", 2));
Collections.sort(list, new President());
map.get("Philippines").put("presidents", list);
for(Entry<String, HashMap<String, Object>> entry: map.getEntrySet()){
HasMap<String, Object> attributes = entry.getValue();
List<President> pList = (List<President>) attributes.get("presidents");
for(President p: pList) System.out.print(p.getPresName() + ", " + p.getYear() + " ");
}
输出是:
"Aguinaldo, 1 Quezon, 2 Marcos, 10"
你必须做检查,以确保你没有得到NPE(比如,总有一个“总统”钥匙?等等。)
通过这种方式,您有Map
,其中包含Map
,其中包含List
。这有点乱。你为什么不使用Java类?
public class State{
String name;
String capital;
List<President> presidents;
public State(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCapital() {
return capital;
}
public void setCapital(String capital) {
this.capital = capital;
}
public List<String> getPresidents() {
return presidents;
}
}
在你的主要:
State philippines = new State("Philippines");
philippines.setCapital("Manila");
philippines.getPresidents().add(new President("Aguinaldo", 1));
philippines.getPresidents().add(new President("Marcos", 10));
philippines.getPresidents().add(new President("Quezon", 2));
Collections.sort(philippines.getPresidents(), new President());
for(President p: philippines.getPresidents())
System.out.print(p.getPresName());
存储你的州:
Map<String, State> states = new HashMap<String, State>();
//create philippines
states.add("Philippines", philippines);
Collections.sort(states.get("Philippines").getPresidents(), new President());
for(President p: states.get("Philippines").getPresidents())
System.out.print(p.getPresName());