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 + "]";
}
}
}