List的removeAll方法异常?

shanliangliuxing 2012-03-22 10:22:00
现在正在实现的一个模块,是用Hibernate做的,一个用户对应多个频道,用的是一对多的关联,配置方件如下:
User表:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class lazy="false" name="com.autonavi.monitor.model.User" table="user">
<id name="userId" type="integer" unsaved-value="0">
<column name="userId"/>
<generator class="native"/>
</id>
<property name="name" type="string">
<column name="name" />
</property>
<property name="userName" type="string">
<column name="userName" not-null="true"/>
</property>
<property name="password" type="string">
<column name="password" not-null="true"/>
</property>
<property name="actor" type="int">
<column name="actor"/>
</property>
<property name="authority" type="string">
<column name="authority"/>
</property>
<property name="email" type="string">
<column name="email"/>
</property>
<property name="phone" type="string">
<column name="phone"/>
</property>
<property name="phone1" type="string">
<column name="phone1"/>
</property>
<property name="phone2" type="string">
<column name="phone2"/>
</property>

<set name="channels" cascade="all" lazy="false">
<key column="userId" />
<one-to-many class="com.autonavi.monitor.model.Channel"/>
</set>

</class>
</hibernate-mapping>


频道表:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class lazy="false" name="com.autonavi.monitor.model.Channel" table="channel">
<id name="channelId" type="integer" unsaved-value="0">
<column name="channelId"/>
<generator class="native"/>
</id>
<property name="channel" type="string">
<column name="channel" />
</property>
<property name="channelName" type="string">
<column name="channel_name"/>
</property>
<property name="delayTime" type="integer">
<column name="delay_time" not-null="true"/>
</property>
</class>
</hibernate-mapping>

在Action里面做相应的查询,由于取消了懒加载,在查出的User里面会取到对应的频道列表

List<Channel> userChannelList = new ArrayList<Channel>();
Channel channel = null;
Set<Channel> channelSet = modifyUser.getChannels();
Iterator<Channel> iterator = channelSet.iterator();
while(iterator.hasNext()) {
channel = (Channel)iterator.next();
userChannelList.add(channel);
}


用Hibernate的HQL功能取到所有的频道:

@SuppressWarnings("unchecked")
public List<Channel> getChannelList() {
String sql = "";
sql = "from Channel";
List<Channel> list = this.getHibernateTemplate().find(sql);
return list;
}


现在我要得到剩下的频道,所以想着是用得到的所有的频道List调用removeAll方法去掉当前用户占用的频道List

channelList.removeAll(userChannelList);


本来挺简单的,可执行后发现并没有达到预期的效果,不能从所有的频道中把当前用户占用的频道remove掉,找了很久才发现所有频道List中包含的当前用户占用的频道的hashcode和当前用户占用的频道List中的频道的hashcode值不一样,其他值都相同,猜测可能是因为这所以不能执行remove操作,我想知道为什么会出现这种情况?该怎么实现我上面所说的需求?


...全文
609 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dryZeng 2012-03-22
  • 打赏
  • 举报
回复
channelList只能remove自己的对象,你那个list是别的对象,它怎么删别人?
你只有通过嵌套for,根据判断channelList.get(i).channelId==userChannelList.get(j).channelId
如果相同,则channelList.remove(i),这样一个一个删除。
baby1986 2012-03-22
  • 打赏
  • 举报
回复
有重写Channel类的equals方法吗?重写得正确吗?
shanliangliuxing 2012-03-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 creso 的回复:]
hashcode算法加入了数组的初始长度,也就是说初始长度不同的数组(ArrayList)中相同对象的hashcode有可能不同
[/Quote]
有没有解决的办法?
creso 2012-03-22
  • 打赏
  • 举报
回复
hashcode算法加入了数组的初始长度,也就是说初始长度不同的数组(ArrayList)中相同对象的hashcode有可能不同
shanliangliuxing 2012-03-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dryzeng 的回复:]
channelList只能remove自己的对象,你那个list是别的对象,它怎么删别人?
你只有通过嵌套for,根据判断channelList.get(i).channelId==userChannelList.get(j).channelId
如果相同,则channelList.remove(i),这样一个一个删除。
[/Quote]
嗯,你说的很对,因为hashcode不同,所以是不同的对象,不能执行remove操作,最后我就是按你上面说的那样处理的,用迭代器一个一个判断,channelId相同则从channelList中删掉该对象。
shanliangliuxing 2012-03-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 baby1986 的回复:]
有重写Channel类的equals方法吗?重写得正确吗?
[/Quote]
没有重写,用的默认的equals方法

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧