如何获取树图中所有值的组合
How to get all combination of values in a Treemap
我有一张包含以下值的地图:
TreeMap<String, String> params = new TreeMap<>();
params.put("Has GPS – based Lat/Long", "Yes, No");
params.put("Ad Size", "320x480, 300x250");
params.put("Integration", "Direct, Indirect");
params.put("Vdo Ad Formats", "Max, Min, Med");
params.put("App/Mobile Targeting", "Mobile Web, App");
现在我想要来自以下值的所有组合:
320x480, Yes, Direct, Max, Mobile Web
320x480, Yes, Direct, Max, App
300x250, Yes, Direct, Max, APP
300x250, Yes, Indirect, Max, Mobile Web
300x250, Yes, Direct, Max, Mobile Web
300x250, No, Direct, Max, Mobile Web
etc....
解决方案已尝试,但根本无法提供所有组合。
List<String> keysList = new ArrayList<>();
keysList.addAll(params.keySet());
//1. iterating the keys list
for(int i=0; i<keysList.size(); i++)
{
String x = "";
String [] values_00 = map.get(keysList.get(i)).split(",");
//2. iterating array of values
for(int a0=0; a0<values_00.length; a0++)
{
//3. Iterating the next available keys from the list
for(int j=i+1; j<keysList.size(); j++)
{
String [] values_01 = map.get(keysList.get(j)).split(",");
//4. Iterating values of next array of values of next available keys
for(int a1=0; a1<values_01.length; a1++)
{
x = values_00[a0] + " " + values_01[a1];
System.out.println(x);
}
}
}
}
这类问题通常用递归程序解决。我放了一个可运行的例子on GitHub。要了解它的作用,基本上您想要打印每个其他选项值的每个选项值。
您将拥有一个长度等于选项的 numbef 的缓冲区,并且对每个选项进行一次(递归)调用。在递归函数中
- 当缓冲区已满时,您将打印结果
- 否则,遍历选项中的值并再次调用递归函数
Java-8 不是很困难:
Stream<String> combinations = params.values().stream()
.<Supplier<Stream<String>>>map(str -> () -> Pattern.compile(", ").splitAsStream(str))
.reduce((s1, s2) -> () -> s1.get().flatMap(e1 -> s2.get().map(e2 -> e1 + ", " + e2)))
.get().get();
combinations.forEach(System.out::println);
输出为:
320x480, Mobile Web, Yes, Direct, Max
320x480, Mobile Web, Yes, Direct, Min
320x480, Mobile Web, Yes, Direct, Med
320x480, Mobile Web, Yes, Indirect, Max
320x480, Mobile Web, Yes, Indirect, Min
...
300x250, App, No, Indirect, Max
300x250, App, No, Indirect, Min
300x250, App, No, Indirect, Med
请注意,当您使用 TreeMap
按键排序时,地图元素已重新排序。如果您需要一些特定的顺序,请改用 LinkedHashMap
。
我有一张包含以下值的地图:
TreeMap<String, String> params = new TreeMap<>();
params.put("Has GPS – based Lat/Long", "Yes, No");
params.put("Ad Size", "320x480, 300x250");
params.put("Integration", "Direct, Indirect");
params.put("Vdo Ad Formats", "Max, Min, Med");
params.put("App/Mobile Targeting", "Mobile Web, App");
现在我想要来自以下值的所有组合:
320x480, Yes, Direct, Max, Mobile Web
320x480, Yes, Direct, Max, App
300x250, Yes, Direct, Max, APP
300x250, Yes, Indirect, Max, Mobile Web
300x250, Yes, Direct, Max, Mobile Web
300x250, No, Direct, Max, Mobile Web
etc....
解决方案已尝试,但根本无法提供所有组合。
List<String> keysList = new ArrayList<>();
keysList.addAll(params.keySet());
//1. iterating the keys list
for(int i=0; i<keysList.size(); i++)
{
String x = "";
String [] values_00 = map.get(keysList.get(i)).split(",");
//2. iterating array of values
for(int a0=0; a0<values_00.length; a0++)
{
//3. Iterating the next available keys from the list
for(int j=i+1; j<keysList.size(); j++)
{
String [] values_01 = map.get(keysList.get(j)).split(",");
//4. Iterating values of next array of values of next available keys
for(int a1=0; a1<values_01.length; a1++)
{
x = values_00[a0] + " " + values_01[a1];
System.out.println(x);
}
}
}
}
这类问题通常用递归程序解决。我放了一个可运行的例子on GitHub。要了解它的作用,基本上您想要打印每个其他选项值的每个选项值。
您将拥有一个长度等于选项的 numbef 的缓冲区,并且对每个选项进行一次(递归)调用。在递归函数中
- 当缓冲区已满时,您将打印结果
- 否则,遍历选项中的值并再次调用递归函数
Java-8 不是很困难:
Stream<String> combinations = params.values().stream()
.<Supplier<Stream<String>>>map(str -> () -> Pattern.compile(", ").splitAsStream(str))
.reduce((s1, s2) -> () -> s1.get().flatMap(e1 -> s2.get().map(e2 -> e1 + ", " + e2)))
.get().get();
combinations.forEach(System.out::println);
输出为:
320x480, Mobile Web, Yes, Direct, Max
320x480, Mobile Web, Yes, Direct, Min
320x480, Mobile Web, Yes, Direct, Med
320x480, Mobile Web, Yes, Indirect, Max
320x480, Mobile Web, Yes, Indirect, Min
...
300x250, App, No, Indirect, Max
300x250, App, No, Indirect, Min
300x250, App, No, Indirect, Med
请注意,当您使用 TreeMap
按键排序时,地图元素已重新排序。如果您需要一些特定的顺序,请改用 LinkedHashMap
。