Re: [問題]set method
※ 引述《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)
討論串 (同標題文章)