Tag: 对象

具有多个字段的对象的Java Comparator

我有一个包含5个字段的Object Collection : id; entityType; entityId; brandId; productId; 要对Collection的ArrayList进行排序,我编写了以下Comparaor 。 Comparator collectionComparator = new Comparator() { @Override public int compare(Collection collection1, Collection collection2) { if(collection1.getId().equals(collection2.getId())) { if(collection1.getEntityType().equals(collection2.getEntityType())) { if(collection1.getEntityId().equals(collection2.getEntityId())) { if(collection1.getBrandId().equals(collection2.getBrandId())) { return collection1.getProductId().compareTo(collection2.getProductId()); } else { return collection1.getBrandId().compareTo(collection2.getBrandId()); } } else { return collection1.getEntityId().compareTo(collection2.getEntityId()); } } else { return collection1.getEntityType().compareTo(collection2.getEntityType()); } } return collection1.getId().compareTo(collection2.getId()); […]

在java中打印对象时会发生什么

class Data { int a = 5; } class Main { public static void main(String[] args) { int b=5; data dObj = new data(); System.out.println(dObj); System.out.println(b); } } 我想知道在打印对象或数字或字符串时发生了什么。 我运行上面的代码,我得到的结果为System.out.println(dObj); “data @ 1ae73783” System.out.println(dObj); System.out.println(b); “5” System.out.println(b); 然后我做了调试来检查打印对象时真的发生了什么,在调试模式中调用了很多参数(比如classloader,theards) 我知道第一次打印时,值表示类名后跟地址。 但是不知道在调试模式下真正发生了什么,因为在调试模式中发生了第二次打印变量赋值,即b = 5。 请解释一下真的发生了什么?

可以将具有推断类型的局部变量重新分配给其他类型吗?

我记得在某处读过带有推断类型的局部变量可以用相同类型的值重新分配,这是有意义的。 var x = 5; x = 1; // Should compile, no? 但是,我很好奇如果你要将x重新分配给不同类型的对象会发生什么。 这样的东西还会编译吗? var x = 5; x = new Scanner(System.in); // What happens? 我目前无法安装JDK 10的早期版本,并且不想等到明天才发现。

如何使用hibernate工具生成带注释的域对象

我使用Eclipse Hibernate Tools从我的数据库开始创建域类,并需要添加JPA注释。 有没有办法添加注释? 可能有reveng.xml和逆向工程? 该怎么做? 生成的域代码: public class Country implements java.io.Serializable { private long id; private String description; private String identifier; private String futureuse; private Set accounts = new HashSet(0); public Country() { } public Country(long id, String description, String identifier) { this.id = id; this.description = description; this.identifier = identifier; } … 需要的代码: […]

JavaFX 2.0选择框问题。 如何在更新对象时更新表示对象列表的choiceBox?

我有一个表示列表对象的choiceBox。 当代表其中一个对象的名称被另一位代码更改时,选择框的下拉列表中的名称不会更改。 例如,如果我有一个由列表Test对象组成的选择框。 测试代码如下所示: class Test { String name; public Test(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } @Override public String toString() { return name; } } 然后选择Box如下: ChoiceBox chi = new ChoiceBox(); ObservableList items = FXCollections.observableArrayList(); chi.setItems(items); items.addAll(new Test(“ITEM1”),new […]

创建的对象的ID生成器

我需要一个创建对象的类,为每个创建的对象分配一个ID。 此ID通常是类的int属性。 我想每次创建一个Object时增加这个值(ID),然后从1开始分配给该对象。我觉得我需要一个静态int属性。 如何初始化此静态属性? 我应该创建一个单独的方法来增加在构造函数中调用的ID(作为ID生成器)吗? 一般来说,最有效和最精心设计的方式是什么?

为什么Object.clone()在Java中是原生的?

Object上的clone方法创建了一个对象的精确副本,声明为: protected native Object clone() throws CloneNotSupportedException; 为什么它是native ?

按顺序迭代对象队列

我创建了一个包含对象的队列,我希望按照它们放入队列的顺序进行迭代(第一个对象放在队列中,第二个对象放在队列中,第三个对象……) 我在网上看到了一种方法,但是我不确定这是否能保证队列中的对象能够以正确的顺序访问? for(MyObject anObject : queue){ //do someting to anObject… 感谢您的帮助。

在不指定对象类型的情况下创建ArrayList时,在添加第一个对象时是否自动创建它?

例如,而不是做 ArrayList variableName; 你做 ArrayList variableName; 然后你添加一个“ClassName”类型的对象 variableName.add(objectName); 会自动将数组的类型设置为 ArrayList ?

Java中的对象创建缓慢

我的经验表明,Java中的对象创建非常缓慢。 通常,我只是通过删除对象创建并重复使用相同的对象来优化我的代码。 我想知道它在OOP基础上的其他语言是否同样缓慢,因为对我来说,非常直观的是,面向硬核对象的语言需要花费很多时间来创建对象。 有人在几个langauges上描述过这个吗?