英文:
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
[
{
"type": "Region",
//more fields
"filterValues": [
{
"name": "west"
//more fields
},
{
"name": "east"
}
]
},
{
"type": "Store",
"filterValues": [
{
"name": "Reliance"
},
{
"name": "Dmart"
}
]
},
{
"type": "State",
"filterValues": [
{
"name": "california"
},
{
"name": "newyork"
}
]
}
]
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.
[
{
"values": [
{
"type": "Region",
"name": "west"
},
{
"type": "Store",
"name": "Reliance"
},
{
"type": "State",
"name": "california"
}
]
},
{
"values": [
{
"type": "Region",
"name": "west"
},
{
"type": "Store",
"name": "Reliance"
},
{
"type": "State",
"name": "newyork"
}
]
}
]
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<String, List<String>> typeBOsMap = attributes
.stream()
.collect(Collectors.toMap(Attribute::getType,
attribute -> 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<Map.Entry<String, List<String>>>
List<Map.Entry<String, List<String>>> list = typeBOsMap.entrySet().stream().collect(Collectors.toList());
Then recursive permute all possible combination
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;
}
You can call this function using list
List<List<Map.Entry<String, String>>> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论