list操作小技巧

2021-10-06 16:59:19 字數 4489 閱讀 6345

逗號分隔的字串轉換為list

list轉換為逗號分隔的字串

list物件排序

list物件去重

list物件分組,組內求和

lis轉map

list刪除

1. 逗號分隔的字串轉換為list

感謝大佬的分享字串以逗號分隔的轉換為list方法

string str =

"a,b,c"

;list

split= arrays.

aslist

(str.

split

(","))

;

string str =

"a, b, c"

;list

splittolist= splitter.on(

",")

.trimresults()

.splittolist

(str)

;

string str =

"a,b,c"

;list

aslist= arrays.

aslist

(stringutils.

split

(str,

",")

);

string str =

"a,b,c"

;list

str =arrays.

aslist

(stringutils.

commadelimitedlisttostringarray

(str)

);

string str =

"a,b,c"

;list

str = stream.

of(str.

split

(","))

.collect

(tolist()

);

2. list轉換為逗號分隔的字串

list

list =

newarraylist()

;list.

add(

"a")

;list.

add(

"b")

; list.

add(

"c")

;string str = joiner.on(

",")

.join

(list)

;

list

list =

newarraylist()

;list.

add(

"a")

;list.

add(

"b")

;list.

add(

"c")

;string str = stringutils.

join

(list.

toarray()

,","

);

list

list =

newarraylist()

;list.

add(

"a")

;list.

add(

"b")

;list.

add(

"c")

;string str = stringutils.

collectiontodelimitedstring

(list,

",")

;

3. list物件排序

collections.

sort

(list,

newcomparator

()})

;

list.

sort

(comparator.

comparing

(countcarddetailvo:

:屬性)

.reversed()

);list.

sort

(comparator.

comparing

(countcarddetailvo:

:屬性)

);

4. list物件去重

set《物件》 personset =

newtreeset

<

>

((o1, o2)

-> o1.

getid()

.compareto

(o2.

getid()

));

list《物件》 unique = list.

stream()

.collect

(collectingandthen

(tocollection((

)->

newtreeset

<

>

(comparinglong

(物件:

:getid)))

, arraylist:

:new))

;

public

static

predicate

distinctbykey

(function<

?super t, object> keyextractor)

list.

stream()

.filter

(distinctbykey

(p -

> p.

getid()

)).collect

(tolist()

);

5. list物件分組,組內求和

map

> collect = list.

stream()

.collect

(collectors.

groupingby

(persondata:

:gettype)

);

map

collect2 = list.

stream()

.collect

(collectors.

groupingby

(persondata:

:gettype,collectors.

summingint

(persondata:

:getage)))

;

6. lis轉map

map

maps = userlist.

stream()

.collect

(collectors.

tomap

(user:

:getid,function.

identity()

));

map

maps = userlist.

stream()

.collect

(collectors.

tomap

(user:

:getid, function.

identity()

,(key1, key2)

-> key2)

);

map

maps = userlist.

stream()

.collect

(collectors.

tomap

(user:

:getid, user:

:getage,

(key1, key2)

-> key2)

);

map

maps = maps.

uniqueindex

(userlist,

newfunction

()})

;

7. lis刪除

iterator

it=list.

iterator()

;while

(it.

hasnext()

)}

list.

removeif

(user -

> user.屬性.

contains(""

));

Python小技巧 關於list

今天記錄一下有關 list 的各種神奇操作,array 和它很像,有些操作是一樣的,部分內容參看了知乎內容。目錄 我們可能會遇到這樣的問題,我想讓這幾個list中的數字 0 0,1 0,1,2 0,1,2,3 合併在乙個 list 裡面,如何實現?比較常用的是迴圈來做,其實還有更簡單高效的做法 a ...

實現List分頁,List常用小操作

using system using system.collections.generic using system.linq using system.web public bool hasnextpage 構造方法 public pagingutil listdatalist,int pages...

linux操作小技巧

總結一下平時能用得上的小技巧 比如以下 ls test reactnative.js jsdnsjdajkdnsakj.php這種情況下,想要開啟test目錄下的檔案是十分麻煩的,還要輸那麼多字元,很耽誤效率。解決方案 只需要輸入 vim rea tab 鍵,這樣就自動補全下面的檔名稱了,很方便。t...