我的班级标题:
public class GraphEdge implements Comparable<GraphEdge>{
/** Node from which this edge starts*/
protected Point from;
/** Node to which this edge goes*/
protected Point to;
/** Label or cost for this edge*/
protected int cost;
Run Code Online (Sandbox Code Playgroud)
我的compareTo方法:
@Override
public int compareTo(GraphEdge other){
return this.cost-other.cost;
}
Run Code Online (Sandbox Code Playgroud)
但是Eclipse给了我错误:
GraphEdge类型的compareTo(GraphEdge)方法必须覆盖超类方法
whyyyyy?我尝试过做Comparable,用
@Override
public int compareTo(Object o){
GraphEdge other = (GraphEdge) o;
return this.cost-other.cost;
}
Run Code Online (Sandbox Code Playgroud)
但这也失败了.
我的insert方法应该有什么签名?我正在与仿制药斗争.在某种程度上,我想都Comparable<T>和T我有试过<Comparable<T> extends T>.
public class Node<T> {
private Comparable<T> value;
public Node(Comparable<T> val) {
this.value = val;
}
// WRONG signature - compareTo need an argument of type T
public void insert(Comparable<T> val) {
if(value.compareTo(val) > 0) {
new Node<T>(val);
}
}
public static void main(String[] args) {
Integer i4 = new Integer(4);
Integer i7 = new Integer(7);
Node<Integer> n4 = new Node<>(i4);
n4.insert(i7);
}
}
Run Code Online (Sandbox Code Playgroud) 我是Java的新手,并尝试实现扩展GeneralList接口的MyLinkedList,我想使用我的Node的类似接口来保持我的列表排序,当我尝试创建头节点时,它给了我错误
请在以下代码下方找到错误消息
//List interface
public interface GeneralList<T>
{
public boolean addNode(T elem);
public boolean deleteNode(T elem);
public T containsNode(T elem);
public void printSll();
}
//ListImplementation
public class SLL2<T> implements GeneralList<T>
{
private static class Node<T extends Comparable<T>>
{
public T data;
public Node<T> next;
public Node()
{
data = null;
next = null;
}
}
public SLL2()
{
head = null;
}
/* 1. Error while creating a head referance*/
private Node<T> head;
@Override
public boolean addNode(T elem)
{
Node<T> …Run Code Online (Sandbox Code Playgroud) 在Comparable的契约中,没有任何东西强迫一个对象与它自己相比较。只是
强烈推荐,但不严格要求 (x.compareTo(y)==0) == (x.equals(y))
这意味着,它的建议对于x.compareTo(x)不扔。但是可以写一个
class X implements Comparable<Y> {
...
}
Run Code Online (Sandbox Code Playgroud)
其中X和Y是两个不相关的类。我看不出它有什么好处,但在 Java 8 版本中HashMap甚至有相应的检查。
X implements Comparable<Y>使用两个不相关的类来实现?我想答案是肯定的和否定的,但这只是一个猜测
我有像Person这样的自定义类:
public class Person {
int age;
String name;
}
Run Code Online (Sandbox Code Playgroud)
现在我想Person基于类对对象进行排序age.
所以我将使用Comparable接口和实现compareTo()方法.
而且compareTo会有基于刚才比较人物对象的逻辑age.
所以,如果我这样做:
Collections.sort(list); // where list is a list of person
Run Code Online (Sandbox Code Playgroud)
我会基于的获得排序人名单age.
但我在某处读到,我们equals()在Comparable执行时也需要覆盖方法.
但我现在还没有看到它的使用.
任何人都可以解释一下,equals()如果我想sort基于age什么,还需要覆盖方法?
我希望能够写出这样的东西:
Fruit f1 = new Apple();
Fruit f2 = new Orange();
int res = f1.compareTo(f2);
Run Code Online (Sandbox Code Playgroud)
在fruit类中实现Comparable接口,如下所示:
public class Fruit<T> implements Comparable<? extends T> {
int compareTo(T other) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
似乎没有用.我猜在通配符中有关键字super的一些技巧...
Employee.java
class Employee implements Comparable
{
int id; String name; int age;
Employee(int id,String name,int age)
{
this.id=id;
this.name=name;
this.age=age;
}
public int compareTo(Object obj)
{
Employee emp = (Employee)obj;
if(age==emp.age)
{
return 0;
}
//else if(age>emp.age)
//return 1;
else
return -1;
}
}
Run Code Online (Sandbox Code Playgroud)
display_logic.java
import java.util.*;
class display_logic
{
public static void main(String args[])
{
ArrayList al = new ArrayList();
al.add(new Employee(1,"Supreeth",21));
al.add(new Employee(2,"Vijay",31));
al.add(new Employee(3,"Ganesh",21));
al.add(new Employee(4,"Aisu",31));
al.add(new Employee(5,"Aizzz",41));
Collections.sort(al);
Iterator it = al.iterator();
while(it.hasNext())
{ …Run Code Online (Sandbox Code Playgroud) 我有一个通用二叉树,它会向左边添加更少或者相等的对象,以及比右边更大的对象.我的问题是比较泛型,我知道数据值将是一个对象包装的原语或一个字符串,所以它们是可比较的.但是,我不知道如何在代码中实现它.
代码是正在进行的工作,我知道添加方法还没有正确添加,但我正在努力.谢谢
这是TreeNode:
public class TreeNode<T>
{
//Instance Variables
TreeNode leftChild;
TreeNode rightChild;
int childCount;
int depth;
T data;
public TreeNode(T data, int parentDepth)
{
leftChild = null;
rightChild = null;
childCount = 0;
depth = parentDepth + 1;
this.data = data;
}
public TreeNode(int parentDepth)
{
leftChild = null;
rightChild = null;
childCount = 0;
depth = parentDepth + 1;
data = null;
}
public void add(T data)
{
if (this.data.compareTo(data) <= 0)
{
addLeft(data);
} else if (this.data.compareTo(data) …Run Code Online (Sandbox Code Playgroud) 这是我第一次HashMap在Java中订购.我需要通过密钥来做到这一点,但在我的情况下,密钥是一个对象,所以我需要按特定字段排序.试图通过我自己来计算它我已经考虑继续这个简单的代码划痕:
private HashMap<SimpleDBField, String> sortTable(HashMap<SimpleDBField, String> row){
LinkedHashMap<SimpleDBField, String> orderedRow = new LinkedHashMap<SimpleDBField, String>();
for(int i = 1; i <= row.size(); i ++){
Iterator iterator = row.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<SimpleDBField, String> entry = (Map.Entry<SimpleDBField, String>) iterator.next();
if(entry.getKey().getListPosition()==i){
orderedRow.put(entry.getKey(), entry.getValue());
break;
}
}
}
return orderedRow;
}
Run Code Online (Sandbox Code Playgroud)
假设它有效并且我不关心性能,在真正使用它之前,我想知道下一个代码划痕是否更好,最重要:为什么?
下面的示例源代码:如何按Java中的键和值对HashMap进行排序
public static <K extends Comparable,V extends Comparable> Map<K,V> sortByKeys(Map<K,V> map){
List<K> keys = new LinkedList<K>(map.keySet());
Collections.sort(keys);
Map<K,V> sortedMap = new LinkedHashMap<K,V>();
for(K key: keys){
sortedMap.put(key, map.get(key));
}
return sortedMap;
} …Run Code Online (Sandbox Code Playgroud) 这两个版本是否有任何区别(例如性能,订购):
版本1:
mylist.sort(myComparator.sort_item);
mylist.sort(myComparator.sort_post);
Run Code Online (Sandbox Code Playgroud)
版本2:
// java 8
mylist.sort(myComparator.sort_item
.thenComparing(myComparator.sort_post));
Run Code Online (Sandbox Code Playgroud)