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 }
相关问答
更多-
您要执行的操作的文档位于此链接的“To-Many Relationships”标题下。 这是一个简短的例子。 首先,我建议将您的关系名称更改为更直观的内容。 它真的会有所帮助: class TrainingDay: NSManagedObject { @NSManaged var day: String @NSManaged var trainingDetails: NSSet } class TrainingDetails: NSManagedObject { // ... ot ...
-
具有一对多关系的NSFetchedResultsController(NSFetchedResultsController with one-to-many relationship)[2022-05-19]
您的数据模型应具有互惠关系,以便在获取任何特定对象时,您可以立即访问所有相关对象。 在您的情况下,具有互惠关系的数据模型看起来像: City{ company<-->Company.city } Company{ city<-->City.company employees<-->>Employee.company } Employee{ company<<-->Company.employees } 因此,如果您有一个Employee对象,您可以找到self.comp ... -
约束一对多关系(Constraint for one-to-many relationship)[2022-02-07]
从模式的角度来看,这种约束是不可能的,因为你遇到了“鸡还是蛋”类型的场景。 在这种情况下,当我插入到父表中时,我必须在子表中有一行,但在父表中有一行之前,我不能在子表中有一行。 这是更好地执行客户端。 Such a constraint isn't possible from a schema perspective, because you run into a "chicken or the egg" type of scenario. Under this sort of scenario, when ... -
“错误”并不意味着错误,它只是意味着返回的对象是一个“幽灵”而没有读入其属性。为关系的另一方获取错误是正常的,因为你没有设置的提取通过其关系从一个不受控制的对象创建级联。 当您访问故障的属性时,它将作为一个功能齐全的对象出现故障。 编辑: 来自评论: 问题是我通过NSLog请求这种关系,但我仍然无法得到关系实体。 不你不是。 您只需要自己请求Items实体然后记录它们。 他们按照预期为他们的关系返回错误。 只有当你问每一个关系另一边的实际对象时,你才能保证看到一个对象而不是一个错误。 这就是你需要强制关系中 ...
-
我似乎通过将private ForeignCollection
替换为Collection 来解决了这个问题。 如果我使用ForeignCollection作为类型,我不太确定为什么它不起作用,但它似乎解决了我所有的问题。 I seemed to have solved the issue by replacing private ForeignCollection to just Collection . I'm not quite sure why it ... -
您无法直接向集合添加对象。 您需要使用Core Data生成的访问者或您自己的自定义访问者。 查看.h文件,您应该看到类似的内容 -(void)addClsObject:(Cls *)theObject; 和 -(void)addClsObjects:(NSSet *)set; 因此,对于您的特定情况,您将在拥有Cls对象后执行以下操作: [student addClassesObject:cls]; 然后执行保存,您应该能够检索设置的类。 仅供参考,调用此方法和relatd NSSet relatd ...
-
识别一对多关系(Identifying one-to-many relationship)[2021-10-06]
如果城市的密钥是(country_id,city_id),那么关系是“识别” - 意味着主键部分或全部是对另一个表的外键引用。 如果country_id不是主键的一部分,那么它是非标识的。 这两个不同的键会使表格在每种情况下代表非常不同的东西,但只有你可以说哪个更适合你的要求。 不要过分担心识别与非识别关系的概念。 这是一个源于ER建模的概念,但在关系数据库设计中,它通常具有很小的实际意义。 If the key of cities is (country_id, city_id) then the rel ... -
我也是CoreData的新手,但在我看来,谓词是错误的。 根据日志记录,您将personToDepartment.departmentName与部门对象进行比较。 谓词应该如下所示: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"personToDepartment.departmentName = %@", selectedDepartment.departmentName]; 但还有更好的方法。 selectedDepartmen ...
-
如何创建帐户和部门对象以便建立一对多的关系? 如果您在模型中配置了核心数据,核心数据将自动为您填充反向关系。 因此,根据您构建代码的方式,您可以将newAccount.department = aDepartment; 你的关系和逆都被创造了。 我应该怎么做CoreDataGeneratedAccessors?创建之后,我应该调用CachedDepartment的-addAddressesObject函数,还是需要创建类似anAccount.department = aDepartment东西,那就是它? ...
-
起初我没有得到你想要的东西,但我想我弄清楚了。 您定义了list-index ,用于保存java.util.List的索引,而不是Child的parent_id。 所以你正在寻找如何创建真正的数据库索引。 这可以通过这样的注释轻松完成; @OneToMany(cascade = { CascadeType.ALL }) @JoinColumn(name="parent_id") @Index(name = "idx_parent_id", columnNames = "parent_id") private ...