TreeSet加了判断还是有重复元素 TreeSet排序 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主重写hashCode写错了,首字母是小写的,注意JAVA是区分大小写的啊 HashCode写错了吧,hashCode才对吧? public int HashCode(){ //这个是你的代码,这里写错了。注意大小写!下面是我写的代码,可以正确运行。package com.wanmei.test;import java.util.HashSet;import java.util.Iterator;import java.util.Set;class T2 { private int id; private String name; public T2(int id, String name) { super(); this.id = id; this.name = name; } @Override public int hashCode() { final int prime = 31; int result = 17; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; T2 other = (T2) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public String toString() { return this.id + "-----------" + this.name; }}public class T1 { public static void main(String[] args) { Set<T2> s = new HashSet<T2>(); s.add(new T2(1, "A")); s.add(new T2(5, "c")); s.add(new T2(3, "d")); s.add(new T2(4, "e")); s.add(new T2(4, "e")); Iterator<T2> it = s.iterator(); while (it.hasNext()) { System.out.println(it.next()); } }} public int HashCode(){int i=this.id+name.hashCode();return i;}hashCode()你把h大写了,改一下就可以了 java怎样将gbk编码的文字转为utf-8编码的,转为utf-8的目的是为了在繁体系统下正常显示 Swing教学视频 如何将鼠标转换为橡皮形状 我在spring 源码里看到这样一段代码 问,接口可以实例化么(解决理解结帖) 帮忙写一个简单的正则表达式,急用 jmx Essential 例子问题 JTable的编辑器和渲染器问题,请高手过来看看 jcreator 不能生成class文件 高分求助,那位有用java多线程编的并行计算的程序(至少300行代码,有注释)。 大家来探讨! 【Java中IO流】OutputStreamWriter的用法 JDBC抛出的异常信息乱码
下面是我写的代码,可以正确运行。package com.wanmei.test;import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;class T2 {
private int id;
private String name; public T2(int id, String name) {
super();
this.id = id;
this.name = name;
} @Override
public int hashCode() {
final int prime = 31;
int result = 17;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
T2 other = (T2) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
} public String toString() {
return this.id + "-----------" + this.name;
}
}public class T1 { public static void main(String[] args) {
Set<T2> s = new HashSet<T2>(); s.add(new T2(1, "A"));
s.add(new T2(5, "c"));
s.add(new T2(3, "d"));
s.add(new T2(4, "e"));
s.add(new T2(4, "e"));
Iterator<T2> it = s.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}}
int i=this.id+name.hashCode();
return i;
}hashCode()你把h大写了,改一下就可以了