gpt4 book ai didi

java - 内部 map 具有多个键值对的 map 内部的 map

转载 作者:行者123 更新时间:2023-12-01 16:43:27 24 4
gpt4 key购买 nike

我获取CSV文件的值,但在内部映射中映射值时遇到困难。

CSV文件如下所示:

The scanned CSV file

对于每个EmpID,都有一个FirstName,一个LastName以及Groupmbr的多个条目。

对于特定的EmpId201),我试图获得如下输出:

{
201= {
EmpID= 201,
FirstName= Sravan,
LastName= Jabbala,
Groupmbr= [
PayrollAccess,
AcctspayableAccess,
AcctsReceivableAccess2
]
}
}


外部 Map应该包含每个 EmpId(键)和一个内部地图,其中包含有关雇员的详细信息:例如 key=FirstNamevalue=Sravan

这就是我读取CSV文件的方式。

code [代码段]

如何填写地图,以便获得理想的结果?

最佳答案

如果我对所需输出的假设是正确的,那么这可以帮助您:

结构如下:

{                                 // ---. the outer map with all EmpIds as keys
201= { // |---. for each EmpId, an inner map with the employee's data
EmpID= 201, // | | -- an Integer
FirstName= Sravan, // | | -- some String
LastName= Jabbala, // | | -- some String
Groupmbr= [ // | |--. a List of Strings
PayrollAccess, // | | |
AcctspayableAccess, // | | |
AcctsReceivableAccess2 // | | |
] // | |--´
} // |---´
} // ---´


因此,您的 map2(我建议您给它一个更有意义的名称)具有以下签名:

Map<String, Map<String, Object>>  // this is when you want to cast the EmpID to a String 
Map<Integer, Map<String, Object>> // here you could keep the id as number might be easier to work with?)


让我们在本示例中使用第二个版本。第一个整数是 EmpID,而 Map<String, Object>将是员工的数据。如您所知,数据可以是 Integer, String甚至是 List,因此在此“内部映射”中,我们将仅保留“对象”并稍后处理它们的类型。

现在,我们将阅读CSV:

Scanner s = new Scanner(new File("c:\path\to\your\file.csv");  // this is easier than the buffered reader and almost works the same
s.nextLine(); // we read the header (first line) of the csv - and forget it right away

while (s.hasNextLine()) { // as long as we didn't reach the end of the file, we continue reading line by line
String line = s.nextLine(); // we read the next line
String[] employeeData = line.split(';');

Integer empId = Integer.valueOf(employeeData[0]); // this will read the EmpId as Integer
if(empId != empIdSearchedByUser){ // if I assume correctly, you want only the employee that corresponds to the user's input
continue; // that's a fail first anchor :)
}

Map<String, Object> employee; // we just tell java that we'll work with another map... but we don't know yet if it already exists or not
if(map2.containsKey(empId){ // in this case the empId is already present (when the same employee in in multiple groups)
employee = map2.get(empId); // so we get the existing employee
} else {
employee = new HashMap<>(); // otherwise we create a new inner map (for each EmpID/line read we will create a new inner map
map2.put(empId, employee); // and add the inner map to our main map
}

// the following three lines are redundant and don't need to be
// executed every time for each employee - if the map "employee" already has the entry "FirstName" - then we'll just write it again..
// you can also add some ifs around it.
employee.put("EmpID", empId); // Integer is an Object so we can add the number directly to our map without casting it any more
employee.put("FirstName", employeeData[1].trim()); //FirstName is a String, we just read it and trim it (to remove all possible whitespaces)
employee.put("LastName", employeeData[2].trim()); // the same

List<String> groups; // the groups - again we don't know if the list is already present or not..
if(employee.containsKey("Groupmbr")){
groups = (ArrayList<String>) employee.get("Groupmbr"); // in case we already have a list, we get it and we cast it (!) from Object to ArrayList
} else {
groups = new ArrayList<String>();
employee.put("Groupmbr", groups);
}
groups.add(employeeData[3].trim()); // finally we add the groupe to the list
} // and this is it..


现在,您有了一个具有单个条目(用户正在寻找的雇员)的Map(map2),并且该地图中的所有雇员数据。现在,您可以遍历员工数据图并进行打印。

如果您想读取整个CSV并将所有员工加载到地图中,也可以删除 if(empId==...)

顺便说一句,我将重命名 map2变量。使用易于识别的变量。为了进一步清理代码,您可能需要将映射键放在静态变量中(或从CSV标头动态读取它们)。但是,在代码中包含修复字符串会很快变得很丑陋-这也浪费了内存。

我希望这会有所帮助(即使时间很长)

关于java - 内部 map 具有多个键值对的 map 内部的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61818281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com