在Java中利用set特性刪除重復(fù)的數(shù)組元素
Set(): 存入Set的每個(gè)元素必須是唯一的,因?yàn)镾et不保存重復(fù)元素。加入Set的Object必須定義equals()方法以確保對象的唯一性。Set與Collection有完
全一樣的接口。Set接口不保證維護(hù)元素的次序。
HashSet: 為快速查找而設(shè)計(jì)的Set。存入HashSet的對象必須定義hashCode()。
TreeSet: 保持次序的Set,底層為樹結(jié)構(gòu)。使用它可以從Set中提取有序的序列。
LinkedHashSet: 具有HashSet的查詢速度,且內(nèi)部使用鏈表維護(hù)元素的順序(插入的次序)。于是在使用迭代器遍歷Set時(shí),結(jié)果會(huì)按元素插入的次序顯示。
HashSet采用散列函數(shù)對元素進(jìn)行排序,這是專門為快速查詢而設(shè)計(jì)的;TreeSet采用紅黑樹的數(shù)據(jù)結(jié)構(gòu)進(jìn)行排序元素;LinkedHashSet內(nèi)部使用散列以加快查
詢速度,同時(shí)使用鏈表維護(hù)元素的次序,使得看起來元素是以插入的順序保存的。需要注意的是,生成自己的類時(shí),Set需要維護(hù)元素的存儲(chǔ)順序,因此要實(shí)現(xiàn)
Comparable接口并定義compareTo()方法。
?
- import ?java.util.LinkedList; ??
- ??
- public ? class ?TestArray01?{ ??
- ? ??
- ? public ? static ? void ?main(String?args[])?{ ??
- ??
- ??Integer[]?a1?=?{? 1 ,? 2 ,? 4 ,? 3 ,? 5 ,? 7 ,? 8 ?}; ??
- ??Integer[]?a2?=?{? 8 ,? 2 ,? 4 ,? 7 ,? 9 ?}; ??
- ??LinkedList<Integer>?list1?=? new ?LinkedList<Integer>(); ??
- ??LinkedList<Integer>?list2?=? new ?LinkedList<Integer>(); ??
- ??
- ?? //?將數(shù)組轉(zhuǎn)換成list ??
- ?? for ?( int ?i?=? 0 ;?i?<?a1.length;?i++)?{ ??
- ???list1.add(a1[i]); ??
- ??} ??
- ?? //?將數(shù)組轉(zhuǎn)換成list ??
- ?? for ?( int ?i?=? 0 ;?i?<?a2.length;?i++)?{ ??
- ???list2.add(a2[i]); ??
- ??} ??
- ??
- ?? int ?size_1?=?list1.size(); ??
- ?? int ?size_2?=?list2.size(); ??
- ?? ??
- ?? //?根據(jù)list中長度最長的設(shè)置list要循環(huán)的長度 ??
- ?? if ?(size_1?>=?size_2)?{ ??
- ??? //?逐個(gè)比較兩個(gè)list中的值是否相同 ??
- ??? for ?( int ?i?=? 0 ;?i?<?list1.size();?i++)?{ ??
- ????Integer?temp?=?list1.get(i); ??
- ???????????????? ??
- ???? //?如果兩個(gè)數(shù)組中有相同的值 ??
- ???? //?則將此值在兩個(gè)list中刪除 ??
- ???? //??注意此處不能使用remove方法 ??
- ???? if ?(list2.contains(temp))?{ ??
- ?????list1.set(i,? null ); ??
- ????? int ?pos?=?list2.indexOf(temp); ??
- ?????list2.set(pos,? null ); ??
- ????} ??
- ???} ??
- ??}? else ?{ ??
- ??? //?逐個(gè)比較兩個(gè)list中的值是否相同 ??
- ??? for ?( int ?i?=? 0 ;?i?<?list2.size();?i++)?{ ??
- ????Integer?temp?=?list1.get(i); ??
- ??
- ???? //?如果兩個(gè)數(shù)組中有相同的值 ??
- ???? //?則將此值在兩個(gè)list中刪除 ??
- ???? //??注意此處不能使用remove方法 ??
- ???? if ?(list1.contains(temp))?{ ??
- ?????list1.remove(temp); ??
- ?????list2.remove(temp); ??
- ????} ??
- ???} ??
- ??} ??
- ??
- ??System.out.println( "???剩余的數(shù)組的信息?list1?:" ); ??
- ?? for ?( int ?i?=? 0 ;?i?<?list1.size();?i++)?{ ??
- ???System.out.println( "-----------------?:??" ?+?list1.get(i)); ??
- ??} ??
- ??
- ??System.out.println( "???剩余的數(shù)組的信息?list2?:" ); ??
- ?? for ?( int ?i?=? 0 ;?i?<?list2.size();?i++)?{ ??
- ???System.out.println( "-----------------?:??" ?+?list2.get(i)); ??
- ??} ??
- ??
- ?? //?將刪除掉重復(fù)元素的兩個(gè)list合并到第三個(gè)list中 ??
- ??LinkedList<Integer>?list3?=? new ?LinkedList<Integer>(); ??
- ?? for ?( int ?i?=? 0 ;?i?<?list1.size();?i++)?{ ??
- ??
- ??? if ?(list1.get(i)?!=? null )?{ ??
- ????list3.addLast(list1.get(i)); ??
- ???} ??
- ??} ??
- ?? for ?( int ?i?=? 0 ;?i?<?list2.size();?i++)?{ ??
- ??? if ?(list2.get(i)?!=? null )?{ ??
- ????list3.addLast(list2.get(i)); ??
- ???} ??
- ??} ??
- ??
- ?? for ?( int ?i?=? 0 ;?i?<?list3.size();?i++)?{ ??
- ???System.out.println( "-------list3----------?:??" ?+?list3.get(i)); ??
- ??} ??
- ?} ??
- }??
import java.util.LinkedList; public class TestArray01 { public static void main(String args[]) { Integer[] a1 = { 1, 2, 4, 3, 5, 7, 8 }; Integer[] a2 = { 8, 2, 4, 7, 9 }; LinkedList<Integer> list1 = new LinkedList<Integer>(); LinkedList<Integer> list2 = new LinkedList<Integer>(); // 將數(shù)組轉(zhuǎn)換成list for (int i = 0; i < a1.length; i++) { list1.add(a1[i]); } // 將數(shù)組轉(zhuǎn)換成list for (int i = 0; i < a2.length; i++) { list2.add(a2[i]); } int size_1 = list1.size(); int size_2 = list2.size(); // 根據(jù)list中長度最長的設(shè)置list要循環(huán)的長度 if (size_1 >= size_2) { // 逐個(gè)比較兩個(gè)list中的值是否相同 for (int i = 0; i < list1.size(); i++) { Integer temp = list1.get(i); // 如果兩個(gè)數(shù)組中有相同的值 // 則將此值在兩個(gè)list中刪除 // 注意此處不能使用remove方法 if (list2.contains(temp)) { list1.set(i, null); int pos = list2.indexOf(temp); list2.set(pos, null); } } } else { // 逐個(gè)比較兩個(gè)list中的值是否相同 for (int i = 0; i < list2.size(); i++) { Integer temp = list1.get(i); // 如果兩個(gè)數(shù)組中有相同的值 // 則將此值在兩個(gè)list中刪除 // 注意此處不能使用remove方法 if (list1.contains(temp)) { list1.remove(temp); list2.remove(temp); } } } System.out.println(" 剩余的數(shù)組的信息 list1 :"); for (int i = 0; i < list1.size(); i++) { System.out.println("----------------- : " + list1.get(i)); } System.out.println(" 剩余的數(shù)組的信息 list2 :"); for (int i = 0; i < list2.size(); i++) { System.out.println("----------------- : " + list2.get(i)); } // 將刪除掉重復(fù)元素的兩個(gè)list合并到第三個(gè)list中 LinkedList<Integer> list3 = new LinkedList<Integer>(); for (int i = 0; i < list1.size(); i++) { if (list1.get(i) != null) { list3.addLast(list1.get(i)); } } for (int i = 0; i < list2.size(); i++) { if (list2.get(i) != null) { list3.addLast(list2.get(i)); } } for (int i = 0; i < list3.size(); i++) { System.out.println("-------list3---------- : " + list3.get(i)); } } }
?
?
- import ?java.util.ArrayList; ??
- import ?java.util.HashMap; ??
- import ?java.util.Iterator; ??
- import ?java.util.List; ??
- import ?java.util.Map; ??
- ??
- public ? class ?TestArray02?{ ??
- ??
- ? public ? static ? void ?main(String?args[])?{ ??
- ??Integer[]?a1?=?{? 1 ,? 2 ,? 3 ?}; ??
- ??Integer[]?a2?=?{? 4 ,? 2 ,? 3 ?}; ??
- ??
- ??Map<Integer,?Object>?map?=? new ?HashMap<Integer,?Object>(); ??
- ??
- ?? for ?( int ?i?=? 0 ;?i?<?a1.length;?i++)?{ ??
- ??? //?因?yàn)槲覀円@取的是set集合,所以 ??
- ??? //?只在此處設(shè)置map的key ??
- ???map.put(a1[i],? null ); ??
- ??} ??
- ??
- ?? for ?( int ?i?=? 0 ;?i?<?a2.length;?i++)?{ ??
- ??? //?因?yàn)槲覀円@取的是set集合,所以 ??
- ??? //?只在此處設(shè)置map的key ??
- ???map.put(a2[i],? null ); ??
- ??} ??
- ??
- ??List<Integer>?list?=? new ?ArrayList<Integer>(); ??
- ??Iterator<Integer>?it?=?map.keySet().iterator(); //?在此處獲取set的集合 ??
- ?? while ?(it.hasNext())?{ ??
- ???Integer?ob?=?(Integer)?it.next(); ??
- ???list.add(ob); ??
- ??} ??
- ??
- ?? //?將list集合轉(zhuǎn)換成Integer數(shù)組 ??
- ??Integer[]?a3?=?(Integer[])?list.toArray( new ?Integer[list.size()]); ??
- ??
- ?? for ?( int ?i?=? 0 ;?i?<?a3.length;?i++)?{ ??
- ???System.out.println(a3[i]); ??
- ??} ??
- ?} ??
- }?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
