Google Scala中Guava synchronizedQueue初始化错误

Google Guava synchronizedQueue initialization error in Scala

我正在尝试在 Scala 中初始化 Guava synchronizedQueue 以进行性能基准测试。

class TestSynchronizedQueueJavaPTS {
  private var assignedQForTest : java.util.Queue[Int] = null;
  private var syncQueue   : java.util.Queue[Int] = null;

  def create(DS_Type : String) : java.util.concurrent.ConcurrentLinkedQueue[Int] ={
     DS_Type match{
       case "syncQueue" =>
         syncQueue = com.google.common.collect.Queues.synchronizedQueue(com.google.common.collect.MinMaxPriorityQueue.[Int]create());
         assignedQForTest = syncQueue;
     }   
     assignedQForTest
  }
}

但是我收到这个错误:

identifier expected but '[' found.

错误来源:.[Int] 部分。

我有等效的 Java 代码,它运行良好,没有任何错误:

import java.util.Queue;
import com.google.common.collect.MinMaxPriorityQueue;
import com.google.common.collect.Queues;

public class test {
    public static void main(String[] args) {
        Queue<Integer> queue = Queues.synchronizedQueue(MinMaxPriorityQueue.<Integer>create());
        queue.add(15);
        queue.add(63);
        queue.add(20);
        System.out.println (queue.poll());
        System.out.println (queue.poll());
    }
}

类型参数应该跟在方法名称之后,如下所示。然后,还有另一个编译错误,因为 Scala 的 Int 不是 Comparable。我将其更改为 Integer 来解决这个问题,但也许您会更喜欢用不同的方式来解决该特定问题。

  private var assignedQForTest : java.util.Queue[Integer] = null;
  private var syncQueue   : java.util.Queue[Integer] = null;

  def create(DS_Type : String) : java.util.Queue[Integer] ={
     DS_Type match{
       case "syncQueue" =>
         syncQueue = com.google.common.collect.Queues.synchronizedQueue(com.google.common.collect.MinMaxPriorityQueue.create[Integer]());
         assignedQForTest = syncQueue;
     }   
     assignedQForTest
  }