-3

I have list of string such this:

List<String> lista = Arrays.asList("Water","Pomegranate","Fish","Potato","Milk","Grape","steaks","parsley");//The list is big of that

This list repeat the category each 4 time I want to fetch each element under its category column as this in jsf page:

Drinks      Fruits           Meat      Vegetables

Water.       Pomegranate.    Fish.     Potato
Milk.        Grape.          steaks.   parsley

              ِِِِ... the others ...

I create Entity class:

public class Entity
{
      private String drink;
      private String fruit;
      private String meat;
      private String vegetable;
      // getter and setter
}

How to do that?

I want to display element 0,4,8,12 .. in first column and element 1,5,9,13 in the second column and element 2,6,10,14 in the third column and element 3,7,11,15 in the fourth column and so on

Each element must be under its category

           <p:layout fullPage="true"> 
                 <h:form> 
                        <p:dataTable var="data" value="#{bean.list}">
                            <p:column headerText="Drinks" >
                                #{data.drink}
                            </p:column>
                        <p:column headerText="Fruits" >
                                #{data.fruit}
                            </p:column>

                        <p:column headerText="Meats" >
                                #{data.meat}
                            </p:column>

                        <p:column headerText="Vegis" >
                                #{data.vegetable}
                            </p:column>

                        </p:dataTable>

                    </h:form>

            </p:layout>
Wasfy
  • 73
  • 8

2 Answers2

0

The list doesn´t know anything about a category. You need to define a specific index for each category by your own.

final int CATEGORY_DRINK = 0;
final int CATEGORY_FRUIT = 1;
final int CATEGORY_MEAT = 2;
final int CATEGORY_VEGETABLE = 3;
final int CATEGORY_COUNT = 4;

int repeat = list.size() / CATEGORY_COUNT; //2
for(int i = 0; i < repeat; i++) {
    int offset = i * CATEGORY_COUNT;
    String drink = list.get(CATEGORY_DRINK + offset);
    String fruit = list.get(CATEGORY_FRUIT + offset);
    String meat = list.get(CATEGORY_MEAT + offset);
    String vegi = list.get(CATEGORY_VEGETABLE + offset);
}

Alternatively, you can try to use an other data structure. A map could be used to map a category to a list of values.

Franz Deschler
  • 2,456
  • 5
  • 25
  • 39
  • thanks for your answer It will help me more to retrieve them to jsf/primefaces datatable. – Wasfy Aug 03 '17 at 09:10
0

You can do it like this:

for(int i=0; i<lista.size();i=+4){
   for(int j=0; j<4; j++){
     if(j==0){
        string obj = lista.get(i+j);
        //add the string to the first list
     }
     if(j==1){
        string obj = lista.get(i+j);
        //add the string to the second list
     }
     if(j==2){
        string obj = lista.get(i+j);
        //add the string to the third list
     }
     if(j==3){
        string obj = lista.get(i+j);
        //add the string to the forth list
     }
   }
}
T. Jung
  • 3,327
  • 3
  • 11
  • 19
  • in your answer lista.length() must be lista.size() and itry your code but it is not executed thanls on anyway. – Wasfy Aug 03 '17 at 09:12