1

I have an arraylist in Java ArrayList[Consultas.ControldeInformes, Consultas.SaldoMora, Consultas.ReporteMensual, Mantenimientos.Agregar, Mantenimientos.Editar, Mantenimientos.Actualizar]

I would like to convert the arraylist to place it as menus and submenus in HTML, how to iterate the array to be of this structure:

Menu: Consultas
Submenus: ControldeInformes,SaldoMora,ReporteMensual

Menu: Mantenimientos
Submenus: Agregar,Editar,Actualizar

How to separate or split the arraylist?

1 Answer 1

1

I write a code to create a map with your requirements:

class Main {
    public static void main(String[] args) {
        List<String> exampleList = new ArrayList<> (Arrays.asList(
          "Consultas.ControldeInformes", "Consultas.SaldoMora", 
          "Consultas.ReporteMensual", "Mantenimientos.Agregar", 
          "Mantenimientos.Editar", "Mantenimientos.Actualizar"

        ));
    System.out.print(mapInput(exampleList).toString());
  }

 private static Map<String,List<String>> mapInput(List<String> input) {

    Map<String,List<String>> map = new HashMap<>();

    input.stream()
      .map(x->getMenu(x))
      .distinct()
      .forEach(x->map.put(x, new ArrayList<>()));

    for (String item : input) {
      List<String> subMenus = map.get(getMenu(item));
      subMenus.add(getSubMenu(item));

      map.put(getMenu(item), subMenus);
    }

    return map;
  }

  private static String getMenu(String item){
    return item.split("\\.")[0];
  }

  private static String getSubMenu(String item){
    return item.split("\\.")[1];
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man, it's just what I needed. Now, how can I do to show that in a Menu and submenus on a jsp page? {Consultas=[ControldeInformes, SaldoMora, ReporteMensual], Mantenimientos=[Agregar, Editar, Actualizar]}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.