package cn.set;
import java.util.HashSet;
import java.util.Set;
class Student{
int id;
public Student(int id) {
this.id = id;
}
@Override
public String toString() {
return this.id+"";
}
@Override
public int hashCode() {
return this.id;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student){
Student stu = (Student) obj;
if (stu.id == this.id)
return true;
}
return false;
}
}
public class HashSetTest {
public static void main(String[] args) {
Set<Student> set = new HashSet<Student>();
Student s1 = new Student(1);
Student s2 = new Student(1);
Student s3 = new Student(2);
set.add(s1);
set.add(s2);
set.add(s3);
for (Student s : set) {
System.out.println(s);
}
}
}
package cn.set;
import java.util.Set;
import java.util.TreeSet;
class Student1 implements Comparable<Student1>{
int id;
public Student1(int id) {
this.id = id;
}
@Override
public String toString() {
return this.id+"";
}
@Override
public int hashCode() {
return this.id;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student1){
Student1 stu = (Student1) obj;
if (stu.id == this.id)
return true;
}
return false;
}
public int compareTo(Student1 o) {
return (this.id-o.id);
}
}
public class TreeSetTest {
public static void main(String[] args) {
Set<Student1> set = new TreeSet<Student1>();
Student1 s1 = new Student1(5);
Student1 s2 = new Student1(1);
Student1 s3 = new Student1(2);
Student1 s4 = new Student1(4);
Student1 s5 = new Student1(3);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
for (Student1 s : set) {
System.out.println(s);
}
}
}
package com.set;
import java.util.Set;
import java.util.TreeSet;
class Student1 implements Comparable<Student1>{
int id;
public Student1(int id) {
this.id = id;
}
@Override
public String toString() {
return this.id+"";
}
@Override
public int hashCode() {
return this.id;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Student1){
Student1 stu = (Student1) obj;
if (stu.id == this.id)
return true;
}
return false;
}
public int compareTo(Student1 o) {
return (this.id-o.id);
}
}
public class TreeSetTest {
public static void main(String[] args) {
Set<Student1> set = new TreeSet<Student1>();
Student1 s1 = new Student1(5);
Student1 s2 = new Student1(1);
Student1 s3 = new Student1(2);
Student1 s4 = new Student1(4);
Student1 s5 = new Student1(3);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
for (Student1 s : set) {
System.out.println(s);
}
}
}
package cn.set;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class MySort1 implements java.util.Comparator<Student3>{
public int compare(Student3 o1, Student3 o2) {
return o2.id-o1.id;
}
}
class Student3 implements Comparable<Student3>{
int id;
public Student3(int id) {
this.id = id;
}
@Override
public String toString() {
return this.id+"";
}
public int compareTo(Student3 o) {
return (this.id-o.id);
}
}
public class ListSort {
public static void main(String[] args) {
List<Student3> list = new ArrayList<Student3>();
Student3 s1 = new Student3(5);
Student3 s2 = new Student3(1);
Student3 s3 = new Student3(2);
Student3 s4 = new Student3(4);
Student3 s5 = new Student3(3);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
System.out.println(list);
//自然排序:
Collections.sort(list);
System.out.println(list);
//客户排序
Collections.sort(list, new MySort1());
System.out.println(list);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有