【参考答案】java基础练习:枚举、泛型
参考答案
list1=[A,B,C],list2=[B,C,A],对比两个列表,内容相同返回ture,不同返回false。要求定义一个泛型方法实现(解决类型侵入的问题)
说明:A可以任意类型的对象,
比如Student对象
List<Student> stu1s = new ArrayList<>(); stu1s.add(new Student("jack",18)); stu1s.add(new Student("tom",19)); List<Student> stu2s = new ArrayList<>(); stu2s.add(new Student("tom",19)); stu2s.add(new Student("jack",18));
也可以是String对象
String[] strArray = {"A","B","C"}; List list1 = Arrays.asList(strArray); String[] strArray2 = {"B","C","A"}; List list2 = Arrays.asList(strArray2);
实现:
Student.java
package com.qzcsbj; import java.util.Objects; /** * @公众号 : 全栈测试笔记 * @博客 : www.cnblogs.com/uncleyong * @微信 : ren168632201 * @描述 : <> */ public class Student implements Comparable<Student>{ private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } 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; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, age); } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public int compareTo(Student o) { // 根据年龄比较,升序 if(this.age>o.getAge()){ return 1; }else if(this.age<o.getAge()){ return -1; }else{ //如果年龄相同,则按姓名比较 return this.name.compareTo(o.getName()); } } }
Test.java
package com.qzcsbj; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; /** * @motto : 将来的你定会感谢现在拼命努力的自己。 * @description : <描述> * @博客 : www.cnblogs.com/uncleyong * @公众号 : 全栈测试笔记 * @作者微信: ren168632201 */ public class Test { public static void main(String[] args) { String[] strArray = {"A","B","C"}; List list1 = Arrays.asList(strArray); String[] strArray2 = {"B","C","A"}; List list2 = Arrays.asList(strArray2); System.out.println(compare(list1,list2)); List<Student> stu1s = new ArrayList<>(); stu1s.add(new Student("jack",18)); stu1s.add(new Student("tom",19)); List<Student> stu2s = new ArrayList<>(); stu2s.add(new Student("tom",19)); stu2s.add(new Student("jack",18)); System.out.println(compare(stu1s,stu2s)); } public static <T extends Comparable<T>> boolean compare(List<T> a, List<T> b) { if(a.size() != b.size()) return false; Collections.sort(a); Collections.sort(b); for(int i=0;i<a.size();i++){ if(!a.get(i).equals(b.get(i))) return false; } return true; } }
【java百题计划汇总】
详见:https://www.cnblogs.com/uncleyong/p/15828510.html
原文会持续更新,原文地址:https://www.cnblogs.com/uncleyong/p/17044004.html
__EOF__
本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!