陣列去重Demo引出的思考

ii_chengzi發表於2019-07-14
package com.pers.Stream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;/**
 * 陣列去重demo
 *
 * @author hoobey */public class RemoveDuplicatedDataTest {    public static void main(String[] args) {
        String[] strArr = new String[]{"abc", "ab", "abc"};        //Arrays.asList(T...a)  接受的是一個泛型的變長引數 而基本型別是無法被泛型化的 可使用基本包裝類
        List<String> strList = Arrays.asList(strArr);//String[] --> List<String>
        for (int i = 0; i < strList.size(); i++) {//因為List是 有序 可重複的   所以並不會去掉重複內容
            System.out.println(strList.get(i)+",");//java.io.PrintStream println()列印物件時會自動呼叫其toString()        }        int[] intArr = {1, 2, 1, 3, 2, 4};
        List list = Arrays.asList(intArr);//List<int[]>不行! --> 原理是List中只能儲存物件!
        for (int i = 0; i < list.size(); i++) {
            Object obj = list.get(i);
            System.out.println("下標i=" + i + ",儲存的內容:" + obj);//[I@14ae5a5  地址
            int[] temp = (int[]) obj;//強轉一下  得到地址中指向的資料
            for (int j = 0; j < temp.length; j++) {
                System.out.print(temp[j]+",");
            }
        }        // 把陣列先變成List,List是繼承Collection的介面,所以就可以把轉換後的list給addAll()
    /*    List intList = new ArrayList();
        for (int i = 0; i < intArr.length; i++) {
            intList.add(intArr[i]);
        }*/
        List<Integer> intList = IntStream.of(intArr).boxed().collect(Collectors.toList());        //要求各位把intArr中重複的數字去掉,僅留下相互不重複的數字   核心就是Set集合儲存的是   無序不重複的資料
        Set dupDataRemoved = new HashSet(intList);//        dupDataRemoved.addAll(intList);//        Collections.addAll(dupDataRemoved, intList);
        System.out.println();
        System.out.print("去重後的資料是:");        for(Iterator it = dupDataRemoved.iterator(); it.hasNext(); ){            
            System.out.print(it.next() + ",");
        }        //=========================================
        /*如果陣列儲存的元素是物件,那麼可以直接用Arrays.aslist()
        但如果是基本型別就需要使用相應的Stream來轉。*/
        
        String[] strArr2 = {"xiaoming", "xiaoyu", "xiaoming", "xiaoming", "xiaoyu"};
        List<String> list1 = Arrays.asList(strArr2);//List<String>
        Set strSet = new HashSet(list1);//構造一個包含指定 collection 中的元素的新 set
        
        for(Iterator it = strSet.iterator(); it.hasNext(); ){            
            System.out.print(it.next() + ",");
        }
    }
}

abc,

ab,

abc,

下標i=0,儲存的內容:[I@14ae5a5

1,2,1,3,2,4,

去重後的資料是:1,2,3,4,=========================================

xiaoyu,xiaoming,

Process finished with exit code 0                                                                      

/*在Stream API中,一個流基本上代表一個元素序列,Stream
API提供了豐富的操作函式來計算這些元素。以前我們在開發業務應用時,通常很多操作的實現是這樣做的:我們使用迴圈對集合做遍歷,針對集合中的元素實現各種操作,定義各種變數來實現目的,這樣我們就得到了一大堆醜陋的順序程式碼。
如果我們使用Stream API做同樣的事情,使用Lambda表示式和其它函式進行抽象,可以使得程式碼更易於理解、更為乾淨。有了這些抽象,還可以做一些最佳化,比如實現並行等。*/
    @Test    public void syso(){
        IntStream rangeStream = IntStream.range(0, 10);
        List<Integer> list = rangeStream.boxed().collect(Collectors.toList());
        Iterator<Integer> it = list.iterator();        while (it.hasNext()){
            System.out.print(it.next()+",");//0,1,2,3,4,5,6,7,8,9,        }
    }                                                                                    

* 把int陣列中的重複資料刪除掉?---HashSet儲存的資料都是物件,不能儲存相同的元素int[] intArr = new int[]{1,2,2,3,1,134,22,1,3,123,563,67,3,134,123,67,4,06,-1,1};//第一步,轉化int陣列為int流IntStream.of(intArr)//在Java中,8種基本資料型別都有對應的包裝類//boolean;char;byte,short,int,long;float,double//要將儲存的int轉換為Integer物件(底層原理:HashSet儲存的資料都是物件,不能儲存相同的元素)IntStream.of(intArr).boxed() --> Stream中儲存Integer物件//Stream --> ListList intList = IntStream.of(intArr).boxed().collect(Collectors.toList());//第二步,3種方式去重://HashSet的構造方法new HashSet(intList);//呼叫HashSet.addAll(Collection c); Collection介面 -繼承-> List介面 --> AbstractList --> ArrayListnew HashSet().addAll(intList);//Collections ArraysHashSet hashset = new HashSet();
Collections.addAll(hashset, intList);//迭代器Iterator,Collection.iterator()方法iterator.next(); */public class RemoveSame {    public static void main(String[] args){        int[] intArr = {1, 2, 3, 43, 2, 4, 1, 5765, 23, 12, 6, 1, 2, 56, 2, 3, 2, 3, 7, 8, 128843, 1};
        List<Integer> intList = IntStream.of(intArr).boxed().collect(Collectors.toList());
        Set dupDataRemoved = new HashSet(intList);        for (Iterator it = dupDataRemoved.iterator(); it.hasNext(); ) {
            System.out.print(it.next() + ",");
        }        /*Arrays.asList([]);
            如果陣列儲存的元素是物件,那麼可以直接用Arrays.aslist()
            但如果是基本型別就需要使用相應的Stream來轉。*/
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31543790/viewspace-2650480/,如需轉載,請註明出處,否則將追究法律責任。

相關文章