1
我必须保存一个集合并访问它(从一个循环),但我找不到一个方法来做它..任何建议,我的程序..我刚开始学习Java 。由于保存一个循环的集合
public class Main {
public static void main(String[] args) {
// my hashmap
Map<String, List<String>> test_01 = new HashMap<>();
Scanner scanner = new Scanner(System.in);
String answer;
List<String> name = new ArrayList<>();
List<String> time = new ArrayList<>();
String lake_Name, lake_time;
do {
System.out.println("Enter the name of the lake : ");
lake_Name = scanner.nextLine();
name.add(lake_Name);
System.out.println("Enter the name of the running time : ");
lake_time = scanner.nextLine();
time.add(lake_time);
//I'm doing this just in case my user does not enter a number
System.out.println("Do you want add another lake : \"Y\" for Yes OR another key to see the statistic the problem ");
answer = scanner.nextLine();
} while (answer.equalsIgnoreCase("Y"));
//We can continue here to do more stuff
for (int i = 0; i < name.size(); i++) {
putObjects(test_01, name.get(i), time.get(i));
}
/*Collection<String> x = test_01.get("x");
System.out.println(x);*/
// to get unique elements
Set<String> treesetList = new TreeSet<String>(name);
// to iterate again
List<String> list = new ArrayList<> (treesetList);
for (String s : treesetList){
Collection<String> uno = test_01.get(s);
System.out.println(uno);
}
}
private static void putObjects (Map<String, List<String>> a, String key, String value) {
List<String> myClassList = a.get(key);
if(myClassList == null) {
myClassList = new ArrayList<>();
a.put(key, myClassList);
}
myClassList.add(value);
}
}
我的问题是在这里
for (String s : treesetList){
Collection<String> uno = test_01.get(s);
System.out.println(uno);
}
因为我必须从地图查看在不同湖泊的名称和它的汇集在“test_01” ......我不知道如何保存到diferent有不同名称的数组(或创建它们),以便稍后检查它的时间....稍后将集合转换为“double”...
谢谢。我会尝试你的回答:) – hbrtxito