Boa noite a todos,
Estou com o seguinte problema, tenho 3 listas e preciso uni-las em 1 unica lista, e para isso criei uma função com recursividade, porem eu tenho um campo chamado (ENABLE_INPUT) que tem condição “0” ou “1” e que pode estar em qualquer um dos níveis, quando for “0” em qualquer dos níveis mesmo que seja no primeiro nível, toda linha gerada deve permanecer com a condição “0”, porem não consigo resolver isso, não consigo sair do lugar…
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Test
{
private static List<Object> level1 = new ArrayList<Object>() {{
add(new ArrayList<Object>() {{
add(new HashMap<String, Object>() {{
put("CALMONTH", "201901");
put("ENABLE_INPUT", "1");
}});
add(new HashMap<String, Object>() {{
put("CALMONTH", "201902");
put("ENABLE_INPUT", "1");
}});
}});
}};
private static List<Object> level2 = new ArrayList<Object>() {{
add(new ArrayList<Object>() {{
add(new HashMap<String, Object>() {{
put("VERSION", "REAL");
put("ENABLE_INPUT", "0");
}});
add(new HashMap<String, Object>() {{
put("VERSION", "PLANNING");
put("ENABLE_INPUT", "1");
}});
}});
}};
private static List<Object> level3 = new ArrayList<Object>() {{
add(new ArrayList<Object>() {{
add(new HashMap<String, Object>() {{
put("INDICATOR", "IND0001");
put("ENABLE_INPUT", "1");
}});
add(new HashMap<String, Object>() {{
put("INDICATOR", "IND0002");
put("ENABLE_INPUT", "1");
}});
}});
}};
private static HashMap<Integer, List<Object>> levelResult = new HashMap<Integer, List<Object>>() {{
put(1, level1);
put(2, level2);
put(3, level3);
}};
public static void main(String[] args)
{
System.out.print(level1);
System.out.print(level2);
System.out.print(level3);
System.out.print(levelResult);
System.out.print("--------------------------------------------------------------------");
List<Object> test = returnResultList(levelResult, null, 1, 3, null);
System.out.print("size: " + test.size());
for(Object item: test ) {
System.out.print((HashMap<String, Object>)item);
}
}
public static List<Object> returnResultList(HashMap<Integer, List<Object>> levelList, HashMap<String, Object> currentLevel, Integer iCurrent, Integer iMax, List<Object> result){
if(result == null) {
result = new ArrayList<Object>();
}
try {
List<Object> levelItem = levelList.get(iCurrent);
for (int i = 0; i < levelItem.size(); i++) {
List<Object> list = (List<Object>) levelItem.get(i);
for (int j = 0; j < list.size(); j++) {
HashMap<String, Object> nivel = (HashMap<String, Object>) list.get(j);
if(currentLevel == null) {
currentLevel = new HashMap<String, Object>();
}
HashMap<String, Object> previousLevel = new HashMap<String, Object>();
previousLevel = returnHash(previousLevel, currentLevel);
currentLevel = returnHash(currentLevel, nivel);
if (iCurrent == iMax){
result.add(currentLevel);
currentLevel = new HashMap<String, Object>();
currentLevel = returnHash(currentLevel, previousLevel);
}
if(iCurrent < iMax) {
HashMap<String, Object> lowerLevel = new HashMap<String, Object>();
lowerLevel = returnHash(lowerLevel, currentLevel);
returnResultList(levelList, lowerLevel, iCurrent + 1, iMax, result);
}
}
}
} catch (Exception e) {
System.out.print("returnResultList: " + e);
}
return result;
}
public static HashMap<String, Object> returnHash (HashMap<String, Object> previousLevel, HashMap<String, Object> nextLevel){
HashMap<String, Object> result = previousLevel;
try {
for(String key : nextLevel.keySet() ) {
if (result.get(key) == null) {
result.put(key, nextLevel.get(key));
} else {
result.replace(key, nextLevel.get(key));
}
}
} catch (Exception e) {
System.out.print("returnHash: " + e);
}
return result;
}
}
Resultado gerado através do codigo acima--------------------------------------------------------------------
size: 8
{CALMONTH=201901, VERSION=REAL, INDICATOR=IND0001, ENABLE_INPUT=1}
{CALMONTH=201901, VERSION=REAL, INDICATOR=IND0002, ENABLE_INPUT=1}
{CALMONTH=201901, VERSION=PLANNING, INDICATOR=IND0001, ENABLE_INPUT=1}
{CALMONTH=201901, VERSION=PLANNING, INDICATOR=IND0002, ENABLE_INPUT=1}
{CALMONTH=201902, VERSION=REAL, INDICATOR=IND0001, ENABLE_INPUT=1}
{CALMONTH=201902, VERSION=REAL, INDICATOR=IND0002, ENABLE_INPUT=1}
{CALMONTH=201902, VERSION=PLANNING, INDICATOR=IND0001, ENABLE_INPUT=1}
{CALMONTH=201902, VERSION=PLANNING, INDICATOR=IND0002, ENABLE_INPUT=1}
Precisaria que ficasse com o seguinte resultado
size: 8
{CALMONTH=201901, VERSION=REAL, INDICATOR=IND0001, ENABLE_INPUT=0}
{CALMONTH=201901, VERSION=REAL, INDICATOR=IND0002, ENABLE_INPUT=0}
{CALMONTH=201901, VERSION=PLANNING, INDICATOR=IND0001, ENABLE_INPUT=1}
{CALMONTH=201901, VERSION=PLANNING, INDICATOR=IND0002, ENABLE_INPUT=1}
{CALMONTH=201902, VERSION=REAL, INDICATOR=IND0001, ENABLE_INPUT=0}
{CALMONTH=201902, VERSION=REAL, INDICATOR=IND0002, ENABLE_INPUT=0}
{CALMONTH=201902, VERSION=PLANNING, INDICATOR=IND0001, ENABLE_INPUT=1}
{CALMONTH=201902, VERSION=PLANNING, INDICATOR=IND0002, ENABLE_INPUT=1}