一、创建一个Student类,有成员变量name和cardId。如果两个学生对象的姓名和学号一样视为同一个学生,在 HashSet中添加学生对象 并遍历打印学生信息。
HashSet中,添加元素时,只有当对象的hashcode一样且equals判断两个对象为一个对象时,才会达到去重效果,因此需要重写这两个方法
package com.dream.work01;
public class Student{
private String name;
private String cardId;
public Student() {
super();
}
public Student(String name, String cardId) {
super();
this.name = name;
this.cardId = cardId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getcardId() {
return cardId;
}
public void setcardId(String cardId) {
this.cardId = cardId;
}
@Override
public String toString() {
return "Student [name=" + name + ", cardId=" + cardId + "]";
}
@Override
public boolean equals(Object obj) {
if (this==obj) {
return true;
}
if (obj instanceof Student) {
Student student=(Student) obj;
if (this.name.equals(student.name) && this.cardId.equals(student.cardId)) {
return true;
}
}
return false;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.name.hashCode()+this.cardId.hashCode();
}
}
package com.dream.work01;
import java.util.HashSet;
public class Test01 {
/*1.创建一个Student类,有成员变量name和cardId。
如果两个学生对象的姓名和学号一样视为同一个学生,
在 HashSet中添加学生对象 并遍历打印学生信息。*/
public static void main(String[] args){
HashSet<Student> set = new HashSet<>();
set.add(new Student("大熊猫", "001"));
set.add(new Student("中熊猫", "002"));
set.add(new Student("小熊猫", "003"));
set.add(new Student("大熊猫", "001"));
for (Student student : set) {
System.out.println(student);
}
}
}
二、向TreeSet集合中加入5个员工的对象,根据员工的年龄(升序)进行排序,若年龄相同,再根据工龄(降序)来排序,若工龄相同,根据薪水(降序)排序
TreeSet中,排序的基础是比较器,因此要达到自定义排序需要重写比较器,实现comparable接口
package com.dream.work02;
public class Staff implements Comparable<Staff>{
private String name;
private int age;
private int workYears;
private int salary;
public Staff() {
super();
}
public Staff(String name, int age, int workYears, int salary) {
super();
this.name = name;
this.age = age;
this.workYears = workYears;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWorkYears() {
return workYears;
}
public void setWorkYears(int workYears) {
this.workYears = workYears;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Staff [name=" + name + ", age=" + age + ", workYears=" + workYears + ", salary=" + salary + "]";
}
@Override
public int compareTo(Staff o) {
if (this.age!=o.age) {
return this.age-o.age;
}
if (this.workYears!=o.workYears) {
return -(this.workYears-o.workYears);
}
if (this.salary!=o.salary) {
return -(this.salary-o.salary);
}
return 1;
}
}
package com.dream.work02;
import java.util.TreeSet;
//向TreeSet集合中加入5个员工的对象,
//根据员工的年龄(升序)进行排序,若年龄相同,
//再根据工龄(降序)来排序,若工龄相同,根据薪水(降序)排序
public class Test01 {
public static void main(String[] args){
TreeSet<Staff> set = new TreeSet<>();
set.add(new Staff("张三", 19, 1, 1000));
set.add(new Staff("李四", 19, 2, 1000));
set.add(new Staff("王五", 19, 2, 2000));
set.add(new Staff("赵六", 20, 2, 2000));
for (Staff staff : set) {
System.out.println(staff);
}
}
}
三、向HashMap集合中key位置加入姓名,value位置加入年龄,HashMap依据value排序
HashMap本身不提供排序实现,因此需要借助外部类实现
package com.dream.work03;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class Test01 {
// 向HashMap集合中key位置加入姓名,value位置加入年龄,HashMap依据value排序
public static void main(String[] args){
HashMap<String, Integer> map = new HashMap<>();
map.put("哈哈", 19);
map.put("呵呵", 20);
map.put("嘿嘿", 17);
Set<Entry<String,Integer>> entrySet = map.entrySet();
ArrayList<Entry<String, Integer>> arrayList = new ArrayList<>(entrySet);
arrayList.sort(new Comparator<Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
// TODO Auto-generated method stub
return o1.getValue()-o2.getValue();
}
});
for (Entry<String, Integer> entry : arrayList) {
System.out.println(entry.getKey()+"--"+entry.getValue());
}
}
}
四、向TreeMap集合中key位置加入8个书本对象,value的位置存储书本类别,TreeMap依据价格排序
TreeMap排序是在key上的,如果要实现自定义排序,也是要重写对象的comparable接口的
package com.dream.work04;
public class Book implements Comparable<Book>{
private String name;
private double price;
public Book() {
super();
}
public Book(String name,double price) {
super();
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book [name=" + name + ", price=" + price + "]";
}
@Override
public int compareTo(Book o) {
if (this.price!=o.price) {
return (int) (this.price-o.price);
}
return 1;
}
}
package com.dream.work04;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
/*向TreeMap集合中key位置加入8个书本对象,
value的位置存储书本类别,TreeMap依据价格排序*/
public class Test01 {
public static void main(String[] args){
TreeMap<Book,String> map = new TreeMap<>();
map.put(new Book("java", 100),"程序类书籍");
map.put(new Book("python", 90),"程序类书籍");
map.put(new Book("html", 80),"程序类书籍");
map.put(new Book("西方哲学史", 60),"文学书籍");
map.put(new Book("思考快与慢", 50),"心理学书籍");
Set<Entry<Book,String>> entrySet = map.entrySet();
for (Entry<Book, String> entry : entrySet) {
System.out.println(entry.getKey()+"---"+entry.getValue());
}
}
}