English 中文(简体)
可以使用 2D 矩阵存储 JSF 复选框吗?
原标题:Is it possible to use 2D array to store JSF checkboxes?

我有一个 JSF 表格, 该表格使用 Hashmap 存储选中的复选框。 我的问题是: 是否可以使用 2D Java 阵列存储密钥而不是 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;
    }

我的想法是创建 2D 数组( 将使用 MAP), 以存储页面页数和列表。 例如 100 页 x 10 个密钥 。

<强 > 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?

问题回答

map 允许通过 put () 方法进行动态扩展,所以在使用前不需要准备,但数组不允许动态解算,而且必须事先手工准备,尺寸固定。

2D 数组很难准备,如果从技术上讲,想要一D 组配对,则完全没有意义。如果使用一D 数组,使用表格行索引作为数组索引,则更容易。

例如:

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

@EJB
private ItemService service;

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

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

但是,如果引入光学或过滤或动态添加/删除行,这可能会造成混乱。您需要手动在数组大小上拉小提琴,并每次都点顺序。一个 Map ,由项目s 自己键入的 id 而不是行索引会更容易得多。


<强 > 更新 :

“ 选择全部” Boolean [ < 应当简单到

Arrays.fill(checked, Boolean.TRUE);

我在我的i3笔记本电脑上测试了它,在2 000米之内完成 100万个条目。也许应该在持久性层中研究你的性能问题。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签