小编NEO*_*NEO的帖子

将速度模板中的java对象列表显示为html表

我从DB获取对象列表.我想使用速度模板将它们填充到html表中.

<table>
<thead>
<tr>
<td>$value1 </td>
<td>$value2 </td>
</tr>
</thead>
<tbody>
<!-- Iterate through the list (List<SomeObject>) and display them here,   -->
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

对于标题我使用下面的代码,

VelocityContext context = new VelocityContext();
context.put("value1", "text1");
context.put("value2", "text2");
Run Code Online (Sandbox Code Playgroud)

我从对象获取数据如下,

List<SomeObject> obj = new ArrayList<SomeObject>();
obj.getItem1();
obj.getItem2();
Run Code Online (Sandbox Code Playgroud)

所有单独的项目都是字符串.如何填充表体内容?

java velocity

3
推荐指数
1
解决办法
1万
查看次数

在字符串集合中使用通配符进行搜索

我有一个HashMap<Integer,String>.我尝试了以下代码来查询Map并返回所有可能的值

public Collection<String> query(String queryStr) {
        List<String> list = new ArrayList<String>();
    for (Map.Entry<String, Integer> entry : myMap.entrySet()) {
        if (queryStr.matches(entry.getKey()))
            list.add(entry.getKey());
    }
    if (list.isEmpty())
        return null;
    else
        return list;
}
Run Code Online (Sandbox Code Playgroud)

如果地图有"test","best","crest","zest","testy","tether","temper","teat","tempest".te*t应该返回的查询"teat","tempest","test".对于'test*',它应该返回"test","testy".怎么实现呢?是否有任何通配符搜索字符串?我不能使用任何外部库.

java collections wildcard

3
推荐指数
1
解决办法
1万
查看次数

Java创建新对象的性能

我有以下课程.

class MyObject implements Serializable {
    private String key;
    private String val;
    private int num;

    MyObject(String a, String b, int c) {
        this.key = a;
        this.val = b;
        this.num = c;
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要创建一个对象列表,重复调用以下方法(比如10K或更多)

public void addToIndex(String a, String b, int c) {
    MyObject ob = new MyObject(a,b,c);
    list.add(ob); // List<MyObject>
}
Run Code Online (Sandbox Code Playgroud)

我使用分析器来查看内存占用,并且由于每次创建对象而增加了很多.有没有更好的方法呢?我正在将列表写入磁盘.

编辑:一旦列表完全填充,这就是我写的方式.一旦内存超出值(列表大小),是否有一种方法可以追加.

ObjectOutputStream oos = new ObjectOutputStream(
                        new DeflaterOutputStream(new FileOutputStream(
                                list)));
                oos.writeObject(list);
                oos.close();
Run Code Online (Sandbox Code Playgroud)

java performance file-io list object

2
推荐指数
1
解决办法
1455
查看次数

确保 jdbctemplate 中返回的列的顺序

我正在尝试使用 JDBCTemplate 检索给定查询的地图列表。以下方法似乎适合我的用例:

public List<Map<String,Object>> queryForList(String sql)
                                      throws DataAccessException
Run Code Online (Sandbox Code Playgroud)

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html#queryForList-java.lang.String-

但是,我想确保对于给定的行,映射的顺序是有保证的。即,LinkedHashMap 会更好,因为我想保留列的顺序。有没有更好的方法使用 JDBCTemplate 来实现这一点?

java spring jdbctemplate

2
推荐指数
1
解决办法
2091
查看次数

Java Regex取代所有

我的文字看起来像这样

| birth_date          = {{birth date|1925|09|2|df=y}}
| birth_place         = [[Bristol]], [[England]], UK
| death_date          = {{death date and age|2000|11|16|1925|09|02|df=y}}
| death_place         = [[Eastbourne]], [[Sussex]], England, UK
| origin              = 
| instrument          = [[Piano]]
| genre               = 
| occupation          = [[Musician]]
Run Code Online (Sandbox Code Playgroud)

我想得到[[]]里面的所有内容.我尝试使用replace all来替换[[]]内部的所有内容,然后使用split by new line来获取带[[]]的文本列表.

input = input.replaceAll("^[\\[\\[(.+)\\]\\]]", "");
Run Code Online (Sandbox Code Playgroud)

所需输出:

[[Bristol]]
[[England]]
[[Eastbourne]]
[[Sussex]]
[[Piano]]
[[Musician]]
Run Code Online (Sandbox Code Playgroud)

但这并没有给出理想的输出.我在这里失踪了什么?有成千上万的文件,这是获得它的最快方法吗?如果不是,请告诉我获得所需输出的最佳方法.

java regex

1
推荐指数
1
解决办法
131
查看次数

如何在返回void的方法上运行junit测试?

我无法更改需要测试的方法的签名.测试代码如下所示

Parser test = new Parser(props);
ArrayList<HDocument> list = new ArrayList<HDocument>();

test.parse("/users/mac/test.xml", list);
System.out.println("Size of list: "+list.size());
assertEquals(5, list.size());
Run Code Online (Sandbox Code Playgroud)

parse 方法签名如下

public void parse(String filename, Collection<HDocument> docs)
Run Code Online (Sandbox Code Playgroud)

解析方法运行正常,但是当我运行测试程序时,列表大小始终为0.我无法更改解析方法签名.我该怎么办?

这是Parser类,

class Parser{
private Collection<HDocument> docs;
     public void parse(String filename, Collection<HDocument> docs) {
        docs = new ArrayList<HDocument>();
        Collection<HDocument> docsFromXml = new ArrayList<HDocument>();

            Handler hl = new Handler();
            try {
                docsFromXml = hl.getDocs(filename);
            } catch (Exception e) {
                e.printStackTrace();
            }
            docs = docsFromXml;
            System.out.println("Size:"
                    + docs.size()); // This prints the size correctly …
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing

0
推荐指数
1
解决办法
409
查看次数