商品规格递归拼接

创建实体类

@Data
public class Shopping {

    private String name;

    private List<String> children;
}

测试

 public static void main(String[] args) {

  ArrayList<Shopping> shoppings = new ArrayList<>();

  Shopping shopping = new Shopping();
  shopping.setName("内存");
  ArrayList<String> strings = new ArrayList<>();
  strings.add("12G");
  strings.add("16G");
  shopping.setChildren(strings);

  Shopping shopping1 = new Shopping();
  shopping1.setName("存储");
  ArrayList<String> strings1 = new ArrayList<>();
  strings1.add("256G");
  strings1.add("512G");
  shopping1.setChildren(strings1);

  Shopping shopping2 = new Shopping();
  shopping2.setName("颜色");
  ArrayList<String> strings2 = new ArrayList<>();
  strings2.add("黑色");
  strings2.add("白色");
  shopping2.setChildren(strings2);

  shoppings.add(shopping);
  shoppings.add(shopping1);
  shoppings.add(shopping2);

  List<List<String>> combinations = generateCombinations(shoppings);
  for (List<String> combination : combinations) {
   System.out.println(combination);
  }

 }

 public static List<List<String>> generateCombinations(List<Shopping> shoppings) {
  List<List<String>> result = new ArrayList<>();
  generateCombinationsRecursive(shoppings, 0, new ArrayList<>(), result);
  return result;
 }

 private static void generateCombinationsRecursive(List<Shopping> shoppings, int index, List<String> current, List<List<String>> result) {
  if (index == shoppings.size()) {
   result.add(new ArrayList<>(current));
   return;
  }

  Shopping shopping = shoppings.get(index);
  for (String option : shopping.getChildren()) {
   current.add(shopping.getName() + ": " + option);
   generateCombinationsRecursive(shoppings, index + 1, current, result);
   current.remove(current.size() - 1);
  }
 }

效果

相关推荐
《源码好优多》8 分钟前
基于Java Springboot宠物流浪救助系统
java·spring boot·宠物
《源码好优多》8 分钟前
基于Java Springboot宠物救助管理系统
java·spring boot·宠物
刽子手发艺18 分钟前
JVM双亲委派机制详解
java·开发语言·jvm
kigumi38 分钟前
Android Framework与JNI
android·java
国通快递驿站1 小时前
AntFlow:一款高效灵活的开源工作流引擎
java·spring boot·后端·开源·钉钉
Tdm_8881 小时前
C# 自动属性
java·开发语言·c#·asp.net
Nonullpoint.1 小时前
对称加密与非对称加密:密码学的基石及 RSA 算法详解
java·计算机网络·算法·网络安全·密码学
问道飞鱼1 小时前
【Java知识】Java性能测试工具JMeter
java·测试工具·jmeter
陈大爷(有低保)1 小时前
JQuery封装的ajax
java·后端
♡喜欢做梦2 小时前
【数据结构】栈和队列详解!!!--Java
java·开发语言·数据结构·链表