1

I have a JSF table which uses Hashmap to store selected checkboxes. My question is: Is it possible to use 2D Java array to store the keys instead of the Hashmap?

EDIT I use this code to store the selected keys. How this code can be rewritten for Map?

private HashMap<String, Boolean> selected = new HashMap<>();

    // send the list
    public Map<String, Boolean> getselected() {
        return selected;
    }

My idea is to create 2D array(it will be used MAP) which will store the number of pages and the list on the pages. For example 100 pages x 10 keys.

EDIT 2

I created JSF table with lazy loading which takes data from Oracle. The JSF works very well and fast. The problem that I face is when I tried to create select all checkbox which takes all kays and stores the keys into hashmap. Later those keys will be used for SQL query to delete the rows which the user selects. I created database table with 10 000 rows and displayed them into JSF table. When I clicked them all I created hashmap with 10 000. the performance is relatively good for that size. I tested to delete them - I created Java method which takes the generated hashmap and deletes the database rows using the keys stored into the hashmap. It's show but there can be done a few code optimizations. The big problem is the scalability. I tested the hashmap with 1 000 000 keys - it works but it's terribly slow. I need to design the JSF table to handle very big data. I think that the solution is to use 2D array (lets call it for this example). I will explain my idea this way: I will have for example JSF table with 100 pages. Every page will have 10 rows. Into the standard hashmap I will have 1000 keys when I select all rows. In 2D array solution I can create array with 100 elements and when I switch between the pages I will have only 10 rows which will be generated from the pagination code. When I select all database rows I can insert only the visible rows key into the 2D array. The other positions into the the array I replace with for example 1 in order to know that there is something. When I press the delete button the Java code will know that all elements are "virtually" selected. Is there a better and more simple solution?

user1285928
  • 1,328
  • 29
  • 98
  • 147

1 Answers1

4

A Map allows a dynamic expansion by put() method, so you don't need to prepare it before use, but an array doesn't allow dynamic expension and must be manually prepared beforehand with a fixed size.

A 2D array is hard to prepare and makes no utter sense if you technically just want a 1D collection of pairs. It's easier if you use an 1D array and use the table's row index as array index.

E.g.

private List<Item> items;
private Boolean[] checked;

@EJB
private ItemService service;

@PostConstruct
public void init() {
    items = service.list();
    checked = new Boolean[items.size()];
}

with

<h:dataTable binding="#{table}" value="#{bean.items}" var="item">
    <h:column>
        <h:selectBooleanCheckbox value="#{bean.checked[table.rowIndex]}" />
    </h:column>
</h:dataTable>

This may however possibly go havoc if you introduce pagination or filtering or dynamic adding/removing rows. You'd need to manually fiddle on the array's size and ordering everytime. A Map which is keyed by item's own id instead of the row index is so much easier then.


Update:

A "select all" of a Boolean[] should be as simple as

Arrays.fill(checked, Boolean.TRUE);

I tested it on my i3 laptop and it's done in 20ms for 1,000,000 entries. Your performance problem should probably be sought in the persistence layer.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I updated the post. Would you please show me how my code must be edited. I'm unaware is JSF capable to work in this case with my specific implementation. – user1285928 May 25 '12 at 20:07
  • You can find a concrete kickoff example how to properly use the `Map` here: http://stackoverflow.com/questions/2524514/how-to-use-jsfs-hselectbooleancheckbox-with-hdatatable-to-create-one-object-p/2524832#2524832 – BalusC May 25 '12 at 20:09
  • I propose to start from the beginning. Maybe if I tell you everything you can tell me what is the best solution. I will write it into the post. – user1285928 May 25 '12 at 20:17
  • If you haven't deleted the test code, would you tell me how much memory occupy the HashMap with String which has 6 characters. Maybe I think where is my mistake - When I click "select all" I get the list with the keys from the DB and I put the keys into hashmap with TRUE flag. The bottleneck is the DB query. The possible solution maybe is to generate the keys from the DB during bean utilization into second thread. – user1285928 May 25 '12 at 22:06