站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题LinkedHashSet

LinkedHashSet

摘要: LinkedHashSet


能不能给一个这个类的简单例子?要使用equals()和hashCode()的


/**
*
*/
package net.mengfanpp;

import java.util.Set;
import java.util.LinkedHashSet;

/**
* 练习使用LinkedHashSet
*
* @author mengfanpp
*
*/
public class LinkedHashSetDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Set set = new LinkedHashSet();

set.add("mengfanpp");

set.add("monor");
set.add("bush");

//加入重复值
set.add("mengfanpp");

//使用增强的for循环显示对象
for(String name : set){
System.out.print(name + " ");
}
System.out.println();

}

}



package net.mengfanpp;

import java.util.Calendar;

/**
* 学生类
*
* @author mengfanpp
*
*/
public class Student {

private String id;

private String name;

private Boolean sex;

private Calendar birthday;



/**
* 学生类简单构造函数
*/
public Student() {
super();
// TODO Auto-generated constructor stub
}

/**
* @return Returns the birthday.
*/
public Calendar getBirthday() {
return birthday;
}

/**
* @param birthday
* The birthday to set.
*/
public void setBirthday(Calendar birthday) {
this.birthday = birthday;
}

/**
* @return Returns the id.
*/
public String getId() {
return id;
}

/**
* @param id
* The id to set.
*/
public void setId(String id) {
this.id = id;
}

/**
* @return Returns the name.
*/
public String getName() {
return name;
}

/**
* @param name
* The name to set.
*/
public void setName(String name) {
this.name = name;
}

/**
* @return Returns the sex.
*/
public String getSex() {
return sex == true ? "男":"女";
}

/**
* @param sex
* The sex to set.
*/
public void setSex(Boolean sex) {
this.sex = sex;
}

//在定义类时,最好总是重新定义equals()与hashCode()方法以及toString(),以符合Java的设计规范。

/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object other) {
// TODO Auto-generated method stub
if(this == other)
return true;

if(!(other instanceof Student))
return false;

final Student stud = (Student) other;

if(!id.equals(stud.getId())){
return false;
}

return true;
}

/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
// TODO Auto-generated method stub
return id.hashCode();
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuilder studInfo = new StringBuilder("");
studInfo.append("学号:"+id+" 的学生姓名为:"+name+",性别为:"+this.getSex()+",出生日期为:"+DateInfo.getStrFromDateTime(birthday,"yyyy-MM-dd") );

return studInfo.toString();
}

}


/**
*
*/
package net.mengfanpp;

import java.util.Set;
import java.util.LinkedHashSet;

/**
* 练习使用LinkedHashSet(加入Student对象)
*
* @author mengfanpp
*
*/
public class LinkedHashSetDemo2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Set set = new LinkedHashSet();

Student stud1 = new Student();
Student stud2 = new Student();
Student stud3 = new Student();
Student stud4 = new Student();

stud1.setId("001");
stud1.setName("mengfanpp");
stud1.setSex(true);
stud1.setBirthday(DateInfo.getDateTimeFromStr("1979-03-12","yyyy-MM-dd"));

stud2.setId("002");
stud2.setName("zhangtf");
stud2.setSex(true);
stud2.setBirthday(DateInfo.getDateTimeFromStr("1978-05-16","yyyy-MM-dd"));

stud3.setId("003");
stud3.setName("良葛格");
stud3.setSex(true);
stud3.setBirthday(DateInfo.getDateTimeFromStr("1975-08-10","yyyy-MM-dd"));

stud4.setId("001");
stud4.setName("mengfanpp");
stud4.setSex(true);
stud4.setBirthday(DateInfo.getDateTimeFromStr("1979-03-12","yyyy-MM-dd"));

/*
* 在一个Set中加入对象时,如果对象重复将被忽略而不加入;要实现对象可比较是否重复,
* 必须实现equals()和hashCode()方法,具体实现例子参见ch13/net.mengfanpp.Student
* **在定义类时,最好总是重新定义equals()与hashCode()方法以及toString(),以符合Java的设计规范。
*/

set.add(stud1);
set.add(stud2);
set.add(stud3);
set.add(stud4);//Set不可以加入重复对象,该对象将被忽略

//使用增强的for循环显示对象
for(Student stud : set){
System.out.println(stud.toString() + " ");
}
System.out.println();

}

}




给分吧!


太长了一点吧~~~~

能不能简洁一点,只要能反映equals()和hashCode()的作用就好~~


这还长???


短一点~~~~


不懂呀,给分啦


↑返回目录
前一篇: java中有这个吗?System.enviorment.newline,好像System里没有啊。
后一篇: 关于序列化接口