gpt4 book ai didi

java - 当我使用 Algebird 库时,如何使用 Java lambda 返回字节数组?

转载 作者:行者123 更新时间:2023-12-02 04:50:27 24 4
gpt4 key购买 nike

我正在使用 Algebird 库在 Java 中实现一个测试类来计算 HyperLogLog。这个库是用scala编写的,但我想在Java中使用它。在某些时候,我必须将 int 列表转换为字节数组列表,然后我必须使用 Java lambda 方法。我收到错误缺少返回语句。我在这里做错了什么?这是java代码:

import com.twitter.algebird.Approximate;
import com.twitter.algebird.HLL;
import com.twitter.algebird.HyperLogLogMonoid;
import scala.collection.TraversableOnce;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// import com.twitter.algebird.HyperLogLog.int2Bytes;

public class AlgebirdHLLAppJ {
public static void main(String[] args) {
System.out.println("This is the Spark test of the Algebird HyperLogLog application");

HyperLogLogMonoid hll = new HyperLogLogMonoid(4);
List<Integer> data = new ArrayList<Integer>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5));

ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.toByteArray();
hll.create(baos.toByteArray());
TraversableOnce<HLL> seqHll = data.stream().map(d -> {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(d);
hll.create(bb.array());
}); // ERROR: Missing return statement
HLL sumHll = hll.sum(seqHll);
Approximate<Object> approxSizeOf = hll.sizeOf(sumHll);
Integer actualSize = data.size();
Integer estimate = (Integer) approxSizeOf.estimate();
System.out.println("Actual size: " + actualSize);
System.out.println("Estimate size: " + estimate);
}
}

这是scala代码

import com.twitter.algebird.HyperLogLogMonoid
import com.twitter.algebird.HyperLogLog.int2Bytes

object AlgebirdHLLApp {
def main(args: Array[String]): Unit = {
println("This is the Spark test of the Algebird HyperLogLog application")

val hll = new HyperLogLogMonoid(4)
val data = List(1, 1, 2, 2, 3, 3, 4, 4, 5, 5)
val seqHll = data.map { hll.create(_) }
val sumHll = hll.sum(seqHll)
val approxSizeOf = hll.sizeOf(sumHll)
val actualSize = data.toSet.size
val estimate = approxSizeOf.estimate

println("Actual size: " + actualSize)
println("Estimate size: " + estimate)
}
}

最佳答案

尝试

import com.twitter.algebird.Approximate;
import com.twitter.algebird.HLL;
import com.twitter.algebird.HyperLogLog;
import com.twitter.algebird.HyperLogLogMonoid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import scala.collection.JavaConverters;

public class AlgebirdHLLAppJ {
public static void main(String[] args) {
System.out.println("This is the Spark test of the Algebird HyperLogLog application");

HyperLogLogMonoid hll = new HyperLogLogMonoid(4);
List<Integer> data = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5));
List<HLL> seqHll = data.stream().map(i -> hll.create(HyperLogLog.int2Bytes(i))).collect(Collectors.toList());
HLL sumHll = (HLL) hll.sum(JavaConverters.collectionAsScalaIterable(seqHll));

Approximate<Object> approxSizeOf = hll.sizeOf(sumHll);
int actualSize = new HashSet<>(data).size();
long estimate = (long) approxSizeOf.estimate();

System.out.println("Actual size: " + actualSize);
System.out.println("Estimate size: " + estimate);
}
}

关于java - 当我使用 Algebird 库时,如何使用 Java lambda 返回字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56446942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com