scala sortWith not work

时间:2016-05-26 05:45:19

标签: java scala sorting arraylist

我需要在scala上编写java ArrayList

它是类的分类方法,文件 - 字段。要使用ArrayList,我要导入java.util.ArrayList和scala.collection.JavaConversions ._

other imports ...
import java.io.File
import java.util.ArrayList
import scala.collection.JavaConversions._

class SortReplays(val files:ArrayList[File]) {  
def sort(kindOfSort:String) = {
    kindOfSort match {
      case "name" => files.sortWith(compareFileNames);
      case "length" => files.sortWith(compareGameLength);
      case "date" => files.sortWith(compareGameDates);
    }
  }

这是比较器之一,它也是SortReplays的方法

def compareGameLength(file1:File, file2:File) = {
    val length1:Long = ReplayViewerController.getGameLength(file1)
    val length2:Long = ReplayViewerController.getGameLength(file2)
    length1 < length2
  }

这就是我在java中所做的

Service.out("scala sorting by length");
      long t1 = System.currentTimeMillis();
      ArrayList<File> fileList = new ArrayList<File>(Arrays.asList(files));
      SortReplays sort = new SortReplays(fileList);
      sort.sort("length");
      Service.out("scala sort by length, time spend: " + (System.currentTimeMillis() - t1));
      try {
        controller.refreshLengths(fileList.toArray(new File[0]));
        controller.refreshReplays(fileList.toArray(new File[0]));
      } catch (Exception e1) {
        e1.printStackTrace();
      }

我原本预计会对文件进行排序 - 但不会发生,时间花费,但列表根本没有变化。 我也在java中做了同样的事情

public void sortLengths(ArrayList<File> files) {
    Collections.sort(files, new Comparator<File>() {

      @Override
      public int compare(File arg0, File arg1) {
        long l1;
        long l2;
        try {
          l1 = getGameLength(arg0);
          l2 = getGameLength(arg1);
          if(l1 < l2)
            return -1;
          else if(l1 == l2)
            return 0;
          else
            return 1;
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          return 0;
        }
      }

    });
  }

它按预期工作

1 个答案:

答案 0 :(得分:2)

问题是files.sortWithSortReplays.sort的来电回复Seq,但不会修改files列表。

要解决此问题,您需要:

首先,从List返回Java def sort。可以通过添加隐式转换器来解决:

import scala.collection.JavaConversions._
import scala.collection.JavaConverters._

...

private implicit def toJavaList[T](seq: Seq[T]): util.List[T] = {
  new util.ArrayList(seq.asJavaCollection)
}

def sort(kindOfSort: String): util.List[File] = {
  kindOfSort match {
    case "name" => files.sortWith(compareFileNames);
    case "length" => files.sortWith(compareGameLength);
    case "date" => files.sortWith(compareGameDates);
  }
}
顺便说一句,在Scala中为公共函数指定显式返回类型(在这种情况下为: util.List[File])是一种很好的做法。

ArrayList声明也应更改为List,以尊重程序到界面原则。

其次,在Java端代码中,需要进行一些更改:

SortReplays sort = new SortReplays(Arrays.asList(files));
List<File> fileList = sort.sort("length");
Service.out("scala sort by length, time spend: " + (System.currentTimeMillis() - t1));
try {
  controller.refreshLengths(fileList.toArray(new File[0]));
  controller.refreshReplays(fileList.toArray(new File[0]));
} catch (Exception e1) {
  e1.printStackTrace();
}

BTW,避免抓住Exception,这很难看。