如何针对此方案优化for循环或更好的解决方案?

时间:2018-01-17 04:31:59

标签: java performance for-loop java-8 hashmap

我必须实现一些类似的东西,我正在使用循环来做它但我不认为这是一种正确的方法。有人可以帮助我以最佳方式做到这一点吗?

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

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}}

我原来的Object包含这样的

{id : color , {black, white}}, {id:size, {32,33,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;
 }

}
public class Test {

    public static void main(String[] args) throws ParseException {

        List<IdData> data = new ArrayList<>();
        IdData ab = new IdData();
        List<String> colors = new ArrayList<>();
        colors.add("black");
        colors.add("red");
        colors.add("white");
        ab.setId("color");
        ab.setValues(colors);
        data.add(ab);

        IdData ab1 = new IdData();
        List<String> size = new ArrayList<>();
        size.add("32");
        size.add("33");
        size.add("34");
        ab1.setId("size");
        ab1.setValues(size);
        data.add(ab1);

        Map<Integer, Map<String, String>> mapItems = new HashMap<>();
        for (IdData item : data) {
            item.getValues().forEach(firstIndex -> {
                data.forEach(itemNext -> {
                    if (!itemNext.getId().equals(item.getId())) {
                        itemNext.getValues().forEach(secondOne -> {
                            Map<String, String> someMap = new HashMap<>();
                            someMap.put(item.getId(), firstIndex);
                            someMap.put(itemNext.getId(), secondOne);
                            mapItems.put(mapItems.size(), someMap);
                        });
                    }
                });
            });
            break;
        }
    }
}

它按预期工作正常并给我输出,但想象List <IdData> data通过向列表中再添加一个项目而增加,它将无效。我怎样才能让它充满活力效率更高。

{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}} 

1 个答案:

答案 0 :(得分:1)

好吧,我不熟悉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);
    }
}

然后用:

调用该函数
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}}