通过他的属性获取一个元素
Get an Element by his attributes
我需要什么:获取一个元素
我从具有 2 个属性的 class 创建了两个对象:id 和 name。
创建对象后,我设置它的 ID 并将该对象添加到 ArrayList:
Variable variable1 = new Variable();
variable1.setID(1);
Variable variable2 = new Variable();
variable1.setID(2);
ArrayList<Variable> varList = new ArrayList<Variable>();
varList.add(variable1);
varList.add(variable2);
然后在我的代码中,我想获得 Id == 1 的元素,但我没有找到可以通过给它 Object 属性 return 我的对象的方法。
有没有这样的方法:Object o = getObjectByAttribute(Object.id==1) ?
您可以使用 Collections.binarySearch()
和自定义 Comparator
编辑: 代码片段:
Comparator<Variable> comparator = new Comparator<Variable>() {
public int compare(Variable o1, Variable o2) {
return Integer.compare(o1.getID(), o2.getID());
}
};
Collections.sort(varList, comparator);
Variable key = new Variable();
key.setID(1);
int index = Collections.binarySearch(varList, key, comparator);
如果您正在使用 java8,过滤器(在 lambda 表达式中)是一个不错的选择。
Stream<Variable> outputList = varList.stream().filter(val -> val.getId() ==1);
outputList 将仅包含 ID 为 1 的 Variable 对象。如果列表不为空,则可以从该列表中获取第一个元素。
参考:
http://zeroturnaround.com/rebellabs/java-8-explained-applying-lambdas-to-java-collections/
Is there a method like this : Object o =
getObjectByAttribute(Object.id==1) ?
不,但创建一个很容易。创建一个接口 Function
,从 T 类型的对象给出 U 类型的 属性。
interface Function<T, U> {
U apply(T t);
}
然后方法:
public static <T, U> T getObjectByAttribute(List<T> objects, Function<T, U> fromAttribute, U attributeResearched) {
for(T obj : objects) {
if(fromAttribute.apply(obj).equals(attributeResearched)) {
return obj;
}
}
return null;
}
以及如何称呼它:
Variable v = getObjectByAttribute(varList, new Function<Variable, Integer>() {
@Override
public Integer apply(Variable variable) {
return variable.id;
}
}, 1);
我需要什么:获取一个元素 我从具有 2 个属性的 class 创建了两个对象:id 和 name。 创建对象后,我设置它的 ID 并将该对象添加到 ArrayList:
Variable variable1 = new Variable();
variable1.setID(1);
Variable variable2 = new Variable();
variable1.setID(2);
ArrayList<Variable> varList = new ArrayList<Variable>();
varList.add(variable1);
varList.add(variable2);
然后在我的代码中,我想获得 Id == 1 的元素,但我没有找到可以通过给它 Object 属性 return 我的对象的方法。
有没有这样的方法:Object o = getObjectByAttribute(Object.id==1) ?
您可以使用 Collections.binarySearch()
和自定义 Comparator
编辑: 代码片段:
Comparator<Variable> comparator = new Comparator<Variable>() {
public int compare(Variable o1, Variable o2) {
return Integer.compare(o1.getID(), o2.getID());
}
};
Collections.sort(varList, comparator);
Variable key = new Variable();
key.setID(1);
int index = Collections.binarySearch(varList, key, comparator);
如果您正在使用 java8,过滤器(在 lambda 表达式中)是一个不错的选择。
Stream<Variable> outputList = varList.stream().filter(val -> val.getId() ==1);
outputList 将仅包含 ID 为 1 的 Variable 对象。如果列表不为空,则可以从该列表中获取第一个元素。
参考: http://zeroturnaround.com/rebellabs/java-8-explained-applying-lambdas-to-java-collections/
Is there a method like this : Object o = getObjectByAttribute(Object.id==1) ?
不,但创建一个很容易。创建一个接口 Function
,从 T 类型的对象给出 U 类型的 属性。
interface Function<T, U> {
U apply(T t);
}
然后方法:
public static <T, U> T getObjectByAttribute(List<T> objects, Function<T, U> fromAttribute, U attributeResearched) {
for(T obj : objects) {
if(fromAttribute.apply(obj).equals(attributeResearched)) {
return obj;
}
}
return null;
}
以及如何称呼它:
Variable v = getObjectByAttribute(varList, new Function<Variable, Integer>() {
@Override
public Integer apply(Variable variable) {
return variable.id;
}
}, 1);