我有一个ArrayList<String>,我想从中删除重复的字符串.我怎样才能做到这一点?
我在很多地方读过,虽然equals在Java中覆盖hashCode方法,也应该覆盖方法,否则就是"违反合同".
但到目前为止,如果我只覆盖equals方法,而不是hashCode方法,我没有遇到任何问题.
合同是什么?当我违反合同时,为什么我没有遇到任何问题?如果我没有覆盖hashCode方法,在哪种情况下我会遇到问题?
我有这个测试代码:
import java.util.*;
class MapEQ {
public static void main(String[] args) {
Map<ToDos, String> m = new HashMap<ToDos, String>();
ToDos t1 = new ToDos("Monday");
ToDos t2 = new ToDos("Monday");
ToDos t3 = new ToDos("Tuesday");
m.put(t1, "doLaundry");
m.put(t2, "payBills");
m.put(t3, "cleanAttic");
System.out.println(m.size());
} }
class ToDos{
String day;
ToDos(String d) { day = d; }
public boolean equals(Object o) {
return ((ToDos)o).day == this.day;
}
// public int hashCode() { return 9; }
}
Run Code Online (Sandbox Code Playgroud)
何时// public int hashCode() { return …
class datatype1
{
public static void main(String args[])
{
int i1 = 1;
Integer i2 = 1;
Integer i3 = new Integer(1);
System.out.println("i1 == i2"+(i1==i2));
System.out.println("i1 == i3"+(i1==i3));
System.out.println("i2 == i3"+(i2==i3));
}
}
Run Code Online (Sandbox Code Playgroud)
产量
i1 == i2true
i1 == i3true
i2 == i3false
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么我在比较i2和i3时会出错吗?
我有一个Parent具有20个属性的Java类(attrib1, attrib2 .. attrib20)及其相应的getter和setter.我还有两个Parent对象列表:list1和list2.
现在我想合并两个列表并避免基于attrib1和的重复对象attrib2.
使用Java 8:
List<Parent> result = Stream.concat(list1.stream(), list2.stream())
.distinct()
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但是我必须在哪个地方指定属性?我应该覆盖hashCode和equals方法吗?
假设我有这个客户端JSON输入:
{
id: "5",
types: [
{id: "1", types:[]},
{id: "2", types:[]},
{id: "1", types[]}
]
}
Run Code Online (Sandbox Code Playgroud)
我有这门课:
class Entity {
private String id;
private Set<Entity> types = new LinkedHashSet<>();
public String getId() {
return this.id;
}
public String setId(String id) {
this.id = id;
}
public Set<Entity> getTypes() {
return types;
}
@JsonDeserialize(as=LinkedHashSet.class)
public void setTypes(Set<Entity> types) {
this.types = types;
}
@Override
public boolean equals(Object o){
if (o == null || !(o instanceof Entity)){
return false;
}
return …Run Code Online (Sandbox Code Playgroud) 所以我有一个自定义类Class,它将拥有一组另一个自定义类Student.所以它看起来像这样:
public class Class {
private Set<Student> students;
// other methods
}
Run Code Online (Sandbox Code Playgroud)
现在我将向学生们添加和删除许多学生,我也将改变已经在学生集中的学生的许多私人领域.
问题:我应该使用什么数据结构来实现这一目标?由于我将更改set student中的Student对象的属性(从而更改哈希码),我应该使用ArrayList吗?
这是我的孔类
class Hole {
public int a;
public int b;
Hole(int a, int b) {
this.a = a;
this.b = b;
}
Run Code Online (Sandbox Code Playgroud)
所以我添加了一个包含几个洞的ArrayList
public void checkPathLoop(int x, int y) {
//rough code
ArrayList<Hole> leftFlowInnerHole = new ArrayList<>();
//left holes rules
leftFlowInnerHole.add(new Hole(0, 1));
leftFlowInnerHole.add(new Hole(1, 5));
leftFlowInnerHole.add(new Hole(5, 4));
leftFlowInnerHole.add(new Hole(0, 4));
Run Code Online (Sandbox Code Playgroud)
当我添加
Hole userInputHole = new Hole(0,1);
System.out.print(leftFlowInnerHole.contain(userInputHole));
Run Code Online (Sandbox Code Playgroud)
它总是返回假!! 它假设返回true.
有什么我想念的吗?
先感谢您
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"但对象包含变量中的相同数据时.解释.