Java通用lambda调用(Java generic lambda call)
我有一个类,在构造函数中有一个接口对象:
private Operator(/* ..., */ final Operation<?> operation) { //... }
Operation是一个通用接口,有一个方法:
@SuppressWarnings("unchecked") public T applyOperation(T... arguments);
我现在可以像这样创建一个类操作符的Object:
public static final Operator POSTFIX_INCREASE = new Operator(/* ...,*/ new Operation<Integer>() { @Override public Integer applyOperation(Integer... arguments) { return arguments[0]++; } });
但我想用lambda表达式来做这个。 这是eclipse本身重新评估的内容(到目前为止看起来很好):
public static final Operator POSTFIX_INCREASE = new Operator("++", 0, 1, true, arguments -> arguments[0]++);
唯一的问题......它不起作用:D当然我知道,我需要提供类型信息:我需要一个操作员界面的整数实现。 在匿名类中,我可以提供默认方式,但在lambda表达式中,这是不可能的。 有没有办法写一个通用的lambda表达式,还是不支持它? (这不会是一个大惊喜,因为泛型在java中不是很灵活)
I got a class, that has a Interface Object in the constructor:
private Operator(/* ..., */ final Operation<?> operation) { //... }
Operation is a generic interface with one method:
@SuppressWarnings("unchecked") public T applyOperation(T... arguments);
I can now create an Object of class Operator like this:
public static final Operator POSTFIX_INCREASE = new Operator(/* ...,*/ new Operation<Integer>() { @Override public Integer applyOperation(Integer... arguments) { return arguments[0]++; } });
But I'd like to this with a lambda expression. This is what eclipse itself reconmends (And it looks good so far):
public static final Operator POSTFIX_INCREASE = new Operator("++", 0, 1, true, arguments -> arguments[0]++);
The only problem... It does not work :D Of course I know, i need to provide type information: I need an integer implementation of the Operator interface. In the anonymous class I can just provide it the default way, but in a lambda expression, this isn't possible. Is there any way to write a generic lambda expression, or don't they support that? (what wouldn't be a big surprise, since generics aren't very flexible in java)
原文:https://stackoverflow.com/questions/36956635
满意答案
我也是CoreData的新手,但在我看来,谓词是错误的。 根据日志记录,您将
personToDepartment.departmentName
与部门对象进行比较。 谓词应该如下所示:NSPredicate *predicate = [NSPredicate predicateWithFormat:@"personToDepartment.departmentName = %@", selectedDepartment.departmentName];
但还有更好的方法。
selectedDepartment.departmentToPerson
将返回一个NSSet,其中包含属于该部门的所有人员(如果先前已设置了关系)。 但是警告,如果你要尝试NSLog(@"%@", selectedDepartment.departmentToPerson)
你可能会得到“ 关系错误 ”,因为在你通过NSSet寻址特定对象或通过它枚举之前,CoreData不会进行获取。 但是例如NSLog(@"%d", selectedDepartment.departmentToPerson.count)
将强制CoreData进行获取,你将看到部门中的人数。更新:NSSet为空可能是因为您在创建人物对象时没有设置关系。 将自动设置从部门到人员的反向To-Many关系。
- (id)insertNewPerson:(NSDictionary *)personInfo inDepartment:(Department*)departmentObject { Person *personObject = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context]; personObject.name = personInfo.name; personObject.departmentName = departmentObject.name; ... personObject.personToDepartment = departmentObject; //this is RELATIONSHIP, if set, then you can access all persons from specific department by doing: departmentObject.departmentToPerson }
I'm also kind of newbie to CoreData, but it seems to me that predicate is wrong. According to Log record you're about to comparing
personToDepartment.departmentName
to a department Object. Predicate should look like:NSPredicate *predicate = [NSPredicate predicateWithFormat:@"personToDepartment.departmentName = %@", selectedDepartment.departmentName];
But there is a better way.
selectedDepartment.departmentToPerson
will return an NSSet with all persons belonging to this department (if relationship was set previously). But warning, if you'll try toNSLog(@"%@", selectedDepartment.departmentToPerson)
probably you'll get "relationship fault", because CoreData will not do fetch until you address specific object in NSSet or enumerate through it. But for exampleNSLog(@"%d", selectedDepartment.departmentToPerson.count)
will force CoreData to make fetch and you'll see number of persons in department.UPDATE: NSSet is empty probably because you are not setting relationship, when creating person's object. Inverse To-Many relationship from department to persons will be set automatically.
- (id)insertNewPerson:(NSDictionary *)personInfo inDepartment:(Department*)departmentObject { Person *personObject = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context]; personObject.name = personInfo.name; personObject.departmentName = departmentObject.name; ... personObject.personToDepartment = departmentObject; //this is RELATIONSHIP, if set, then you can access all persons from specific department by doing: departmentObject.departmentToPerson }
相关问答
更多CoreData - 一对多关系(CoreData - One-to-many relationship)
具有一对多关系的NSFetchedResultsController(NSFetchedResultsController with one-to-many relationship)
约束一对多关系(Constraint for one-to-many relationship)
iPhone Core Data关系错误(iPhone Core Data relationship fault)
ORMLite自身的一对多关系(ORMLite one-to-many relationship of itself)
将NSManagedObjects添加到一对多关系中(Adding NSManagedObjects to a one-to-many relationship)
识别一对多关系(Identifying one-to-many relationship)
添加setRelationshipKeyPathsForPrefetching后仍然显示获取一对多关系错误(Fetch one-to-many relationship fault still shows after adding setRelationshipKeyPathsForPrefetching)
如何管理Core Data中的一对多关系?(How to manage one-to-many relationship in Core Data?)
以一对多关系在列上创建索引(Create index on column in one-to-many relationship)
相关文章
更多java lambda表达式-JAVA8新特性
怎样用Struts2的lambda表达式取一个map集合的子集
Hadoop源码分析之二(RPC机制之Call处理)
Hadoop源码分析之RPC(Remote Procedure Call Protocol)
Solr Cache使用介绍及分析,包括LRUCache、filterCache、queryResultCache、documentCache、Generic Caches
[Hadoop Err] Call to ? failed on local exception: java.net.NoRouteToHostException: No route to host
java 调用oracle的存储过程返回记录集
java 调用oracle的存储过程返回记录集
RFC2848--The PINT Service Protocol: Extensions to SIP and SDP for IP Access to Telephone Call Services
java通用返回对象
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!