如何创建没有 POJO(动态列)的网格? [英] How to create a Grid without POJO (dynamic columns)?
问题描述
这里问的问题:https://vaadin.com/forum/thread/18095407/how-to-create-a-grid-without-binder
但是 vaadin 的论坛已经关闭,所以我想在这里继续.
在 Vaadin 14 上,关于实现具有动态可变列数的网格的最佳方法的任何建议.使用列 Index (1,2,3...) 对我来说不是一个好的选择.假设我有一个简单的 Json 文件(只有 1 个级别:键值)来映射到网格,而这个 Json 有一个未知的属性列表.
哪种方法在性能方面更好?:
[选项 1]
class 数据 {私有映射<字符串,对象>values = new HashMap<>();公共无效集(字符串键,对象值){values.put(key, val);}公共对象获取(字符串键){返回值.get(key);}}网格<数据>myGrid = new Grid<>();
[选项 2]
公共类GridDynamicValueProvider实现ValueProvider{私有整数列索引;公共 GridDynamicValueProvider(int columnIndex) {this.columnIndex = columnIndex;}@覆盖公共对象应用(GridDynamicRow dynamicRow){返回 dynamicRow.getValue(columnIndex);}}公共类 GridDynamicRow {私人列表<对象>values = new ArrayList<>();公共无效添加值(字符串值){值.添加(值);}公共对象 getValue(int columnIndex) {返回值.get(columnIndex);}}
Vaadin 的 SerializablePredicate 接受函数引用和 Lambdas,因此可以使用 Vaadin 中带有 Grid 和 Binder 的非 POJO 数据类型,尽管这有点不合常规.主要成分是:
Grid
如果您需要保护内部结构,您也可以将 Map 包装在自定义类中.
class 数据 {私有映射<字符串,对象>values = new HashMap<>();公共无效集(字符串键,对象值){values.put(key, val);}公共对象获取(字符串键){返回值.get(key);}}网格<数据>myGrid = 新网格<>();
至于性能,本质上,您是在使用按索引获取的 List 与按键获取的 HashMap 进行比较.这是一个相关的问题:ArrayList .get 比 HashMap .get 快?>
您也可以使用 ArrayList 作为网格的类型,如果您可以使用一个数字.
这两种方法都允许生成具有不同动态列数的网格,例如,如果您直接从文件读取数据或具有原始数据后端查询.
The question was asked here: https://vaadin.com/forum/thread/18095407/how-to-create-a-grid-without-binder
However the vaadin's forum closed so i want to continue it here.
On Vaadin 14, Any recommendation on the best way to implement grid with dynamic varying number of columns. Using column Index (1,2,3...) is not a good choice for me. Let say I have a simple Json file (only 1 level: key-value) to map to a grid and this Json has an unknown list of properties.
which approach is better in term of performance ?:
[Option 1]
class Data {
private Map<String, Object> values = new HashMap<>();
public void set(String key, Object val) {
values.put(key, val);
}
public Object get(String key) {
return values.get(key);
}
}
Grid<Data> myGrid = new Grid<>();
[Option 2]
public class GridDynamicValueProvider implements ValueProvider<GridDynamicRow, Object> {
private int columnIndex;
public GridDynamicValueProvider(int columnIndex) {
this.columnIndex = columnIndex;
}
@Override
public Object apply(GridDynamicRow dynamicRow) {
return dynamicRow.getValue(columnIndex);
}
}
public class GridDynamicRow {
private List<Object> values = new ArrayList<>();
public void addValue(String value) {
values.add(value);
}
public Object getValue(int columnIndex) {
return values.get(columnIndex);
}
}
The SerializablePredicate of Vaadin accepts both function references and Lambdas, thus it is possible to use non-POJO data types with Grid and Binder in Vaadin, although that is a bit unconventional. The key ingredients are:
Grid<Map<String, Integer>> grid = new Grid<>();
...
grid.addColumn(map -> map.get("column")).setHeader("Column");
You can also wrap the Map in custom class if you have need to protect the internals.
class Data {
private Map<String, Object> values = new HashMap<>();
public void set(String key, Object val) {
values.put(key, val);
}
public Object get(String key) {
return values.get(key);
}
}
Grid<Data> myGrid = new Grid<>();
As for the performance, essentially, you're comparing between using a List where you fetch by index versus a HashMap where you fetch by key. Here's a related question: ArrayList .get faster than HashMap .get?
You can use also ArrayList as Grid's type if you can index the columns with a number.
The both approaches allow generating Grid's with varying dynamic number of columns, for example if you read the data directly from a file or have raw data backend queries.
这篇关于如何创建没有 POJO(动态列)的网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!