comparator可以直接构造吗?因为比较器是一个接口而感到困惑。
comparator can be constructed directly? got confuse since comparator is a interface.
我看到它直接在这段代码中创建了一个 Comparator 并且它可以 运行 成功,我感到困惑,因为 Comparator 是一个 界面。我能找到的所有示例都是 类 实现的 Comparator 接口。
public class Solution
{
private Comparator<ListNode> ListNodeComparator = new Comparator<ListNode>(){
public int compare(ListNode left, ListNode right) {
if (left == null) {
return 1;
} else if (right == null) {
return -1;
}
return left.val - right.val;
}
};
public ListNode mergeKLists(ArrayList<ListNode> lists)
{
if (lists == null || lists.size() == 0) {
return null;
}
Queue<ListNode> heap = new PriorityQueue<ListNode>(lists.size(), ListNodeComparator);
for (int i = 0; i < lists.size(); i++) {
if (lists.get(i) != null) {
heap.add(lists.get(i));
}
}
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
while (!heap.isEmpty())
{
ListNode head = heap.poll();
tail.next = head;
tail = head;
if (head.next != null) {
heap.add(head.next);
}
}
return dummy.next;
}
}
它并不是真正直接创建比较器,那是一个 anonymous class,其中创建了一个没有名称的 class。这相当于:
class A implements Comparator<ListNode> {
@Override
public int compare(ListNode left, ListNode right) {
if (left == null) {
return 1;
} else if (right == null) {
return -1;
}
return left.val - right.val;
}
}
然后:
Comparator<ListNode> listNodeComparator = new A();
实际上,您在这里所做的是创建一个匿名的 class,它实现了接口:
private Comparator<ListNode> ListNodeComparator = new Comparator<ListNode>(){ // here starts the implementation
@Override
public int compare(ListNode left, ListNode right) {
if (left == null) {
return 1;
} else if (right == null) {
return -1;
}
return left.val - right.val;
}
};
其实是一样的:
public class MyClass implementsComparator<ListNode>(){
@Override
public int compare(ListNode left, ListNode right) {
if (left == null) {
return 1;
} else if (right == null) {
return -1;
}
return left.val - right.val;
}
};
如果您只需要在一个 class 中使用该实现一次,则无需创建单独的 class 实现。
我看到它直接在这段代码中创建了一个 Comparator 并且它可以 运行 成功,我感到困惑,因为 Comparator 是一个 界面。我能找到的所有示例都是 类 实现的 Comparator 接口。
public class Solution
{
private Comparator<ListNode> ListNodeComparator = new Comparator<ListNode>(){
public int compare(ListNode left, ListNode right) {
if (left == null) {
return 1;
} else if (right == null) {
return -1;
}
return left.val - right.val;
}
};
public ListNode mergeKLists(ArrayList<ListNode> lists)
{
if (lists == null || lists.size() == 0) {
return null;
}
Queue<ListNode> heap = new PriorityQueue<ListNode>(lists.size(), ListNodeComparator);
for (int i = 0; i < lists.size(); i++) {
if (lists.get(i) != null) {
heap.add(lists.get(i));
}
}
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
while (!heap.isEmpty())
{
ListNode head = heap.poll();
tail.next = head;
tail = head;
if (head.next != null) {
heap.add(head.next);
}
}
return dummy.next;
}
}
它并不是真正直接创建比较器,那是一个 anonymous class,其中创建了一个没有名称的 class。这相当于:
class A implements Comparator<ListNode> {
@Override
public int compare(ListNode left, ListNode right) {
if (left == null) {
return 1;
} else if (right == null) {
return -1;
}
return left.val - right.val;
}
}
然后:
Comparator<ListNode> listNodeComparator = new A();
实际上,您在这里所做的是创建一个匿名的 class,它实现了接口:
private Comparator<ListNode> ListNodeComparator = new Comparator<ListNode>(){ // here starts the implementation
@Override
public int compare(ListNode left, ListNode right) {
if (left == null) {
return 1;
} else if (right == null) {
return -1;
}
return left.val - right.val;
}
};
其实是一样的:
public class MyClass implementsComparator<ListNode>(){
@Override
public int compare(ListNode left, ListNode right) {
if (left == null) {
return 1;
} else if (right == null) {
return -1;
}
return left.val - right.val;
}
};
如果您只需要在一个 class 中使用该实现一次,则无需创建单独的 class 实现。