public class Account {
String account;
double balance;
Account(String account, double balance) {
this.account = account;
this.balance = balance;
}
}
public class AccountTest {
public static void main(String[] args) {
Account a1 = new Account("Sandy", 1000);
Account a2 = new Account("Sandy", 1000);
System.out.println(a1.equals(a2));
}
}
Run Code Online (Sandbox Code Playgroud)
当我执行它显示"false"但对象包含变量中的相同数据时.解释.
我用Python编写了几个月,现在我因为工作原因而不得不切换到Java.我的问题是,有一种方法来模拟这种陈述
if var_name in list_name:
# do something
Run Code Online (Sandbox Code Playgroud)
没有定义一个额外isIn()的布尔函数,扫描list_name以便找到var_name?
我正在创建两个对象类的多对多映射.我需要写作hashCode()和equals()方法吗?如果是这样,netbeans会自动生成代码帮助吗?
我有这样的 hashmap
HashMap<Man, Double> d = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
然后我添加新的一对.
d.put(new Man("John"), 5.);
Run Code Online (Sandbox Code Playgroud)
如何从此地图中检索此对?我试着这样做:
Man man = new Man("John");
System.out.println(d.get(man));
Run Code Online (Sandbox Code Playgroud)
但结果是null我想到的结果5.
给定两个对象列表,我将能够根据其中一个属性判断哪些项目不在其交叉点中.我们来看下面的例子:
我有一个Foo有两个属性的类:boo和placeholder
class Foo {
private int boo;
private int placeholder = 1;
public Foo(int boo) {
this.boo = boo;
}
public int getBoo() {
return boo;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我正在创建两个列表(让我们说这是我的输入)
List<Foo> list1 = new ArrayList<Foo>();
list1.add(new Foo(1));
list1.add(new Foo(2));
list1.add(new Foo(3));
List<Foo> list2 = new ArrayList<Foo>();
list2.add(new Foo(0));
list2.add(new Foo(1));
list2.add(new Foo(2));
Run Code Online (Sandbox Code Playgroud)
现在我想根据它们的属性说出哪些项目在list1,不在list2或list2不存在.所以在上面的例子中我想要一个包含一个和一个的例子.list1booList<Foo> notInIntersectListFoo(0)Foo(3)
List<Foo> notInIntersectList = new ArrayList<Foo>();
list1.forEach(li1foo -> {
boolean inBothLists …Run Code Online (Sandbox Code Playgroud) 我正在开发一个允许用户管理帐户的应用程序.所以,假设我有一个Account类,代表用户的一个帐户:
class Account
{
public int id;
public String accountName;
public String accountIdentifier;
public String server;
public String notes;
}
Run Code Online (Sandbox Code Playgroud)
我的equals方法看起来像这样:
public boolean equals(Object o)
{
if (this == o)
return true;
if (o == null || !(o instanceof Account))
return false;
Account other = (Account) o;
if (!accountIdentifier.equals(other.accountIdentifier))
return false;
if (!server.equals(other.server))
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
如你所见,我只是比较accountIdentifier和server其他领域,而不是其他领域.我选择这种方法有几个原因.
List.当用户更新帐户时,通过更改帐户名称(这只是用户指定的名称来识别帐户)或备注,我可以accountList.set(accountList.indexOf(account), account);更新列表中的帐户.如果equals比较所有属性,这种方法将不起作用,我必须解决它(例如通过迭代列表并手动检查这些属性).Account是唯一被标识accountIdentifier和 …我读到要在java中使用equals()方法我们还必须覆盖hashcode()方法,并且相等(逻辑)对象应该具有eual哈希码,但这并不意味着基于引用的相等!这是我的重写equals()方法的代码,我应该如何覆盖hashcode方法:
@Override
public boolean equals(Object o)
{
if (!(o instanceof dummy))
return false;
dummy p = (dummy) o;
return (p.getName() == this.getName() && p.getId() == this.getId() && p.getPassword() == this.getPassword());
}
Run Code Online (Sandbox Code Playgroud)
我只是想了解它是如何工作的,所以只有三个字段,即名称,id和密码,只是试图比较我在main()中定义的两个对象!我还需要知道是否总是需要覆盖hashcode()方法和equals()方法?
假设Person是一个包含属性的类
一个ArrayList持有千人对象,我想检查"11"personId是否在ArayList中?
一种方法是迭代(循环)arraylist并逐个单独检查.
有没有其他方法可以解决这个问题?
我遇到了与联系人相关的问题。我得到了电话联系人并将它们存储在我的列表对象中。这是它的代码
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = {
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.PHOTO_ID,
ContactsContract.Data.DATA1
};
Cursor phones = getContentResolver().query(
uri, projection, ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND " + ContactsContract.Data.DATA1 + "!=''", null, null);
if (phones.moveToFirst()) {
do {
long ID = phones.getLong(phones.getColumnIndex(projection[0]));
String DisplayName = phones.getString(phones.getColumnIndex(projection[1]));
String photoID = phones.getString(phones.getColumnIndex(projection[2]));
String Key = phones.getString(phones.getColumnIndex(projection[3]));
String photoURI = "null";
if(Key != null && Key.toString().trim().length() > 0 && (Key.startsWith("0") || Key.startsWith("+"))){
if (photoID != null) {
photoURI=String.valueOf(ID);;
//Console.WriteLine("*************************************> id="+ID+" uri="+photoURI.ToString()); …Run Code Online (Sandbox Code Playgroud) 我有一个定义节点的类(一个有三个双坐标的点).
public class Node {
private final double x, y, z;
public Node() {
}
public Node(final double x, final double y, final double z) {
this.x = x;
this.y = y;
this.z = z;
}
public void setCoordinates(final double x, final double y, final double z) {
this.x = x;
this.y = y;
this.z = z;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要创建很多节点并给它们整数ID,但我必须避免重复.
创建节点的方法如下所示:
private Node vertex = new Node(0, 0, 0);
private final AtomicInteger nodeCounter = new AtomicInteger();
private final Map<Node, Integer> …Run Code Online (Sandbox Code Playgroud)