小编Coc*_*oco的帖子

Java 8 - 将 List<byte[]> 合并到 byte[] 的最有效方法

我有一个库,它返回一些二进制数据作为二进制数组列表。这些 byte[] 需要合并到一个 InputStream 中。

这是我当前的实现:

public static InputStream foo(List<byte[]> binary) {
    byte[] streamArray = null;
    binary.forEach(bin -> {
        org.apache.commons.lang.ArrayUtils.addAll(streamArray, bin);
    });
    return new ByteArrayInputStream(streamArray);
}
Run Code Online (Sandbox Code Playgroud)

但这对CPU来说是相当密集的。有没有更好的办法?

感谢所有的答案。我做了一个性能测试。这些是我的结果:

  • 函数:'NicolasFilotto' => 100 次调用平均耗时 68,04 毫秒
  • 函数:'NicolasFilottoEstSize' => 100 次调用平均 65,24 毫秒
  • 函数:'NicolasFilottoSequenceInputStream' => 100 次调用平均耗时 63,09 毫秒
  • 函数:'Saka1029_1' => 100 次调用平均 63,06 毫秒
  • 函数:'Saka1029_2' => 100 次调用的平均时间为 0.79 毫秒
  • 函数:'Coco' => 10 次调用平均 541,60 毫秒

我不确定“Saka1029_2”是否测量正确......

这是执行函数:

private static double execute(Callable<InputStream> funct, int times) throws Exception {
    List<Long> executions = new …
Run Code Online (Sandbox Code Playgroud)

java performance inputstream stream

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

JPA Hibernate 选择不同的 @ElementCollection 并进行比较

我正在将 Hibernate 与 JpaRepositories 一起使用。

实体类的相关部分是:

@Entity
public class Person {
   //.. id and other attributes

   @Column(name = "function")
   @ElementCollection
   private Set<String> functions;

  // .. getter setter
}
Run Code Online (Sandbox Code Playgroud)

我需要将我的实体类从只有一个函数更改为能够处理多个函数。我的一个 DAO 中有一个搜索函数,可以将所有现有函数与一个字符串进行比较,希望能找到已定义的函数。

最初的 JPA 查询是:

select DISTINCT(p.function) from Person p where UPPER(p.function) like UPPER(:term) order by p.function
Run Code Online (Sandbox Code Playgroud)

如何使用新的@ElementCollection 存档相同的结果?

java spring hibernate jpa

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

标签 统计

java ×2

hibernate ×1

inputstream ×1

jpa ×1

performance ×1

spring ×1

stream ×1