我编写了一个程序来在HashSet中插入数据......这里是代码
public class Person implements Comparable<Person>
{
private int person_id;
private String person_name;
public Person(int person_id,String person_name)
{
this.person_id=person_id;
this.person_name=person_name;
}
/* getter and setter method */
public boolean equals(Object obj)
{
Person p=(Person)obj;
if(!(p instanceof Person))
{
return false;
}
else if(this.person_id==p.person_id)
return true;
else
return false;
}
@Override
public int hashCode()
{
return person_id*6;
}
@Override
public int compareTo(Person o)
{
if(this.person_id>o.person_id)
return 1 ;
else if(this.person_id<o.person_id)
return -1;
else return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
我还没有粘贴其他两个类.我在这些类中所做的就是填充数据而其他类是主类.
现在我明白了,通过Java Doc Api我知道在Collections类中有一个名为sort()的方法.现在我的问题就是那个排序mathod的列表. …
我刚刚开始使用Collection,我已经编写了代码来测试 HashSet
这是代码(Person.java):
public class Person
{
int id;
public Person(int id)
{
this.id=id;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id=id;
}
public boolean equals(Object o)
{
if (o instanceof Person)
{
Person p=(Person)o;
if (this.id==p.id)
return false;
else
return true;
}
else return false;
}
public int hashCode()
{
return 21*id;
}
}
Run Code Online (Sandbox Code Playgroud)
和Implementation类,只是一个简单的类:
import java.util.*;
class HashSetTest
{
public static void main(String[] args)
{
Set<Person> set=new HashSet<Person>();
Person p1=new Person(6);
Person …Run Code Online (Sandbox Code Playgroud) 我试图使用反射打印构造函数名称但是它跳过用于打印循环名称的循环.
package reflection.com;
import java.lang.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
abstract interface FirstInterface {
void showFirstInterface();
}
abstract interface SecondInterface {
void showSecondInterface();
}
abstract interface ThirdInterface {
void showThirdinterface();
}
class SuperClass implements FirstInterface {
int x, y, z;
SuperClass() {
System.out.println("Super Class Constructor...");
}
public void showFirstInterface() {
System.out.println("In class Super Class....");
}
}
public class SubClass extends SuperClass implements SecondInterface, ThirdInterface {
int a, b, c;
SubClass() {
}
SubClass(int a, int b, int …Run Code Online (Sandbox Code Playgroud)