陣列之間轉換和List轉換的總結

2021-10-06 20:53:43 字數 1477 閱讀 7943

//將陣列轉為list

integer[

] i =

; arrays.

sort

(i);

//排序

list

list = arrays.

aslist

(i);

//將list轉為陣列

integer[

] j =

newinteger

[list.

size()

];list.

toarray

(j);

//list之間型別裝換,將integer型別轉換成string型別

list

integers = arrays.

aslist

(123

,456

,789);

list

strings = integers.

stream()

.map

(string:

:valueof)

.collect

(collectors.

tolist()

);

//將user型別的list,取出id轉為string型別

list

users =

newarraylist

<

>()

;list

strings = users.

stream()

.map

(users:

:getuserid)

.collect

(collectors.

tolist()

);

//將list裡面的數全都*2

list

integers = arrays.

aslist

(123

,456

,789);

list

mobiles = integers.

stream()

.map

(integer -

> integer *2)

.collect

(collectors.

tolist()

);

map()裡面要兩個引數,第乙個是要操作的物件,第二個是物件所具有的方法

list

users = userdao.

getuser()

;user.

stream()

.foreach

(user -

> user.

setgender

("男"))

;

map並不能給屬性賦值,要統一修改某乙個屬性,需要用foreach

陣列 list之間的轉換

listpaixu new arraylist paixu.add 3 paixu.add 7 paixu.add 4 paixu.add 22 paixu.add 1 paixu.add 5 integer strarray new integer 5 strarray 0 12 strarray...

List轉換陣列 陣列轉換List

陣列轉list package listtoarray import j a.util.arraylist import j a.util.arrays import j a.util.collections import j a.util.list import j a.util.stream.c...

陣列和List 之間相互轉換的方法

總結下 資料和list 之間經行轉換的方法。一 從陣列轉換成list 1.for 迴圈遍歷 遍歷陣列將資料元素放入到list中。例子如下 public static void mian string args arraylist strlist new arraylist for int i 0 i...