我希望能够测试List是否包含具有给定键值的对象
例如,我想做像 Iterables.contains(l2,“lname”,“Jordan”)); 之类的东西,而不必在l2中创建如下所有的其他Map对象< / p>
//List<String> l = Arrays.asList("Mickael", "Jordan", "His Airness");
//System.out.println(Iterables.contains(l, "Jordan"));
Map<String, String> p1 = new HashMap<String, String>();
p1.put("fname", "Mickael");
p1.put("lname", "Jordan");
p1.put("nname", "His Airness");
Map<String, String> p2 = new HashMap<String, String>();
p2.put("fname", "Paul");
p2.put("lname", "Pierce");
p2.put("nname", "The Truth");
List<Map<String, String>> l2 = Arrays.asList(p1, p2);
Map<String, String> p3 = new HashMap<String, String>();
p3.put("fname", "Mickael"); //
p3.put("lname", "Jordan");
p3.put("nname", "His Airness"); //
System.out.println(Iterables.contains(l2, p3));
我想知道是否有这样的番石榴的功能,而不是在l2上做一个循环并测试每个elt.get(“lname”)
修改 的
回答了3个解决方案:试图查看哪个更具性能
System.out.println(Iterables.any(l2, withEntry("lname", "Jordan"))); //@axtavt
System.out.println(has("lname", "Jordan")); //@JB
System.out.println(Iterables.any(l2, new KeyValuePredicate("lname", "Jordan"))); //@JB
public static Boolean has(final String key, final String value) {
return Iterables.any(l2, new Predicate<Map<String, String>>() {
@Override
public boolean apply(Map<String, String> input) {
return input.get(key).equals(value);
}
});
}
public static Predicate<Map<String, String>> withEntry(final String key, final String value) {
return new Predicate<Map<String, String>>() {
public boolean apply(Map<String, String> input) {
return value.equals(input.get(key));
}
};
}
class KeyValuePredicate implements Predicate<Map<String, String>>{
private String key;
private String value;
public KeyValuePredicate(String key, String value) {
super();
this.key = key;
this.value = value;
}
@Override
public boolean apply(Map<String, String> arg0) {
// TODO Auto-generated method stub
return arg0.get(key).equals(value);
}
}
答案 0 :(得分:1)
return Iterables.any(l2, new Predicate<Map<String, String>>() {
@Override
public boolean apply(Map<String, String> input) {
return input.get("lname").equals("Jordan");
}
});
但是当你应该使用具有属性的对象时,你正在使用地图。
当然,如果你需要多次使用各种属性,你应该将谓词转换为非匿名,可重用的类:
return Iterables.any(l2, new KeyValuePredicate("lname", "Jordan"));
答案 1 :(得分:0)
您可以实施适当的Predicate
并使用Iterables.any()
:
public Predicate<Map<String, String>> withEntry(final String key, final String value) {
return new Predicate<Map<String, String>>() {
public boolean apply(Map<String, String> input) {
return value.equals(input.get(key));
}
};
}
System.out.println(Iterables.any(l2, withEntry("lname", "Jordan")));
答案 2 :(得分:0)
这很简单。
您应该创建一个合适的实体类:
public class Person {
private String fName;
private String lName;
private String nName;
public Person(String fName, String lName, String nName) {
this.fName = fName;
this.lName = lName;
this.nName = nName;
}
public String getFName() {
return fName;
}
public String getLName() {
return lName;
}
public String getNName() {
return nName;
}
}
然后您可以执行以下操作:
import java.util.*;
public class Test {
public static void main (String [] args) {
List<Person> list = new ArrayList<Person>();
Person p1 = new Person("Mickael", "Jordan", "His Airness");
for (Person person : list) {
if (person.getFName().equals("Mickael")) {
System.out.println("Mickael is in the list!");
break;
}
}
}
}