遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

【参考答案】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

 

【bak】

原文会持续更新,原文地址:https://www.cnblogs.com/uncleyong/p/17044004.html

 

posted @ 2023-01-14 15:31  全栈测试笔记  阅读(147)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end