首页 \ 问答 \ Java通用lambda调用(Java generic lambda call)

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
更新时间:2023-12-14 21:12

最满意答案

我也是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 to NSLog(@"%@", 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 example NSLog(@"%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 ...
  • 您的数据模型应具有互惠关系,以便在获取任何特定对象时,您可以立即访问所有相关对象。 在您的情况下,具有互惠关系的数据模型看起来像: City{ company<-->Company.city } Company{ city<-->City.company employees<-->>Employee.company } Employee{ company<<-->Company.employees } 因此,如果您有一个Employee对象,您可以找到self.comp ...
  • 从模式的角度来看,这种约束是不可能的,因为你遇到了“鸡还是蛋”类型的场景。 在这种情况下,当我插入到父表中时,我必须在子表中有一行,但在父表中有一行之前,我不能在子表中有一行。 这是更好地执行客户端。 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 ...
  • 如果城市的密钥是(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 ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)