相关疑难解决方法(0)

为什么我需要覆盖Java中的equals和hashCode方法?

最近我读了这个 Developer Works文档.

该文档是关于定义hashCode()equals()有效和正确的,但我无法弄清楚为什么我们需要覆盖这两种方法.

如何有效地实施这些方法?

java equals hashcode

355
推荐指数
15
解决办法
39万
查看次数

带有Int数组的Java HashMap

我正在使用此代码来检查HashMap中是否存在该数组.

public class Test {
    public static void main(String[] arg) {
        HashMap<int[], String> map = new HashMap<int[], String>();
        map.put(new int[]{1, 2}, "sun");
        System.out.println(map.containsKey((new int[]{1, 2})));
    }
}
Run Code Online (Sandbox Code Playgroud)

但这打印错误.如何检查HashMap中是否存在该数组.提前致谢.

java arrays hashmap

16
推荐指数
2
解决办法
4万
查看次数

TreeSet内部使用TreeMap,因此在使用Treeset时是否需要实现Hashcode方法?

我想知道这是什么意思时的javadocTreeSet

这个类实现了由TreeMap实例支持的Set接口?

在下面的示例中,我还没有实现该Hashcode方法,但仍按预期工作,即它能够对对象进行排序.请注意,我故意没有实现一致的Equals实现来检查TreeSet行为.

import java.util.TreeSet;


public class ComparisonLogic implements Comparable<ComparisonLogic>{

String field1;
String field2;

public String toString(){
    return field1+" "+field2;
}

ComparisonLogic(String field1,String field2){
    this.field1= field1;
    this.field2= field2;

}
public boolean equal(Object arg0){
    ComparisonLogic obj = (ComparisonLogic) arg0; 

    if(this.field1.equals(obj.field1))
        return true;
    else
        return false;
}

public int compareTo(ComparisonLogic arg0){
    ComparisonLogic obj = (ComparisonLogic) arg0;   
    return this.field2.compareToIgnoreCase(obj.field2);
}

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO …
Run Code Online (Sandbox Code Playgroud)

java collections hashcode treemap treeset

8
推荐指数
3
解决办法
2万
查看次数

分组为int数组列表

我有一个int数组列表.我想通过唯一的数组进行分组.

int[] array1 = new int[]{1, 2, 3};
int[] array2 = new int[]{1, 2, 3}; //array1 = array2 
int[] array3 = new int[]{0, 2, 3};

List<int[]> test = new ArrayList<>();

test.add(array1);
test.add(array2);
test.add(array3);

test.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); 
Run Code Online (Sandbox Code Playgroud)

不幸的是,它不起作用.它分组就好像任何数组都是唯一的:

{1, 2, 3} - 1
{1, 2, 3} - 1 
{0, 2, 3} - 1
Run Code Online (Sandbox Code Playgroud)

我预计:

{1, 2, 3} - 2
{0, 2, 3} - 1
Run Code Online (Sandbox Code Playgroud)

我能做什么?

java java-8 java-stream

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

字符串作为HashMap中的键

我曾经看到,只有String被用作HashMap中的一个键.虽然put()方法将Object作为参数.它的重要性如何.如果其他任何对象也可以用作Key?请提供答案.

java hashmap

3
推荐指数
2
解决办法
3万
查看次数

如何在具有选定值的jsp/jstl中进行多选?

您好我有一个用户有一些角色User.class

public class User {

 private Long id;
 private String firstName;
 private String lastName;

 private Set<Role> roles = new HashSet<Role>(0);
Run Code Online (Sandbox Code Playgroud)

public Long getId(){return id; public void setId(Long id){this.id = id; }

 public String getFirstName() { return this.firstName; }
 public void setFirstName(String firstname) { this.firstName = firstname; }

 public String getLastName() { return this.lastName; }
 public void setLastName(String lastname) { this.lastName = lastname; }

 public Set<Role> getRoles() { return this.roles; }
 public void setRoles(Set<Role> roles) { this.roles = roles; }
} …
Run Code Online (Sandbox Code Playgroud)

java jsp jstl el

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

是否存在使用equals方法进行密钥检查的地图?

我想将数据存储在地图中,关键是单一,但我希望地图使用我的密钥类的equals方法.

似乎HashMap不使用equals方法(我可能错了,如果是这样我的测试是错误的).

我的问题是地图使用hashCode来检查重复,我想要一个使用equals的地图实现.

我将时间戳存储在密钥中,并且如果时间戳差异不超过定义的量(假设1000毫秒),则希望使2个密钥等于.

编辑:代码

public class CleanKey
{
    private DateTime start;
    private DateTime end;

    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((end == null) ? 0 : end.hashCode());
        result = prime * result + ((start == null) ? 0 : start.hashCode());
        return result;
    }

    public boolean equals(Object obj)
    {
        if(this == obj)
            return true;
        if(obj == null)
            return false;
        if(getClass() != obj.getClass())
            return false;
        CleanKey other = …
Run Code Online (Sandbox Code Playgroud)

java dictionary hashmap

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

标签 统计

java ×7

hashmap ×3

hashcode ×2

arrays ×1

collections ×1

dictionary ×1

el ×1

equals ×1

java-8 ×1

java-stream ×1

jsp ×1

jstl ×1

treemap ×1

treeset ×1