作者TonyQ (沉默是金)
看板java
标题Re: [问题]set method
时间Mon Jul 7 18:05:09 2008
※ 引述《hsnucsc (hsnugo)》之铭言:
: 请问java有什麽class可以做到像
: { 1 , 2 , 3} 交集 { 2 ,4} => {1,2,3,4}
: { 1 , 2 , 3} - { 2 ,4} => {1,3}
: 我看collection里的set hashset treeset
: 好像都没有这样的method
: 还是有什麽好方法可以做到呢
: 感谢
你可以用HashSet做到
假设 HashSet A,B ,
假设 A ={1,2,3} B={2,4}
你可以写个HashSetUtil
加法的部分
HashMap c= new HashMap(A); //一定要用复制,不然A会被影响.
c.addAll(b); //得到 a+b的结果
减法
HashMap c= new HashMap(A); //一定要用复制,不然A会被影响.
c.removeAll(b); //得到 a-b的结果
根据Set add的时候会过滤相同值的特色,来达到目标>
--
I am a person, and I am always thinking .
Thinking in love , Thinking in life ,
Thinking in why , Thinking in worth.
I can't believe any of what ,
I am just thinking then thinking ,
but worst of all , most of mine is thinking not actioning...
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.128.219.202
※ 编辑: TonyQ 来自: 220.128.219.202 (07/07 18:05)