What you need is not a HashSet but a HashMap. Take a look at the program below and try to understand what exactly is going on here. I'll suggest you read how HashMap works before reading this.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class sky {
private static Map<String, Integer> planetMap = new HashMap<String,Integer>();
public static void main(String args[]) {
populateDB();
Scanner scanner = new Scanner(System.in);
String planetName = scanner.nextLine();
if(planetMap.get(planetName) != null) {
System.out.println("The planet "+ planetName +" was found in "+ planetMap.get(planetName));
}
else {
System.out.println("Invalid Planet Name");
}
}
public static void populateDB() {
planetMap.put("Earth", 1600);
planetMap.put("Mars", 1500);
planetMap.put("Jupiter", 1100);
planetMap.put("Saturn", 1900);
planetMap.put("Venus", 1300);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…