1. 程式人生 > >我就吃瓜(Guava): 集合1-> Collections2,Sets,Table

我就吃瓜(Guava): 集合1-> Collections2,Sets,Table

Collections2: 集合的使用,包括元素轉換,元素過濾

  • 轉換一般使用Function 來實現,重寫apply()方法。

@Test
public void testGuava3() {
    Set<Long> timeSet = Sets.newHashSet();
    //型別轉換
    timeSet.add(19990701L);
    timeSet.add(20080808L);
    timeSet.add(20161212L);
    Collection<String> timeStrCol = Collections2.transform(timeSet, new Function<Long, String>() 	{
        @Override
        public String apply(Long input) {
            return new SimpleDateFormat("yyyy-MM-dd").format(input);
        }
    });
    //[1970-01-01, 1970-01-01, 1970-01-01]
    System.out.println(timeStrCol);
}
  • 過濾一般使用Predicate來實現,重寫apply()方法。
@Test
public void testGuava2() {
    //建立List 靜態初始化
    List<String> list = Lists.newArrayList("moon", "son", "dad", "refer");
    //找出迴文字串
    Collection<String> palindromeList = Collections2.filter(list, new Predicate<String>() {
        @Override
        public boolean apply(String input) {
            //業務邏輯
            return new StringBuilder(input).reverse().toString().equals(input);
        }
    });
    //[dad, refer]
    System.out.println(palindromeList);
    Collection<String> strings = Collections2.filter(list, new Predicate<String>() {
        @Override
        public boolean apply(String input) {
            return input.contains("m");
        }
    });
    //[moon]
    System.out.println(strings);
}

Table: 雙鍵map,由rowKey和columnKey共同確定value

  • Table的每一個元素都是一個Table.Cell,有rowKey,columnKey,value

@Test
public void testGuava5() {
    Table<String, String, Integer> table = HashBasedTable.create();
    //測試資料
    table.put("龍傲天", "java", 50);
    table.put("龍傲天", "oracle", 60);
    table.put("福爾康", "java", 70);
    table.put("福爾康", "oracle", 100);
    table.put("張凌一", "java", 95);
    //所有的行資料
    Set<Table.Cell<String, String, Integer>> cells = table.cellSet();
    for (Table.Cell<String, String, Integer> temp : cells) {
        System.out.println(temp.getRowKey() + ":" + temp.getColumnKey() + ":" + 		    		temp.getValue());
    }
    System.out.println("=======學生成績========");
    System.out.print("學\t生\t");
    //所有的課程
    Set<String> cols = table.columnKeySet();
    for (String t : cols) {
        System.out.print(t + "\t");
    }
    System.out.println();
    //所有的學生
    Set<String> rows = table.rowKeySet();
    for (String stu : rows) {
        System.out.print(stu + "\t");
        Map<String, Integer> scores = table.row(stu);
        for (String c : cols) {
            System.out.print(scores.get(c) + "\t\t");
        }
        System.out.println();
    }
}

Sets : 建立Set,快速取到Set之間的交集,並集,差集

public class SetsDemo {
    @Test
    public void test1() {
        //建立TreeSet
        TreeSet<SubscribeReq> set = getSubSets();
        //賦值
        for (int i = 0; i < 5; i++) {
            SubscribeReq subscribeReq = new SubscribeReq();
            subscribeReq.setSubReqID(i);
            subscribeReq.setPhoneNumber(i + "");
            set.add(subscribeReq);
        }
        TreeSet<SubscribeReq> set1 = getSubSets();

        for (int i = 3; i < 8; i++) {
            SubscribeReq subscribeReq = new SubscribeReq();
            subscribeReq.setSubReqID(i);
            subscribeReq.setPhoneNumber(i + "");
            set1.add(subscribeReq);
        }
        //取兩個集合的並集
        Sets.SetView<SubscribeReq> union = Sets.union(set, set1);
        System.out.println(union.toString());
        //取set相對於set1的差集
        Sets.SetView<SubscribeReq> difference = Sets.difference(set, set1);
        System.out.println(difference.toString());
        //取兩個集合的交集
        Sets.SetView<SubscribeReq> intersection = Sets.intersection(set, set1);
        System.out.println(intersection.toString());
    }

    TreeSet<SubscribeReq> getSubSets() {
        return Sets.newTreeSet(new Comparator<SubscribeReq>() {
            @Override
            public int compare(SubscribeReq o1, SubscribeReq o2) {
                return o1.getSubReqID() - o2.getSubReqID();
            }
        });
    }

    @Test
    public void Test2() {
        TreeSet<SubscribeReq> set = getSubSets();
        for (int i = 0; i < 5; i++) {
            SubscribeReq subscribeReq = new SubscribeReq();
            subscribeReq.setSubReqID(i);
            subscribeReq.setPhoneNumber(i + "");
            set.add(subscribeReq);
        }
        //取這個集合的冪
        Set<Set<SubscribeReq>> sets = Sets.powerSet(set);
        System.out.println(sets.toString());
    }
	//pojo
    private class SubscribeReq {
        /**
         * 預設的序列號ID
         */
        private static final long serialVersionUID = 1L;

        private int subReqID;

        private String userName;

        private String productName;

        private String phoneNumber;

        private String address;
        /**
         * @return the subReqID
         */
        public final int getSubReqID() {
            return subReqID;
        }
        /**
         * @param subReqID
         */
        public final void setSubReqID(int subReqID) {
            this.subReqID = subReqID;
        }
        /**
         * @return the userName
         */
        public final String getUserName() {
            return userName;
        }
        /**
         * @param userName
         */
        public final void setUserName(String userName) {
            this.userName = userName;
        }
        /**
         * @return the productName
         */
        public final String getProductName() {
            return productName;
        }
        /**
         * @param productName
         */
        public final void setProductName(String productName) {
            this.productName = productName;
        }
        /**
         * @return the phoneNumber
         */
        public final String getPhoneNumber() {
            return phoneNumber;
        }
        /**
         * @param phoneNumber
         */
        public final void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
        }
        /**
         * @return the address
         */
        public final String getAddress() {
            return address;
        }
        /**
         * @param address
         */
        public final void setAddress(String address) {
            this.address = address;
        }
        /*
         * toString()
         */
        @Override
        public String toString() {
            return "SubscribeReq [subReqID=" + subReqID + ", userName=" + userName
                    + ", productName=" + productName + ", phoneNumber="
                    + phoneNumber + ", address=" + address + "]";
        }
    }
}