如何在java里面对Object的类型值进行一个大小的比较
2个回答
展开全部
作者:Tao Li
链接:https://www.zhihu.com/question/36897771/answer/69763955
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
java中类的比较一般有两种方法,一种就是类本身实现Comparable<T>这个接口,比如通过对用户的姓名进行比较排序:
package com.ailot.compare;
public class User implements Comparable<User> {
private String name;
private int age;
private String sex;
public User(String name,int age,String sex){
this.name = name;
this.age = age;
this.sex = sex;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
@Override
public int compareTo(User u) {
// TODO Auto-generated method stub
int c = this.name.compareTo(u.name);
if(c==0){
return 0;
}else if(c > 0){
return 1;
}else{
return -1;
}
}
}
Test.java
package com.ailot.compare;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Test {
public static void main(String[] args) {
List<User> userList = new ArrayList<User>();
for(int i=0;i<10;i++){
int s = new Random().nextInt(20);
User u = new User(s+"张三",i,"男");
userList.add(u);
}
Collections.sort(userList);
for(User user : userList){
System.out.println(user.toString());
}
}
}
排序的话直接用 Collections.sort( userList ) ;就可以了。
作者:Tao Li
链接:https://www.zhihu.com/question/36897771/answer/69763955
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
另一种就是类没有实现Comparable<T>这个接口,这就需要自己新建一个比较的类,通过实现Comparator<T>来对类进行比较,还是对User类的姓名进行比较排序:
User.java
package com.ailot.compare;
public class User {
private String name;
private int age;
private String sex;
public User(String name,int age,String sex){
this.name = name;
this.age = age;
this.sex = sex;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
UserCompare.java
package com.ailot.compare;
import java.util.Comparator;
public class UserCompare implements Comparator<User> {
@Override
public int compare(User u1, User u2) {
// TODO Auto-generated method stub
int c = u1.getName().compareTo(u2.getName());
if(c == 0){
return 0;
}else if(c < 0){
return -1;
}else{
return 1;
}
}
}
Test.java
package com.ailot.compare;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Test {
public static void main(String[] args) {
List<User> userList = new ArrayList<User>();
for(int i=0;i<10;i++){
int s = new Random().nextInt(20);
User u = new User(s+"张三",i,"男");
userList.add(u);
}
Collections.sort(userList,new UserCompare());
for(User user : userList){
System.out.println(user.toString());
}
}
}
排序的话直接用 Collections.sort(userList,new UserCompare());就可以了。
链接:https://www.zhihu.com/question/36897771/answer/69763955
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
java中类的比较一般有两种方法,一种就是类本身实现Comparable<T>这个接口,比如通过对用户的姓名进行比较排序:
package com.ailot.compare;
public class User implements Comparable<User> {
private String name;
private int age;
private String sex;
public User(String name,int age,String sex){
this.name = name;
this.age = age;
this.sex = sex;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
@Override
public int compareTo(User u) {
// TODO Auto-generated method stub
int c = this.name.compareTo(u.name);
if(c==0){
return 0;
}else if(c > 0){
return 1;
}else{
return -1;
}
}
}
Test.java
package com.ailot.compare;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Test {
public static void main(String[] args) {
List<User> userList = new ArrayList<User>();
for(int i=0;i<10;i++){
int s = new Random().nextInt(20);
User u = new User(s+"张三",i,"男");
userList.add(u);
}
Collections.sort(userList);
for(User user : userList){
System.out.println(user.toString());
}
}
}
排序的话直接用 Collections.sort( userList ) ;就可以了。
作者:Tao Li
链接:https://www.zhihu.com/question/36897771/answer/69763955
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
另一种就是类没有实现Comparable<T>这个接口,这就需要自己新建一个比较的类,通过实现Comparator<T>来对类进行比较,还是对User类的姓名进行比较排序:
User.java
package com.ailot.compare;
public class User {
private String name;
private int age;
private String sex;
public User(String name,int age,String sex){
this.name = name;
this.age = age;
this.sex = sex;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
UserCompare.java
package com.ailot.compare;
import java.util.Comparator;
public class UserCompare implements Comparator<User> {
@Override
public int compare(User u1, User u2) {
// TODO Auto-generated method stub
int c = u1.getName().compareTo(u2.getName());
if(c == 0){
return 0;
}else if(c < 0){
return -1;
}else{
return 1;
}
}
}
Test.java
package com.ailot.compare;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Test {
public static void main(String[] args) {
List<User> userList = new ArrayList<User>();
for(int i=0;i<10;i++){
int s = new Random().nextInt(20);
User u = new User(s+"张三",i,"男");
userList.add(u);
}
Collections.sort(userList,new UserCompare());
for(User user : userList){
System.out.println(user.toString());
}
}
}
排序的话直接用 Collections.sort(userList,new UserCompare());就可以了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询