English 中文(简体)
在单一类别档案中多次使用比较器[复制]
原标题:Implementing comparator multiple times in a single class file [duplicate]

我有几部法典,按修改日期划分道路。 我也想写一些法典,以改变方向,然后可能想增加一些其他分类方法。 是否有办法从单一类别档案中进行分类? 或者,我不得不另设一个班子,即“开拓者”、“开拓者”、“开拓者”等。 此外,我如何使用不同的分类方法?

import java.nio.file.Path;
import java.util.Comparator;

public class PathSortByDate implements Comparator<Path> {

@Override
public int compare(Path first, Path second) {
    long seconddate = second.toFile().lastModified(); // get just the filename
    long firstdate = first.toFile().lastModified();

    if (firstdate == seconddate) {
        return 0;
    } else if (firstdate > seconddate) {
        return 1;
    } else {
        return -1;
    }
}
}

然后,我从另一类人打电话给:

public static ArrayList<Path> sortArrayListByDate(ArrayList<Path> pathlist) {
    Collections.sort(pathlist,new PathSortByDate());
    return pathlist;
}    
问题回答

为什么不上匿名内部课?

public static final Comparator<Person> ID_DESC
     = new Comparator<Person>() {
      public int compare(Person p1, Person p2) {
         return -1 * p1.getId().comparedTo(p2.getId());
         // reversed order
      }
    };

我通常这样做。 通知、建筑商是“私人”,有“公共工厂方法”来获得证明。 无论在什么时候,都会有两例“开拓者”事件。 如果你正在优化你的守则,并采用最佳做法,那是一件大事。

import java.nio.file.Path;
import java.util.Comparator;

final public class PathComparator implements Comparator<Path> {

// comparator for in order
final private static PathComparator ascendingOrderComparatorDate = new PathComparator(true);
// comparator for reverse order
final private static PathComparator descendingOrderComparatorDate = new PathComparator(false);

final private int isAscendingOrderInt;

final public PathComparator getPathComparator(boolean isAscendingOrder) {
    return isAscendingOrder ? ascendingOrderComparatorDate : descendingOrderComparatorDate;
}

private PathComparator(boolean isAscendingOrder) {
    this.isAscendingOrderInt = isAscendingOrder ? 1 : -1;
}

@Override
public int compare(Path first, Path second) {
    // for optimization (not required but highly recommended)
    if(first == second) return 0;

    long seconddate = second.toFile().lastModified(); // get just the filename
    long firstdate = first.toFile().lastModified();

    if (firstdate == seconddate) {
        return 0;
    } else if (firstdate > seconddate) {
        return isAscendingOrderInt * 1;
    } else {
        return isAscendingOrderInt * -1;
    }
}}

您迫切需要使反向比较者,使之与逆向比较,并与之相反。

Collections.reverseOrder()




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签