根據您的要求,您可以創建一個Planet
類和/或使用Hashmap作爲另一個Hashmap的值。
至於階級來說,已經有介紹如何做到這一點的答案,反正會給你下面一個例子:
class Planet {
String name;
String gravityStrength;
String universe;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGravityStrength() {
return gravityStrength;
}
public void setGravityStrength(String gravityStrength) {
this.gravityStrength = gravityStrength;
}
public String getUniverse() {
return universe;
}
public void setUniverse(String universe) {
this.universe= universe;
}
}
然後你可以有你的Hashmap與鍵名跟隨地球(字符串)和值作爲地球的對象:
HashMap<String, Planet> planets = new HashMap<String, Planet>();
Planet planetEarth = new Planet();
planetEarth.setName("Earth");
planetEarth.setGravityStrength("X");
planetEarth.setUniverse("The Milky Way");
planets.put("earth", planetEarth);
或者你可以有以下hasmap,其中關鍵是這個星球作爲字符串的名稱,而值是另一個HashMap中是需要的星球細節名作爲關鍵字和行星細節值作爲值(兩個字符串)。
HashMap<String, HashMap<String, String>> planets = new HashMap<String, HashMap<String, String>>();
HashMap<String, String> earth = new HashMap<String, String>();
earth.put("name", "Earth");
earth.put("gravityStrength": "X4");
earth.put("universe", "The Milky Way");
planets.put("earth", earth);
以下是Planet
類實現一個什麼樣的代碼做什麼。我已經重寫了Planet類中的toString
()以允許Planet對象的漂亮打印。
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Sky{
private static Map<String, Planet> planetMap = new HashMap<String, Planet>();
public static void main(String args[]) {
populateDB();
Scanner scanner = new Scanner(System.in);
System.out.print("Please input planet name: ");
String planetName = scanner.nextLine();
//converting to lowercase so user input Earth and earth and EARTH eArTH all match
if (planetMap.containsKey(planetName.toLowerCase())) {
//invokes the overrided toString() of Planet class
System.out.println(planetMap.get(planetName.toLowerCase()).toString());
} else {
System.out.println("Invalid Planet Name");
}
}
public static void populateDB() {
Planet earth = new Planet("Earth", "4x", 1600);
Planet mars = new Planet("Mars", "1x", 1500);
Planet jupiter = new Planet("Jupiter", "3x", 1100);
Planet saturn = new Planet("Saturn", "16x", 1900);
Planet venus = new Planet("Venus", "5x", 1300);
planetMap.put("earth", earth);
planetMap.put("mars", mars);
planetMap.put("jupiter", jupiter);
planetMap.put("saturn", saturn);
planetMap.put("venus", venus);
}
}
class Planet {
String name;
String gravityStrength;
int found;
//empty constructor
public Planet() {}
public Planet(String name, String gravityStrength, int found) {
this.name = name;
this.gravityStrength = gravityStrength;
this.found = found;
}
/* Getters and setters */
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGravityStrength() {
return gravityStrength;
}
public void setGravityStrength(String gravityStrength) {
this.gravityStrength = gravityStrength;
}
public int getFound() {
return found;
}
public void setUniverse(int found) {
this.found = found;
}
//override toString methdo to pretty-print planet
public String toString() {
return "Planet " + name + " with gravity strenght " + gravityStrength + " was found in " + found;
}
}
如果你不喜歡Planet
類實現,那麼你可以隨時使用下面的Hashmap裏面執行的Hashmap:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Sky {
private static Map<String, HashMap<String, String>> planetMap = new HashMap<String, HashMap<String, String>>();
public static void main(String args[]) {
populateDB();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter planet name to search for: ");
String planetName = scanner.nextLine();
if (planetMap.containsKey(planetName.toLowerCase())) {
HashMap<String, String> p = planetMap.get(planetName.toLowerCase());
System.out.println("The planet " + planetName + " was found in " + p.get("name")
+ " and has gravity strenght of " + p.get("gravityStrength"));
} else {
System.out.println("Invalid Planet Name");
}
}
public static void populateDB() {
HashMap<String, String> earthDetails = new HashMap<String, String>();
earthDetails.put("name", "Earth");
earthDetails.put("gravityStrength", "4X");
earthDetails.put("found", "1600");
HashMap<String, String> marsDetails = new HashMap<String, String>();
marsDetails.put("name", "Mars");
marsDetails.put("gravityStrength", "31X");
marsDetails.put("found", "1500");
HashMap<String, String> jupiterDetails = new HashMap<String, String>();
jupiterDetails.put("name", "Jupiter");
jupiterDetails.put("gravityStrength", "1X");
jupiterDetails.put("found", "1100");
HashMap<String, String> saturnDetails = new HashMap<String, String>();
saturnDetails.put("name", "Saturn");
saturnDetails.put("gravityStrength", "100X");
saturnDetails.put("found", "1900");
HashMap<String, String> venusDetails = new HashMap<String, String>();
venusDetails.put("name", "Venus");
venusDetails.put("gravityStrength", "150X");
venusDetails.put("found", "1300");
planetMap.put("earth", earthDetails);
planetMap.put("mars", marsDetails);
planetMap.put("jupiter", jupiterDetails);
planetMap.put("saturn", saturnDetails);
planetMap.put("venus", venusDetails);
}
}
對於每一個星球,而不是添加的日期,添加另一個哈希,其中鍵值對保存附加數據:'earthFacts.put(「found」,1600「); planetMap.put(」Earth「,earthFacts);'。 –
@MichaWiedenmann對於這個地圖的地圖可能有點矯枉過正,當我想到一個地圖時,我想到的是關鍵/對象對,其中對象的值是相似的。行星被找到的日期,行星的名字以及行星的位置都是不是很相似 – phflack
[早期問題](http://stackoverflow.com/questions/34025379/joining-both-hashsets-together) – phflack