Guava小功能收集

2021-09-01 20:26:02 字數 2674 閱讀 2996

自定義過濾條件的集合

listnumbers = lists.newarraylist(1, 2, 3, 6, 10, 34, 57, 89);

predicateaccepteven = new predicate()

};listevennumbers = lists.newarraylist(collections2.filter(numbers, accepteven));

integer found = collections.binarysearch(evennumbers, 57);

assertthat(found, lessthan(0));

從集合中剔除掉空值

listwithnulls = lists.newarraylist("a", "bc", null, "def");

iterablewithoutnuls = iterables.filter(withnulls, predicates.notnull());

asserttrue(iterables.all(withoutnuls, predicates.notnull()));

從集合中檢查某乙個條件

listevennumbers = lists.newarraylist(2, 6, 8, 10, 34, 90);

predicateaccepteven = new predicate()

};asserttrue(iterables.all(evennumbers, accepteven));

集合的反向過濾

listevennumbers = lists.newarraylist(2, 6, 8, 10, 34, 90);

predicateacceptodd = new predicate()

};asserttrue(iterables.all(evennumbers, predicates.not(acceptodd)));

集合元素型別轉換

listnumbers = lists.newarraylist(1, 2, 3);

listasstrings = lists.transform(numbers, functions.tostringfunction());

assertthat(asstrings, contains("1", "2", "3"));

集合的排序

listnumbers = arrays.aslist(2, 1, 11, 100, 8, 14);

orderingordering = ordering.natural().onresultof(functions.tostringfunction());

listinalphabeticalorder = ordering.sortedcopy(numbers);

listcorrectalphabeticalorder = lists.newarraylist(1, 100, 11, 14, 2, 8);

assertthat(correctalphabeticalorder, equalto(inalphabeticalorder));

集合的過濾與功能化

listnumbers = arrays.aslist(2, 1, 11, 100, 8, 14);

predicateacceptevennumber = new predicate()

};functionpoweroftwo = new function() };

fluentiterablepoweroftwoonlyforevennumbers =

fluentiterable.from(numbers).filter(acceptevennumber).transform(poweroftwo);

assertthat(poweroftwoonlyforevennumbers, contains(4, 10000, 64, 196));

集合功能塊的結合

listnumbers = arrays.aslist(2, 3);

functionpoweroftwo = new function()

};listresult = lists.transform(numbers,

functions.compose(poweroftwo, poweroftwo));

assertthat(result, contains(16, 81));

集合通過過濾器建立功能塊

listnumbers = lists.newarraylist(1, 2, 3, 6);

predicateacceptevennumber = new predicate()

};functioniseventnumberfunction = functions.forpredicate(acceptevennumber);

listarenumberseven = lists.transform(numbers, iseventnumberfunction);

assertthat(arenumberseven, contains(false, true, false, true));

iOS常用小功能

這些功能比較實用,而且實現的 也比較簡單 此方式打完 不會返回應用介面,而是停留在打 介面 nsurl url nsurl urlwithstring tel 10086 此方法撥號之前會提示是否撥號,打完 後會回到應用介面,但是因為是私有api,蘋果不建議使用 nsurl url nsurl ur...

JS實用小功能

獲取使用者ip ua 城市 操作cookie function getcookie c name return unescape document.cookie.substring c start,c end return function setcookie c name,value,expire...

JS常用小功能

個人部落格 常用小功能 陣列去重 字串反轉 陣列去扁平化並去重排序 判斷陣列的方法 陣列並集 交集 差集 var arr1 1 2,3 4 var arr2 2 4,6 8 使用includes var union arr1.concat arr2.filter v arr1.includes v ...