用户输入值未添加到 hashmap
user input values are not added to hashmap
我有一个 hashmap,它有预先输入的值,我正在从用户那里获取更多值。然后这些值按升序排序。但是在这段代码中,用户输入的值没有被插入到 hashmap 中。
import java.sql.SQLOutput;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class CFG {
public static void main(String[] args) {
sortByValueJava8Stream();
}
private static void sortByValueJava8Stream()
{
Map<String, Integer> unSortedMap = getUnSortedMap();
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
unSortedMap.entrySet().stream().sorted(Map.Entry.comparingByValue())
.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
System.out.println("Sorted Map : " + sortedMap);
}
public static Map<String, Integer> getUnSortedMap() {
Scanner in = new Scanner(System.in);
HashMap<String, Integer> unsortMap = new HashMap<>();
unsortMap.put("Shiran", 342);
unsortMap.put("Hashini", 448);
unsortMap.put("Chanchala", 398);
unsortMap.put("Priyankara", 399);
unsortMap.put("Mayuri", 350);
unsortMap.put("Sameera", 321);
unsortMap.put("Supun", 299);
unsortMap.put("Supuni", 378);
unsortMap.put("Kavindu", 384);
unsortMap.put("Nadeeka", 440);
System.out.println("Do you want to add more players? ");
System.out.println("if yes pres 1 : ");
Integer c = in.nextInt();
if (c == 1) {
addUser();
} return unsortMap;
}
public static void addUser(){
Scanner in = new Scanner(System.in);
Map<String, Integer> unsortMap = new HashMap<>();
System.out.println("Enter name : ");
String a = in.nextLine();
in.nextLine();
System.out.println("Enter Time : ");
Integer b =in.nextInt();
unsortMap.put(a,b);
}
}
我创建了一个名为 adduser() 的方法并调用它来添加新值。但数据不会被它输入。我该如何解决这个问题?
您必须将 unSortedMap 传递给 addUser 方法。
public static Map<String, Integer> getUnSortedMap() {
Scanner in = new Scanner(System.in);
HashMap<String, Integer> unsortMap = new HashMap<>();
unsortMap.put("Shiran", 342);
unsortMap.put("Hashini", 448);
unsortMap.put("Chanchala", 398);
unsortMap.put("Priyankara", 399);
unsortMap.put("Mayuri", 350);
unsortMap.put("Sameera", 321);
unsortMap.put("Supun", 299);
unsortMap.put("Supuni", 378);
unsortMap.put("Kavindu", 384);
unsortMap.put("Nadeeka", 440);
System.out.println("Do you want to add more players? ");
System.out.println("if yes pres 1 : ");
Integer c = in.nextInt();
if (c == 1) {
addUser(unsortMap);
} return unsortMap;
}
public static void addUser(Map<String, Integer> unsortMap){
Scanner in = new Scanner(System.in);
System.out.println("Enter name : ");
String a = in.nextLine();
in.nextLine();
System.out.println("Enter Time : ");
Integer b =in.nextInt();
unsortMap.put(a,b);
}
一种方法是全局声明 unsortMap Map 以便它可以在其他方法中使用。
问题在于您的 addUser
方法,因为它创建了一个本地 unsortMap
,该方法在函数完成后从未使用过。
另一种方法是将 unsortMap Map 作为参数传递给 addUser
方法并放入所需的对
以下是修改后的工作代码。
class CFG
{
static Map<String, Integer> unsortMap; // global declaration of Map
public static void main(String[] args)
{
sortByValueJava8Stream();
}
private static void sortByValueJava8Stream()
{
Map<String, Integer>unSortedMap = getUnSortedMap();
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
unSortedMap.entrySet().stream().sorted(Map.Entry.comparingByValue())
.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
System.out.println("Sorted Map : " + sortedMap);
}
public static Map<String, Integer> getUnSortedMap()
{
Scanner in = new Scanner(System.in);
unsortMap = new HashMap<>(); // Initializing the Map
unsortMap.put("Shiran", 342);unsortMap.put("Hashini", 448);
unsortMap.put("Chanchala", 398);unsortMap.put("Priyankara", 399);
unsortMap.put("Mayuri", 350);unsortMap.put("Sameera", 321);
unsortMap.put("Supun", 299);unsortMap.put("Supuni", 378);
unsortMap.put("Kavindu", 384);unsortMap.put("Nadeeka", 440);
System.out.println("Do you want to add more players? ");
System.out.println("if yes pres 1 : ");
Integer c = in.nextInt();
if (c == 1)
addUser();
return unsortMap;
}
public static void addUser()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter name : ");
String a = in.nextLine();
System.out.println("Enter Time : ");
int b = in.nextInt();
unsortMap.put(a,b);
}
}
我有一个 hashmap,它有预先输入的值,我正在从用户那里获取更多值。然后这些值按升序排序。但是在这段代码中,用户输入的值没有被插入到 hashmap 中。
import java.sql.SQLOutput;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class CFG {
public static void main(String[] args) {
sortByValueJava8Stream();
}
private static void sortByValueJava8Stream()
{
Map<String, Integer> unSortedMap = getUnSortedMap();
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
unSortedMap.entrySet().stream().sorted(Map.Entry.comparingByValue())
.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
System.out.println("Sorted Map : " + sortedMap);
}
public static Map<String, Integer> getUnSortedMap() {
Scanner in = new Scanner(System.in);
HashMap<String, Integer> unsortMap = new HashMap<>();
unsortMap.put("Shiran", 342);
unsortMap.put("Hashini", 448);
unsortMap.put("Chanchala", 398);
unsortMap.put("Priyankara", 399);
unsortMap.put("Mayuri", 350);
unsortMap.put("Sameera", 321);
unsortMap.put("Supun", 299);
unsortMap.put("Supuni", 378);
unsortMap.put("Kavindu", 384);
unsortMap.put("Nadeeka", 440);
System.out.println("Do you want to add more players? ");
System.out.println("if yes pres 1 : ");
Integer c = in.nextInt();
if (c == 1) {
addUser();
} return unsortMap;
}
public static void addUser(){
Scanner in = new Scanner(System.in);
Map<String, Integer> unsortMap = new HashMap<>();
System.out.println("Enter name : ");
String a = in.nextLine();
in.nextLine();
System.out.println("Enter Time : ");
Integer b =in.nextInt();
unsortMap.put(a,b);
}
}
我创建了一个名为 adduser() 的方法并调用它来添加新值。但数据不会被它输入。我该如何解决这个问题?
您必须将 unSortedMap 传递给 addUser 方法。
public static Map<String, Integer> getUnSortedMap() {
Scanner in = new Scanner(System.in);
HashMap<String, Integer> unsortMap = new HashMap<>();
unsortMap.put("Shiran", 342);
unsortMap.put("Hashini", 448);
unsortMap.put("Chanchala", 398);
unsortMap.put("Priyankara", 399);
unsortMap.put("Mayuri", 350);
unsortMap.put("Sameera", 321);
unsortMap.put("Supun", 299);
unsortMap.put("Supuni", 378);
unsortMap.put("Kavindu", 384);
unsortMap.put("Nadeeka", 440);
System.out.println("Do you want to add more players? ");
System.out.println("if yes pres 1 : ");
Integer c = in.nextInt();
if (c == 1) {
addUser(unsortMap);
} return unsortMap;
}
public static void addUser(Map<String, Integer> unsortMap){
Scanner in = new Scanner(System.in);
System.out.println("Enter name : ");
String a = in.nextLine();
in.nextLine();
System.out.println("Enter Time : ");
Integer b =in.nextInt();
unsortMap.put(a,b);
}
一种方法是全局声明 unsortMap Map 以便它可以在其他方法中使用。
问题在于您的 addUser
方法,因为它创建了一个本地 unsortMap
,该方法在函数完成后从未使用过。
另一种方法是将 unsortMap Map 作为参数传递给 addUser
方法并放入所需的对
以下是修改后的工作代码。
class CFG
{
static Map<String, Integer> unsortMap; // global declaration of Map
public static void main(String[] args)
{
sortByValueJava8Stream();
}
private static void sortByValueJava8Stream()
{
Map<String, Integer>unSortedMap = getUnSortedMap();
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
unSortedMap.entrySet().stream().sorted(Map.Entry.comparingByValue())
.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
System.out.println("Sorted Map : " + sortedMap);
}
public static Map<String, Integer> getUnSortedMap()
{
Scanner in = new Scanner(System.in);
unsortMap = new HashMap<>(); // Initializing the Map
unsortMap.put("Shiran", 342);unsortMap.put("Hashini", 448);
unsortMap.put("Chanchala", 398);unsortMap.put("Priyankara", 399);
unsortMap.put("Mayuri", 350);unsortMap.put("Sameera", 321);
unsortMap.put("Supun", 299);unsortMap.put("Supuni", 378);
unsortMap.put("Kavindu", 384);unsortMap.put("Nadeeka", 440);
System.out.println("Do you want to add more players? ");
System.out.println("if yes pres 1 : ");
Integer c = in.nextInt();
if (c == 1)
addUser();
return unsortMap;
}
public static void addUser()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter name : ");
String a = in.nextLine();
System.out.println("Enter Time : ");
int b = in.nextInt();
unsortMap.put(a,b);
}
}