关闭

day14集合中的map系列(hashset,treeset)

标签: javaset
42人阅读 评论(0) 收藏 举报
分类:
import java.util.*;


/*
往hashSet集合中存入自定义对象
姓名和年龄相同为同一人,重复元素


*/
class HashSetTest
{
public static void sop(Object obj)
{
System.out.println(obj);
}

public static void main(String[] args) 
{
HashSet hs=new HashSet();


hs.add(new Person("a1",11));
hs.add(new Person("a2",12));
hs.add(new Person("a3",13));
hs.add(new Person("a4",14));


Iterator it=hs.iterator();


while (it.hasNext())
{
Person p=(Person)it.next();
sop(p.getName()+"....."+p.getAge());
}


}
}
class Person
{
private String name;
private int age;
Person(String name,int age)
{
this.name=name;
this.age=age;
}
public int hashCode()
{
return name.hashCode()+age;
}
public boolean equals(Object obj)
{
if (!(obj instanceof Person))
{
return false;
}
Person p=(Person) obj;


return this.name.equals(p.name)&&this.age==p.age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}

}

----------------------------

import java.util.*;
import java.lang.Comparable;
/*


需求:
往treeset集合中存储自定义对象学生。
想按照学生的年龄进行排序。

TreeSet第一种方式
*/
class TreeSetDemo 
{
public static void main(String[] args) 
{
TreeSet ts=new TreeSet();


ts.add(new Student("lisi02",22));
ts.add(new Student("lisi007",20));
ts.add(new Student("lisi09",19));
ts.add(new Student("lisi01",40));

Iterator it=ts.iterator();
while(it.hasNext()) 
{
Student stu=(Student)it.next();
System.out.println(stu.getName()+"..."+stu.getAge());
}
}

}

class Student implements Comparable
{
private String name;
private int age;
Student(String name,int age)
{
this.name=name;
this.age=age;
}
public int compareTo(Object obj)
{
//return 0;1-1
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");

Student s=(Student)obj;
if (this.age>s.age) 
return 1;
if (this.age==s.age)
{
return 0;
}
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
} //该接口强制让学生具有比较性

-----------------------------------

import java.util.*;
/*
TreeSet第二种方式
*/
class Student implements Comparable
{
private String name;
private int age;
Student(String name,int age)
{
this.name=name;
this.age=age;
}
public int compareTo(Object obj)
{
//return 0;1-1
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");


Student   s= (Student)obj;


if (this.age>s.age)
return 1;
if (this.age==s.age)
{
return this.name.compareTo(s.name);
}
return -1;
}


public String getName()
{
return name;
}


public int getAge()
{
return age;
}
}

class TreeSetDemo2 
{
public static void main(String[] args) 
{
TreeSet ts=new TreeSet();

ts.add(new Student("lisi02",22));
ts.add(new Student("lisi007",20));
ts.add(new Student("lisi09",19));
ts.add(new Student("lisi01",40));

Iterator it=ts.iterator();
while(it.hasNext()) 
{
Student stu=(Student)it.next();
System.out.println(stu.getName()+"..."+stu.getAge());
}
}
}
class MyCompare implements Comparator
{
public int compare(Object o1,Object o2)
{
Student s1=(Student)o1;
Student s2=(Student)o2;
int num=s1.getName().compareTo(s2.getName());
if (num==0)
{
new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
/*if(s1.getAge()>s2.getAge())
return 1;
if(s1.getAge()==s2.getAge())
return 0;
return -1;*/
}
return num;
}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4647次
    • 积分:495
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:47篇
    • 译文:30篇
    • 评论:0条
    文章分类
    文章存档