好吧,你不介意我写我的“测试”项目;首先,我创建了实现 AbstractMap 的类。
public class TestClass <K, V> extends AbstractMap <K, V>
Run Code Online (Sandbox Code Playgroud)
TestClass 具有作为参数的私有 LinkedList(它是另一个实现 Map.Entry 的类:
private int size = 1000;
private LinkedList <InfoClass <K, V>> [] array = new LinkedList [size];
Run Code Online (Sandbox Code Playgroud)
之后,我创建了检查和替换重复项的方法:
public V put (K key, V value){ // Void doesn't work, therefore we need to return any value;
V temp = null;
boolean found = false;
int index = Math.abs(key.hashCode()) % size;
if (array[index] == null)
array[index] = new LinkedList <InfoClass <K, V>> (); // If …Run Code Online (Sandbox Code Playgroud) 我有一个简单的类 - 人.
public class Person {
private int age;
public Person(int age) {
this.age = age;
}
public int GetAge() {
return age;
}
}
Run Code Online (Sandbox Code Playgroud)
我提出了10到20岁的人名单.现在,我想使用Collections.sort()方法对列表进行排序,但我不明白这是如何工作的.
public class Main {
public static void main (String [] args) throws IOException {
List<Person> list = new ArrayList<Person>();
list.add(new Person (11));
list.add(new Person (13));
list.add(new Person (32));
list.add(new Person (10));
Collections.sort(list, new Comparator <Person>() {
@Override
public int compare(Person a1, Person a2) {
return a1.GetAge() > a2.GetAge();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)