java hashset应用_三.java集合的应用

目录

需求一:存放学生信息

定义学生类,按年龄升序,将学生信息存放在集合中,遍历学生的信息(Student注释部分为后续的HashSet使用,此处可忽略)

思路:①定义一个学生类,声明name,age,grade...等属性,并自动生成toString,和构造方法

②向集合中加入学生信息

③对学生按年龄排序     思路:调用sort,重写Comparator接口进行排序

④遍历集合中的学生信息

1.定义一个学生类,声明name,age,grade...等属性,并自动生成toString,和构造方法

package com.bdsw.wxl.day5;

public class Student {//implements Comparable{

String name;

int age;

int grade;

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + age;

result = prime * result + grade;

result = prime * result + ((name == null) ? 0 : name.hashCode());

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Student other = (Student) obj;

if (age != other.age)

return false;

if (grade != other.grade)

return false;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

return true;

}

@Override

public String toString() {

return "Student [name=" + name + ", age=" + age + ", grade=" + grade + "]";

}

public Student(String name, int age, int grade) {

super();

this.name = name;

this.age = age;

this.grade = grade;

}

//@Override

//public int compareTo(Object o) {

//if(o instanceof Student) {

//if(this.age-((Student) o).age>0) return 1;

//else return -1;

//}

//return 0;

//}

}

2.向集合中加入学生信息,对学生进行排序

package com.bdsw.wxl.day5;

import java.util.ArrayList;

import java.util.Comparator;

import java.util.List;

import java.util.ListIterator;

/**

*

* @author 王雪亮

* @date 2018年7月20日

* @Description

* ①向集合中加入学生信息

* ②遍历集合中的学生

* ③怎样使List中不能添加重复元素 -->此时引入了Set集合,可使用HashSet实现

* ④怎么处理排序问题 -->使用TreeSet

*/

public class ListTest {

public static void main(String[] args) {

// 1.创建4个学生对象

Student s1 = new Student("a", 15, 4);

Student s2 = new Student("a", 15, 4);

Student s3 = new Student("c", 18, 24);

Student s4 = new Student("d", 20, 24);

// 2.构造List集合,并将学生对象加进List集合中

List al = new ArrayList();

al.add(s1);

al.add(s2);

al.add(s3);

al.add(s4);

// 3.迭代器遍历排序前的List

System.out.println("排序前的List为:");

ListIterator it = al.listIterator();

while (it.hasNext()) {

System.out.println(it.next());

}

// 4.List集合处理排序问题

// ①调用sort

// ②重写Comparator接口

Comparator c=new Comparator() {

@Override

public int compare(Object o1, Object o2) {

if (o1 instanceof Student && o2 instanceof Student) {

Student s1 = (Student) o1;

Student s2 = (Student) o2;

if (s1.age > s2.age)

return 1;

else

return -1;

}

return 0;

}

};

al.sort(c);

// 5.加强for循环遍历排序后的List

System.out.println("排序后的List为:");

for (Object object : al) {

System.out.println(object + " ");

}

}

}

需求二:要求学生信息不能重复存放

思路:此时引入了Set集合,可使用HashSet实现(无序列),也可使用LinkedHashSet实现(

注:①实现Set时,需要重写equals和hashCode方法

②HashSet底层是Hash表,LinkedHashSet底层是Hash表和链表

package com.bdsw.wxl.day5;

import java.util.LinkedHashSet;

import java.util.Set;

public class SetTest {

public static void main(String[] args) {

// 1.创建2个学生对象

Student s1 = new Student("a", 15, 4);

Student s2 = new Student("b", 15, 4);

// 2.构造List集合,并将学生对象加进List集合中

//若使用HashSet,则无序(HashSet底层时哈希表)

Set al = new LinkedHashSet();

al.add(s1);

al.add(s2);

al.add(new Student("c", 18, 24));

al.add(new Student("f", 18, 24));

al.add(new Student("e", 18, 24));

al.add(new Student("f", 18, 24));

al.add(new Student("g", 18, 24));

//3.加强for循环遍历Set集合

System.out.println("Set集合为:");

for (Object object : al) {

System.out.println(object+" ");

}

}

}

需求三:在不允许出现相同对象的情况下,如何进行按照年龄排序(也可以年龄为第一要素,成绩为第二要素排序)

思路:使用TreeSet

TreeSet两种排序方式:

1.自然排序:①Student类中实现 Comparable接口   ②重写Comparable接口中的Compareto方法

2.比较排序   ①构造TreeSet对象,并且要让其实现Comparator接口   ②重写Comparator接口中的Compare方法

自然排序在Student类中已经注释掉了,此处省略,以下展示比较排序

package com.bdsw.wxl.day5;

import java.util.Comparator;

import java.util.Set;

import java.util.TreeSet;

public class TreeSetTest {

public static void main(String[] args) {

// 1.创建4个学生对象

Student s1 = new Student("a", 15, 90);

Student s2 = new Student("b", 15, 80);

// 2.构造List集合,并将学生对象加进List集合中

Set al = new TreeSet(new Comparator() {

@Override

public int compare(Object o1, Object o2) {

if(o1 instanceof Student&&o2 instanceof Student) {

Student s1=(Student)o1;

Student s2=(Student)o2;

if(s1.age-s2.age>0) return 1;

else if(s1.age-s2.age<0) return -1;

else {

if(s1.grade-s2.grade>0) return 1;

else return -1;

}

}

return 0;

}

});

al.add(s1);

al.add(s2);

al.add(new Student("c", 18, 24));

al.add(new Student("f", 20, 98));

al.add(new Student("e", 20, 89));

al.add(new Student("f", 5, 34));

al.add(new Student("g", 5, 20));

//3.排序

/**

* 1.自然排序

* ①Student类中实现 Comparable接口

* ②重写Comparable接口中的Compareto方法

* 2.比较排序

* ①单独创建一个比较类,并且要让其继承Comparator接口

* ②重写Comparator接口中的Compare方法

*/

// 4.加强for循环遍历Set集合

System.out.println("Set集合为:");

for (Object object : al) {

System.out.println(object + " ");

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值