Creating combinations of multiple columns into object of array consisting of combination of values & column name in java?

huangapple go评论81阅读模式
英文:

Creating combinations of multiple columns into object of array consisting of combination of values & column name in java?

问题

import java.util.ArrayList;
import java.util.List;

class Attribute {
    private String type;
    private List<AttributeFilters> filterValues;

    // Constructor, getters, setters
}

class AttributeFilters {
    private String name;

    // Constructor, getters, setters
}

public class Main {
    public static void main(String[] args) {
        List<Attribute> attributes = new ArrayList<>();
        // Populate the attributes list with data
        
        List<List<AttributeFilters>> combinations = generateCombinations(attributes, 0, new ArrayList<>());

        List<Object> result = new ArrayList<>();
        for (List<AttributeFilters> combination : combinations) {
            List<Object> values = new ArrayList<>();
            for (AttributeFilters filter : combination) {
                values.add(new KeyValuePair(filter.getType(), filter.getName()));
            }
            result.add(new ValueObject(values));
        }

        // Now 'result' contains the desired combination structure
    }

    static class KeyValuePair {
        private String type;
        private String name;

        public KeyValuePair(String type, String name) {
            this.type = type;
            this.name = name;
        }

        // Getters, setters
    }

    static class ValueObject {
        private List<KeyValuePair> values;

        public ValueObject(List<KeyValuePair> values) {
            this.values = values;
        }

        // Getters, setters
    }

    static List<List<AttributeFilters>> generateCombinations(List<Attribute> attributes, int index, List<AttributeFilters> currentCombination) {
        List<List<AttributeFilters>> combinations = new ArrayList<>();

        if (index == attributes.size()) {
            combinations.add(new ArrayList<>(currentCombination));
            return combinations;
        }

        Attribute attribute = attributes.get(index);
        for (AttributeFilters filter : attribute.getFilterValues()) {
            currentCombination.add(filter);
            combinations.addAll(generateCombinations(attributes, index + 1, currentCombination));
            currentCombination.remove(currentCombination.size() - 1);
        }

        return combinations;
    }
}

Please note that this code is just a starting point and may need adjustments based on your specific requirements and data structures.

英文:

Creating combinations of multiple columns into object of array consisting of combination of values & column name in java ?
So that each object will consist of array of Object (column name, value).

I've information stored by it's column name & values inside that column.
For example:

Region 	Store	 	State
west 	Reliance 	california
east 	Dmart 		newyork

Basically for above the JSON structure is as below

[
{
&quot;type&quot;: &quot;Region&quot;,
//more fields
&quot;filterValues&quot;: [
{
&quot;name&quot;: &quot;west&quot;
//more fields
},
{
&quot;name&quot;: &quot;east&quot;
}
]
},
{
&quot;type&quot;: &quot;Store&quot;,
&quot;filterValues&quot;: [
{
&quot;name&quot;: &quot;Reliance&quot;
},
{
&quot;name&quot;: &quot;Dmart&quot;
}
]
},
{
&quot;type&quot;: &quot;State&quot;,
&quot;filterValues&quot;: [
{
&quot;name&quot;: &quot;california&quot;
},
{
&quot;name&quot;: &quot;newyork&quot;
}
]
}
]

How I can convert the above structure into the following combinations:

Region west  | Store Reliance | State california
Region west  | Store Reliance | State newyork
Region west  | Store Dmart    | State california
Region west  | Store Dmart    | State newyork
.
.
.

So the Object structure will be as follow.

[
{
&quot;values&quot;: [
{
&quot;type&quot;: &quot;Region&quot;,
&quot;name&quot;: &quot;west&quot;
},
{
&quot;type&quot;: &quot;Store&quot;,
&quot;name&quot;: &quot;Reliance&quot;
},
{
&quot;type&quot;: &quot;State&quot;,
&quot;name&quot;: &quot;california&quot;
}
]
},
{
&quot;values&quot;: [
{
&quot;type&quot;: &quot;Region&quot;,
&quot;name&quot;: &quot;west&quot;
},
{
&quot;type&quot;: &quot;Store&quot;,
&quot;name&quot;: &quot;Reliance&quot;
},
{
&quot;type&quot;: &quot;State&quot;,
&quot;name&quot;: &quot;newyork&quot;
}
]
}
]

How can I do implement this data structure ?
I was trying by creating map first in which column name will be key & values in that column will be part of value as

Map&lt;String, List&lt;String&gt;&gt; typeBOsMap = attributes
.stream()
.collect(Collectors.toMap(Attribute::getType,
attribute -&gt; attribute.getFilterValues()
.stream()
.map(AttributeFilters::getFilterValue)
.collect(Collectors.toList()))
);

But after this again I'm stuck at how the combination will get created from this map ?


答案1

得分: 1

// 首先,将 `typeBOsMap` 转换为 `List<Map.Entry<String, List<String>>>`
List<Map.Entry<String, List<String>>> list = typeBOsMap.entrySet().stream().collect(Collectors.toList());

// 然后递归地对所有可能的组合进行排列
List<List<Map.Entry<String, String>>> permute(List<Map.Entry<String, List<String>>> list,
    int index, List<Map.Entry<String, String>> now) {
  if (index >= list.size()) {
    return Arrays.asList(now);
  }
  Map.Entry<String, List<String>> entry = list.get(index);
  List<List<Map.Entry<String, String>>> res = new ArrayList<>();
  for (String value : entry.getValue()) {
    List<Map.Entry<String, String>> newList = new ArrayList<>(now);
    newList.add(new AbstractMap.SimpleEntry<>(entry.getKey(), value));
    res.addAll(permute(list, index + 1, newList));
  }
  return res;
}

// 可以使用 `list` 调用这个函数
List<List<Map.Entry<String, String>>> data = permute(list, 0, Collections.emptyList());

// 输出结果:
// [State=california, Region=west, Store=Reliance]
// [State=california, Region=west, Store=Dmart]
// [State=california, Region=east, Store=Reliance]
// [State=california, Region=east, Store=Dmart]
// [State=newyork, Region=west, Store=Reliance]
// [State=newyork, Region=west, Store=Dmart]
// [State=newyork, Region=east, Store=Reliance]
// [State=newyork, Region=east, Store=Dmart]

在线演示在这里

英文:

First, convert typeBOsMap into List&lt;Map.Entry&lt;String, List&lt;String&gt;&gt;&gt;

  List&lt;Map.Entry&lt;String, List&lt;String&gt;&gt;&gt; list = typeBOsMap.entrySet().stream().collect(Collectors.toList());

Then recursive permute all possible combination

  List&lt;List&lt;Map.Entry&lt;String, String&gt;&gt;&gt; permute(List&lt;Map.Entry&lt;String, List&lt;String&gt;&gt;&gt; list,
int index, List&lt;Map.Entry&lt;String, String&gt;&gt; now) {
if (index &gt;= list.size()) {
return Arrays.asList(now);
}
Map.Entry&lt;String, List&lt;String&gt;&gt; entry = list.get(index);
List&lt;List&lt;Map.Entry&lt;String, String&gt;&gt;&gt; res = new ArrayList&lt;&gt;();
for (String value : entry.getValue()) {
List&lt;Map.Entry&lt;String, String&gt;&gt; newList = new ArrayList&lt;&gt;(now);
newList.add(new AbstractMap.SimpleEntry&lt;&gt;(entry.getKey(), value));
res.addAll(permute(list, index + 1, newList));
}
return res;
}

You can call this function using list

List&lt;List&lt;Map.Entry&lt;String, String&gt;&gt;&gt; data = permute(list, 0, Collections.emptyList());

Output:

[State=california, Region=west, Store=Reliance]
[State=california, Region=west, Store=Dmart]
[State=california, Region=east, Store=Reliance]
[State=california, Region=east, Store=Dmart]
[State=newyork, Region=west, Store=Reliance]
[State=newyork, Region=west, Store=Dmart]
[State=newyork, Region=east, Store=Reliance]
[State=newyork, Region=east, Store=Dmart]

Online Demo here

huangapple
  • 本文由 发表于 2020年8月15日 16:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63423871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定