Java 如何针对这种情况优化for循环或更好的解决方案?

Java 如何针对这种情况优化for循环或更好的解决方案?,java,performance,for-loop,java-8,hashmap,Java,Performance,For Loop,Java 8,Hashmap,我必须实现一些类似这样的目标,我正在使用for循环来实现它,但我认为这不是一个正确的方法。有人能帮我以最佳的方式做这件事吗 我想建立一个地图,应该包含这样的数据 Map<Integer, Map<String, String> mapData = new HashMap<>(); {0,{color : black,size : 32}}, {1,{color : black,size : 33}}, {2,{color : black,size : 34}},

我必须实现一些类似这样的目标,我正在使用for循环来实现它,但我认为这不是一个正确的方法。有人能帮我以最佳的方式做这件事吗

我想建立一个地图,应该包含这样的数据

Map<Integer, Map<String, String> mapData = new HashMap<>();

{0,{color : black,size : 32}},
{1,{color : black,size : 33}},
{2,{color : black,size : 34}},
{3,{color : white,size : 32}},
{4,{color : white,size : 33}},
{5,{color : white,size : 34}}
编辑: 这就是我尝试过的

public class IdData {

 String id;
 List < String > values;

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public List < String > getValues() {
  return values;
 }

 public void setValues(List < String > values) {
  this.values = values;
 }

}

好吧,我对Java 8不太熟练,但您所要求的可以很容易地使用递归函数来完成:

private static void getAllCombinations(Map<Integer, Map<String, String>> mapItems,
                              Map<String, String> tempMap, 
                              List<IdData> data, int index)
{
    if (index == data.size())
    {
        mapItems.put(mapItems.size(), new LinkedHashMap<>(tempMap));
        return;
    }

    IdData item = data.get(index);
    String key = item.id;
    for (String value : item.values)
    {
        tempMap.put(key, value);
        getAllCombinations(mapItems, tempMap, data, index + 1);
    }
}
这也适用于两种以上的项目类型。只需在列表中添加另一项即可

IdData ab2 = new IdData();
List<String> weight = new ArrayList<>();
weight.add("50");
weight.add("60");
weight.add("70");
ab2.setId("weight");
ab2.setValues(weight);
data.add(ab2);

我没有看到任何循环。努力在哪里?for循环对我来说似乎很合适。左边的数字从
0
开始到
5
,右边的数字从
32
开始到
34
,然后再回到
32
。可以使用模运算符实现正确数字的逻辑。将其存储在HashMap中,其中数据是一个包含字段的类:颜色、大小。是否可以将其从保留中删除@SaviNuclear添加了我的代码。哇,真棒@Vivek Kumar,这就是我想要的,简单的递归函数。谢谢
{0={color=black, size=32}, 1={color=black, size=33}, 2={color=black,   size=34}, 3={color=red, size=32}, 4={color=red, size=33}, 5={color=red,    
size=34}, 6={color=white, size=32}, 7={color=white, size=33}, 8={color=white, size=34}} 
private static void getAllCombinations(Map<Integer, Map<String, String>> mapItems,
                              Map<String, String> tempMap, 
                              List<IdData> data, int index)
{
    if (index == data.size())
    {
        mapItems.put(mapItems.size(), new LinkedHashMap<>(tempMap));
        return;
    }

    IdData item = data.get(index);
    String key = item.id;
    for (String value : item.values)
    {
        tempMap.put(key, value);
        getAllCombinations(mapItems, tempMap, data, index + 1);
    }
}
Map<Integer, Map<String, String>> mapItems = new LinkedHashMap<>();
getAllCombinations(mapItems, new LinkedHashMap<String, String>(), data, 0);
{0={color=black, size=32}
1={color=black, size=33}
2={color=black, size=34}
3={color=red, size=32}
4={color=red, size=33}
5={color=red, size=34}
6={color=white, size=32}
7={color=white, size=33}
8={color=white, size=34}}
IdData ab2 = new IdData();
List<String> weight = new ArrayList<>();
weight.add("50");
weight.add("60");
weight.add("70");
ab2.setId("weight");
ab2.setValues(weight);
data.add(ab2);
{0={color=black, size=32, height=50}, 1={color=black, size=32, height=60},
 2={color=black, size=32, height=70}, 3={color=black, size=33, height=50},
 4={color=black, size=33, height=60}, 5={color=black, size=33, height=70},
 6={color=black, size=34, height=50}, 7={color=black, size=34, height=60},
 8={color=black, size=34, height=70}, 9={color=red, size=32, height=50}, 
10={color=red, size=32, height=60}, 11={color=red, size=32, height=70}, 
12={color=red, size=33, height=50}, 13={color=red, size=33, height=60}, 
14={color=red, size=33, height=70}, 15={color=red, size=34, height=50}, 
16={color=red, size=34, height=60}, 17={color=red, size=34, height=70}, 
18={color=white, size=32, height=50}, 19={color=white, size=32, height=60}, 
20={color=white, size=32, height=70}, 21={color=white, size=33, height=50}, 
22={color=white, size=33, height=60}, 23={color=white, size=33, height=70}, 
24={color=white, size=34, height=50}, 25={color=white, size=34, height=60}, 
26={color=white, size=34, height=70}}