相关疑难解决方法(0)

为什么更改用作HashMap中的键的对象的哈希码会使查找返回null?

请考虑以下情形:

Object o1 = new Object();
Object o2 = new Object();

HashMap<Object, Object> map = new HashMap<Object, Object>();
map.put(o1, o2);

boolean test1 = map.get(o1) == o2; // This evaluates to true

// Now lets say we alter the state of o1:
o1.setSomeInternalState(Object newState);

boolean test2 = map.get(o1) == o2; // This evaluates to false, because now map.get(o1) returns null
Run Code Online (Sandbox Code Playgroud)

假设o1的类已被覆盖equals()并且hashCode().

我已经在调试过程中遇到过这个问题,因为我已经明确覆盖equalshashCode一个特定的对象上我用了一些业务逻辑.我完全理解为什么当我改变它的状态时对象的哈希码会改变,但为什么map.get(o1)会因为它而返回null?只有一个对象,所以密钥的哈希码不应该匹配吗?

java equals hashmap hashcode

2
推荐指数
1
解决办法
2148
查看次数

java.util.HashSet不遵守其规范吗?

作为一个相对的Java菜鸟,我很困惑,找出以下内容:

Point.java:

public class Point {
...
    public boolean equals(Point other) {
        return x == other.x && y == other.y;
    }
...
}
Run Code Online (Sandbox Code Playgroud)

Edge.java:

public class Edge {
    public final Point a, b;
    ...
    public boolean equals(Edge other) {
        return a.equals(other.a) && b.equals(other.b);
    }
...
}
Run Code Online (Sandbox Code Playgroud)

main snippet:private Set blockedEdges;

public Program(...) {
    ...
    blockedEdges = new HashSet<Edge>();

    for (int i = 0; ...) {
        for (int j = 0; ...) {

            Point p = new Point(i, j);              
            for (Point …
Run Code Online (Sandbox Code Playgroud)

java hashset

2
推荐指数
1
解决办法
58
查看次数

在 HashSet 中存储坐标

我正在尝试将坐标存储在 HashSet 中并检查我的集合中是否存在坐标。

    HashSet<int[]> hSet = new HashSet<>();
    hSet.add(new int[] {1, 2});
    hSet.add(new int[] {3, 4});
    System.out.println(hSet.contains(new int[] {1, 2}));

>>> false
Run Code Online (Sandbox Code Playgroud)

我对 Java 比较陌生,根据我的理解,上面的输出为 false 是由于比较了 int[] 数组的引用,而不是它们值的逻辑比较。但是,使用 Arrays.equals() 不会使用散列集的散列,因为我必须遍历其所有元素。

我还阅读了其他问题,不建议在集合中使用数组。

因此,如果我希望在 HashSet 中存储坐标对,我应该使用什么数据结构,以便我可以使用哈希码搜索元素?

java hashset coordinates

2
推荐指数
1
解决办法
555
查看次数

如何使我的HashMap按预期工作?

假设一个人有一个简单的类:

public class Point implements Comparable<Point> {

    public int compareTo(Point p) {
        if ((p.x == this.x) && (p.y == this.y)) {
            return 0;
        } else if (((p.x == this.x) && (p.y > this.y)) || p.x > this.x) {
            return 1;
        } else {
            return -1;
        }
    }

    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    } …
Run Code Online (Sandbox Code Playgroud)

java hashmap

1
推荐指数
1
解决办法
113
查看次数

多项式算术java等于方法

我正在研究一个多项式计算器.我的问题是使用equals方法.这是相关代码:

public class Poly{
    Term[] terms;

    //Constructors-------------------------------------------
    public Poly() {}
    public Poly(ArrayList<Term> Terms) {
        terms = Terms.toArray(new Term[Terms.size()]);
        Arrays.sort(terms, new TermComparator());
    }

    //Methods-------------------------------------------------
    public boolean equals(Poly x) {
        boolean q=false;
        if(this == x){
            q=true;
        }
    return q;
    }

    //used in constructor to order terms
    class TermComparator implements Comparator<Term> {
        @Override
        public int compare(Term t1, Term t2) {
            return t2.getExp() - t1.getExp();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

即使两个Poly对象具有相同的值,equals方法也始终返回false.有人可以帮忙吗?

java eclipse

1
推荐指数
1
解决办法
608
查看次数

关于Java集,如何通过其属性删除元素?

我的代码中有一个HashSet,它使用自定义类型Line,其中Line有四个整数字段(x1,y1,x2,y2;所有字段代表行的起点和终点的坐标).我使用大量这些行填充HashSet.然后我想稍后删除特定的行.我试过调用HashSet.remove(新行(正确的属性)),但失败了.关于如何做到这一点的任何想法?

附件是参考代码.该类试图实现Aldous-Broder迷宫生成器,这样最初所有墙都被填充到集合中,然后移除墙(因为该方法雕刻迷宫路径),然后传递给绘图机制.

package tests;

import java.util.HashSet;
import java.util.Random;

public class AldousBroderTest {

static HashSet<Line> walls = new HashSet<Line>();
static Random rn = new Random();

public static void generateWalls(int x1, int y1, int x2, int y2){
    for (int i = x1; i < x2; i += 10){
        for (int j = y1; j < y2; j += 10){
            walls.add(new Line(i, j, i + 10, j));
            walls.add(new Line(i,j,i,j+10));
        }
    }
    walls.add(new Line(x1, y1, x1, y2));
    walls.add(new Line(x1, y1, x2, y1));
    walls.add(new Line(x2, …
Run Code Online (Sandbox Code Playgroud)

java set

1
推荐指数
1
解决办法
64
查看次数

HashMap没有按键找到价值 - 为什么?

我有一节课: Question

它有idcontent.

我有一个HashMapQuestions作为asnwers(Strings)作为价值.

我尝试使用for键从我的地图中查找答案Question.

但是它总是给出null值,我不知道为什么因为map确实包含了我的问题.

码:

            Log.i("info", "map:");
            Log.i("info", questionAnswers.toString());
            Log.i("info", "================================");

            Log.i("info", "question we are looking for:");
            Log.i("info", question.toString());
            Log.i("info", "================================");

            Log.i("info", "anwer is null: " + questionAnswers.get(question));
            Log.i("info", "================================");

            Iterator it = questionAnswers.entrySet().iterator();

            //Iterating the map just to get information for test's sake
            while (it.hasNext()) {

                Map.Entry<Question, String> entry = (Map.Entry<Question, String>) it.next();

                //Question in the map
                Question questionInCont …
Run Code Online (Sandbox Code Playgroud)

java android

1
推荐指数
1
解决办法
105
查看次数

删除ArrayList中的重复对象

所有,

我有一个对象

public class Device {

     public String getUserId() {
        return userId;
    }
    public String getDeviceId() {
        return deviceId;
    }

}
Run Code Online (Sandbox Code Playgroud)

我得到了所有的价值清单

     List<Device> uList = getList();
Run Code Online (Sandbox Code Playgroud)

在列表中,我有一个基于userId的重复值现在我要获取“唯一”列表,该列表将删除userId的重复项

我要如何实现它,我是Java的新手

java arraylist comparator

1
推荐指数
1
解决办法
55
查看次数

在Java中,如何比较两个对象的值?

我想构建和IP地址管理工具.我想在数据库中存储使用过的IP,其中包含使用IP的设备的信息.我希望能够将"已使用"的ips拉出来并将它们与来自给定子网的IP列表进行比较,并确定ip是"已使用"还是"可用".我需要"可用"列表来生成一个列表,我可以选择添加到新设备,而不选择"已使用"的IP.

这是我尝试过的,但它似乎并没有比较IPAddress类型的两个对象.

    ArrayList<IPAddress> used = new ArrayList<>();
    ArrayList<IPAddress> available = new ArrayList<>();
    ArrayList<IPAddress> subnet = new ArrayList<>();

    //I reference my IPAddress class which has four int fields:
    //oct_1, oct_2, oct_3, oct_4  
    //the constructor for IPAddress takes for Ints and I build my IPAddress
    //from the for octets.

    //build a list of all ips in the subnet 10.50.2.0 - 10.50.2.255
    //assuming a class C subnet 255.255.255.0
    for(int i = 0; i < 256; i++){
        subnet.add(new IPAddress(10,50,2,i));
    }

    //identify used ips.  Eventually …
Run Code Online (Sandbox Code Playgroud)

java compare arraylist

0
推荐指数
1
解决办法
1095
查看次数

在JAVA中设置不正确

我只是想从列表中删除重复的元素.为此,我写了一个POJO类Student:

class Student{

private String roll;
private String name;

public Student(String roll, String name) {
    this.roll = roll;
    this.name = name;
}
@Override
public boolean equals(Object obj) {
    Student st = (Student) obj;
    return st.getRoll().equals(roll);
}

public String getRoll() {
    return roll;
}
public void setRoll(String roll) {
    this.roll = roll;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
@Override
public String toString() {
    return roll ;
}

} …
Run Code Online (Sandbox Code Playgroud)

java collections set hashcode hashset

0
推荐指数
1
解决办法
76
查看次数