HashSet AND TreeSet
Posted jiminluo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HashSet AND TreeSet相关的知识,希望对你有一定的参考价值。
package com.gxnu.edu.bean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.gxnu.edu.lqm.Month;
public class Person implements Comparable<Person>,Cloneable,Serializable{
private String name;
private double height;
private Month month;
private List<Integer> list;
{
list = new ArrayList<>();
list.addAll(Arrays.asList(1,2,3,4));
}
public void add(int i){
this.list.add(i);
}
public void printList(){
list.forEach(System.out::println);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Month getMonth() {
return month;
}
public void setMonth(Month month) {
this.month = month;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, double height, Month month) {
super();
this.name = name;
this.height = height;
this.month = month;
}
@Override
public String toString() {
return "Person [name=" + name + ", height=" + height + ", month=" + month + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(height);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((month == null) ? 0 : month.hashCode());
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;
Person other = (Person) obj;
if (Double.doubleToLongBits(height) != Double.doubleToLongBits(other.height))
return false;
if (month != other.month)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Person p) {
int i = this.getName().compareTo(p.getName());
if(i==0){
double d = this.getHeight()-p.getHeight();
if(d==0){
i=this.getMonth().ordinal()-p.getMonth().ordinal();
}else if(d<0){
i=-1;
}else{
i=1;
}
}
return i;
}
public Person clone(){
try {
return (Person) super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
package com.gxnu.edu.lqm.collection.test;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.junit.Test;
import com.gxnu.edu.bean.Person;
import com.gxnu.edu.lqm.Month;
public class SetEx {
@Test
public void test1(){
Set<Person> hashSet = new HashSet<>();
Person p = new Person("zhangsan",166,Month.APRIL);
hashSet.add(p);
p = new Person("lisi",176,Month.AUGUST);
hashSet.add(p);
p = new Person("wangwu",169,Month.AUGUST);
hashSet.add(p);
p = new Person("zhaoliu",186,Month.DECEMBER);
hashSet.add(p);
p = new Person("zhangsan",166,Month.APRIL);
hashSet.add(p);
p = new Person("liqi",188,Month.JULY);
hashSet.add(p);
p = new Person("wangba",178,Month.NOWENBER);
hashSet.add(p);
p = new Person("jiujiu",167,Month.JUNE);
hashSet.add(p);
p = new Person("xiaoxiao",187,Month.JANUARY);
hashSet.add(p);
p = new Person("xihua",169,Month.JANUARY);
hashSet.add(p);
p = new Person("zhangsan",166,Month.OTOBER);
hashSet.add(p);
List<Person> list = new LinkedList<>();
Person p2 = new Person("zhangsan",166,Month.MARCH);
list.add(p2);
p2 = new Person("zhangsan",166,Month.APRIL);
list.add(p2);
p2 = new Person("lisi",176,Month.AUGUST);
list.add(p2);
hashSet.forEach(System.out::println);
boolean b = hashSet.retainAll(list);
System.out.println("****"+b);
hashSet.forEach(System.out::println);
System.out.println("************使用removeIf***************");
hashSet.removeIf(pp->pp.getName().contains("li"));
hashSet.forEach(s->System.out.println(s));
}
@Test
public void testTree(){
Comparator<Person> cr1=Comparator.comparing(Person::getName);
Comparator<Person> cr2=Comparator.comparing(Person::getHeight);
Comparator<Person> cr3=Comparator.comparing(Person::getMonth);
Comparator<Person> com=cr1.thenComparing(cr2).thenComparing(cr3);
TreeSet<Person> set = new TreeSet<>(com);
Person p = new Person("zhangsan",166,Month.APRIL);
set.add(p);
p = new Person("lisi",176,Month.AUGUST);
set.add(p);
p = new Person("wangwu",169,Month.AUGUST);
set.add(p);
p = new Person("zhaoliu",186,Month.DECEMBER);
set.add(p);
p = new Person("zhangsan",166,Month.APRIL);
set.add(p);
p = new Person("liqi",188,Month.JULY);
set.add(p);
p = new Person("wangba",178,Month.NOWENBER);
set.add(p);
p = new Person("jiujiu",167,Month.JUNE);
set.add(p);
p = new Person("xiaoxiao",187,Month.JANUARY);
set.add(p);
p = new Person("xihua",169,Month.JANUARY);
set.add(p);
p = new Person("zhangsan",166,Month.OTOBER);
set.add(p);
set.forEach(s->System.out.println(s));
System.out.println("***********");
set.headSet(new Person("maocao",188,Month.APRIL));
set.forEach(System.out::println);
/*
List<Person> list = new LinkedList<>();
Person p2 = new Person("zhangsan",166,Month.MARCH);
list.add(p2);
p2 = new Person("zhangsan",166,Month.APRIL);
list.add(p2);
p2 = new Person("lisi",176,Month.AUGUST);
list.add(p2);
set.forEach(System.out::println);
boolean b = set.retainAll(list);
System.out.println("****"+b);
set.forEach(System.out::println);
System.out.println("************使用removeIf***************");
set.removeIf(pp->pp.getName().contains("li"));
set.forEach(s->System.out.println(s));*/
}
}
以上是关于HashSet AND TreeSet的主要内容,如果未能解决你的问题,请参考以下文章