Flask接口签名sign原理与实例代码浅析
280
2023-05-19
浅谈TreeSet中的两种排序方式
直接上代码:
package exercise1;
public class Persohttp://n implements Comparable{
private int id;
private String name;
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prhttp://ime * result + id;
result = primSdzqzJe * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getCSdzqzJlass() != obj.getClass())
return false;
Person other = (Person) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public int compareTo(Object o) {
if(o instanceof Person){
Person p=(Person)o;
return this.name.compareTo(p.name);
}
return 0;
}
}
package exercise1;
//TreeSet下的自然排序和定制排序
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import org.junit.Test;
//定制排序
public class DisOrder {
@Test
public void unNature(){
Comparator com=new Comparator() {
public int compare(Object o1, Object o2) {
if(o1 instanceof Person && o2 instanceof Person){
Person p1=(Person)o1;
Person p2=(Person)o2;
return p1.getName().compareTo(p2.getName());
}
return 0;
}
};
Set set=new TreeSet(com);
set.add(new Person(111,"MM"));
set.add(new Person(222,"DD"));
set.add(new Person(333,"GG"));
for(Object obj:set){
System.out.println(obj);
}
}
//自然排序
@Test
public void nature() {
Set set=new TreeSet();
set.add("aa");
set.add("bb");
set.add("ff");
set.add("zz");
for(Object obj:set){
System.out.println(obj);
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~