Java 8 如何将带有谓词接口的泛型列表转换为Lambda表达式?
我正在学习Java8函数接口,并尝试了一些示例。 我正在尝试创建一个方法,该方法将接受泛型列表作为一个参数,接受字符串数据过滤器作为另一个参数。 下面的代码按预期工作,但当我试图将谓词转换为Lambda表达式时,我遇到了困难Java 8 如何将带有谓词接口的泛型列表转换为Lambda表达式?,java-8,predicate,Java 8,Predicate,我正在学习Java8函数接口,并尝试了一些示例。 我正在尝试创建一个方法,该方法将接受泛型列表作为一个参数,接受字符串数据过滤器作为另一个参数。 下面的代码按预期工作,但当我试图将谓词转换为Lambda表达式时,我遇到了困难 @SuppressWarnings("unchecked") public static <T> List<T> filter_and_find_only_selected_Data1(List<T> genericList, Strin
@SuppressWarnings("unchecked")
public static <T> List<T> filter_and_find_only_selected_Data1(List<T> genericList, String dataFilter){
Stream<List<T>> list = genericList.stream().map(eachListObj-> {
if(eachListObj instanceof Employee){
return genericList.stream().filter((Predicate<? super T>) new Predicate<Employee>() {
public boolean test(Employee eachEmpObj) {
return eachEmpObj.getEmpDept().equalsIgnoreCase(dataFilter);
}
}).collect(Collectors.toList());
}else if(eachListObj instanceof Customer){
return genericList.stream().filter((Predicate<? super T>) new Predicate<Customer>(){
public boolean test(Customer eachCust) {
return !eachCust.getCustomerName().equalsIgnoreCase(dataFilter);
}
}).collect(Collectors.toList());
}
return null;
});
return list.findAny().get();
}
@SuppressWarnings(“未选中”)
公共静态列表筛选器\u和\u仅查找\u\u选定的\u数据1(列表genericList,字符串数据筛选器){
Stream list=genericslist.Stream().map(eachListObj->{
if(员工的每个列表实例){
返回genericList.stream().filter((谓词为什么不将所有内容都放在筛选器中?试试这个
返回genericList.stream().filter(项->
(客户和((客户)项目的项目实例)。getCustomerName().equalsIgnoreCase(数据过滤器)
||(员工和((员工)项目的项目实例).getEmpDept().equalsIgnoreCase(数据过滤器)))
.collect(Collectors.toList());
或提取此筛选器的函数
public boolean isAllow(T项,字符串数据过滤器){
return(客户和((客户)项目的项目实例).getCustomerName().equalsIgnoreCase(数据过滤器))
||(员工和((员工)项目的项目实例).getEmpDept().equalsIgnoreCase(数据过滤器)))
}
//然后在过滤器中使用它
返回genericList.stream().filter(项->isAllow(项,数据过滤器)
.collect(Collectors.toList());
希望它有帮助,泛型在这里对您没有多大帮助,因为Customer
和Employee
似乎互不兼容。只要您想使用泛型类型
,就必须确保该类型在所有方法范围执行中都是一致的。您所能做的就是使用显式强制转换
我将从一个静态的映射开始
根据传入的类
提取映射函数。函数
将产生字符串
,只要您希望将其与数据过滤器
进行比较:
static Map<Class<?>, Function<Object, String>> exctractionMap() {
Map<Class<?>, Function<Object, String>> map = new HashMap<>();
map.put(Customer.class, item -> Customer.class.cast(item).getCustomerName());
map.put(Employee.class, item -> Employee.class.cast(item).getEmpDept());
return map;
}
staticmap,Function>Map=newhashmap();
map.put(Customer.class,item->Customer.class.cast(item.getCustomerName());
map.put(Employee.class,item->Employee.class.cast(item.getEmpDept());
返回图;
}
暂时把这个静态地图放在一边,我认为您的整个流程可能会被简化。这应该可以一起工作:
static List<String> findSelectedData(List<?> genericList, String dataFilter) {
return genericList.stream() // Stream<Object>
.map(item -> exctractionMap() // Stream<String> using the function
.get(item.getClass()) // ... get through Class<Object>
.apply(item)) // ... applied Function<Object,String>
.filter(s-> s.equalsIgnoreCase(dataFilter)) // Stream<String> equal to dataFilter
.collect(Collectors.toList()); // List<String>
}
静态列表findSelectedData(列表genericList、字符串数据筛选器){
返回genericList.stream()//流
.map(item->extractionmap()//使用函数的流
.get(item.getClass())/…通过类
.应用(项目))/…应用功能
.filter(s->s.equalsIgnoreCase(dataFilter))//流等于dataFilter
.collect(Collectors.toList());//列表
}
注意:请尊重Java约定,并将方法命名为filterandFindlyselectedData1
我认为,您实际上需要这样的名称:
public static <T> List<T> filter_and_find_only_selected_Data(
List<T> list, Function<? super T, String> stringProperty, String filterValue) {
return list.stream()
.filter(t -> filterValue.equalsIgnoreCase(stringProperty.apply(t)))
.collect(Collectors.toList());
}
公共静态列表过滤器和仅查找选定数据(
List,List,function接受任意T
的“泛型”方法没有多大意义,但只有当T
实际上是两种不相关的具体类型中的一种时,它才会做一些有用的事情。
List<Employee> source = …;
List<Employee> filtered
= filter_and_find_only_selected_Data(source, Employee::getEmpDept, "value");
List<Customer> source = …;
List<Customer> filtered
= filter_and_find_only_selected_Data(source, Customer::getCustomerName, "Bob");
List<File> source = Arrays.asList(new File("foo", "bar"), new File("foo", "test"),
new File("xyz"), new File("TEST"), new File("abc", "bar"), new File("bla", "Test"));
List<File> filtered = filter_and_find_only_selected_Data(source, File::getName, "test");