[英]How to iterate a list of an object to make a new Map from it?
I am trying to iterate a List of an Object such that after iterating I can make a Map of String and List<String>
from it. 我正在尝试迭代一个对象的列表,以便在迭代后可以从中创建
Map of String and List<String>
的Map of String and List<String>
。
Below is my Machine class which has dataCenter, hostName and hostId. 下面是我的Machine类,其中包含dataCenter,hostName和hostId。
public class Machine {
private String dataCenter;
private String hostName;
private String hostId;
// getters here
public Machine(String dataCenter, String hostName, String hostId) {
// some code here
}
}
If you see my below code, I have a List of machines
in which I have 6 machine object. 如果您看到下面的代码,则有一个
machines
列表,其中有6个机器对象。 And then iterate machines
object and make a new Map out of it. 然后迭代
machines
对象,并根据它创建一个新的Map。
Map<String, List<String>> holder = new LinkedHashMap<String, List<String>>();
List<Machine> machines = new ArrayList<Machine>();
Machine machine1 = new Machine("DC1", "machineA", "hostA");
Machine machine2 = new Machine("DC1", "machineB", "hostB");
Machine machine3 = new Machine("DC2", "machineC", "hostC");
Machine machine4 = new Machine("DC2", "machineD", "hostD");
Machine machine5 = new Machine("DC3", "machineE", "hostE");
Machine machine6 = new Machine("DC3", "machineF", "hostF");
machines.add(machine1);
machines.add(machine2);
machines.add(machine3);
machines.add(machine4);
machines.add(machine5);
machines.add(machine6);
// some other code here
// iterate machines object
for (Machine machine : machines) {
LinkedList<String> ppMachine = new LinkedList<String>();
// below code doesn't work as it shows only last machine for that Datacenter
ppMachine.add(machine.getHostId());
holder.put(machine.getDatacenter(), ppMachine);
}
System.out.println(holder);
The new map should be something like this - 新地图应该是这样的-
DC1 - hostA,hostB
DC2 - hostC,hostD
DC3 - hostE,hostF
Here DC1
is the key of the new map and hostA,hostB
is the list of string values for that map. 这里
DC1
是新映射的键, hostA,hostB
是该映射的字符串值的列表。 Similarly for others. 其他人也一样。
But somehow in my holder map, I always see latest value of machine for that Datacenter, not all the machines for that Datacenter. 但是以某种方式在我的所有者图中,我总是看到该数据中心的机器的最新价值,而不是该数据中心的所有机器。
I am doing very silly mistake somehow. 我以某种方式犯了非常愚蠢的错误。
LinkedList<String> ppMachine = new LinkedList<String>();
// below code doesn't work as it shows only last machine for that Datacenter
ppMachine.add(machine.getHostId());
holder.put(machine.getDatacenter(), ppMachine);
You're replacing the entire list associated with machine.getColo()
, not adding a new entry. 您将替换与
machine.getColo()
关联的整个列表,而不添加新条目。
Probably the simplest fix would be 可能最简单的解决方法是
LinkedList<String> dynMachine = holder.get(machine.getDatacenter());
if (dynMachine == null) {
dynMachine = new LinkedList<String>();
holder.put(machine.getDatacenter(), dynMachine);
}
dynMachine.add(machine.getDatacenter());
Thats because your are creating a new list on each iteration, this is how you can achieve it: 那就是因为您要在每次迭代中创建一个新列表,因此可以实现它:
// iterate machines object
for (Machine machine : machines) {
LinkedList<String> dynMachine = holder.get(machine.getColo());
if (null == dynMachine) {
dynMachine = new LinkedList<String>();
holder.put(machine.getColo(), dynMachine);
}
dynMachine.add(machine.getHostId());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.