java根据对象属性从对象列表中取出对象
现有两个类,A的实例存储在List中,classA{}classListextendArrayList<A>{}在后一个类中写一个方法,根据A的某个属性在List的实例中...
现有两个类,A的实例存储在List中,
class A{}
class List extend ArrayList<A>{}
在后一个类中写一个方法,根据A的某个属性在List的实例中提取出A的实例 展开
class A{}
class List extend ArrayList<A>{}
在后一个类中写一个方法,根据A的某个属性在List的实例中提取出A的实例 展开
1个回答
2018-06-20
展开全部
package test;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class AList extends ArrayList<A> {
/**
*
*/
private static final long serialVersionUID = -5626405721508120479L;
public A getSubject(String name, Object value)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, IntrospectionException {
A[] result = getSubjects(name, value);
if (result.length <= 0)
return null;
else
return result[0];
}
public A[] getSubjects(String name, Object value)
throws IntrospectionException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(name, A.class);
Method getter = pd.getReadMethod();
Iterator<A> it = this.iterator();
A[] result = new A[0];
while (it.hasNext()) {
A obj = it.next();
Object theValue = getter.invoke(obj);
if (theValue != null && theValue.equals(value)) {
result = Arrays.copyOf(result, result.length + 1);
result[result.length - 1] = obj;
}
}
return result;
}
public static void main(String[] args) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException,
IntrospectionException {
AList al = new AList();
A a = new A("张三");
a.setAge(18);
a.setGender("男");
a.setSchool("XX大学");
A b = new A("李四");
b.setAge(20);
b.setGender("男");
b.setSchool("XX大学");
A c = new A("王五");
c.setAge(18);
c.setGender("女");
c.setSchool("XX大学");
al.add(a);
al.add(b);
al.add(c);
System.out.println(Arrays.toString(al.getSubjects("name", "张三")));
System.out.println(Arrays.toString(al.getSubjects("age", 18)));
System.out.println(Arrays.toString(al.getSubjects("gender", "男")));
System.out.println(Arrays.toString(al.getSubjects("school", "XX大学")));
// [A [name=张三, age=18, gender=男]]
// [A [name=张三, age=18, gender=男], A [name=王五, age=18, gender=女]]
// [A [name=张三, age=18, gender=男], A [name=李四, age=20, gender=男]]
// [A [name=张三, age=18, gender=男], A [name=李四, age=20, gender=男], A [name=王五, age=18, gender=女]]
}
}
class A {
private String name;
private int age;
private String gender;
private String school;
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public A(String name) {
super();
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "A [name=" + name + ", age=" + age + ", gender=" + gender + "]";
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询