首页 > 编程知识 正文

java remove方法,list的remove方法返回值

时间:2023-05-06 21:11:22 阅读:128466 作者:53

HashMap cityMap=new HashMap (;

.

for (ArrayList list : city map.values () ) )

int size=list.size (;

if(size0) {

list.removeif(I-true );

}

}

id on ' tquiteunderstandwhattheremoveifdoesinthiscase.especiallythepart (I-true ).Thank you for any explanation。

解决方案

The Javadoc of removeIf () states:

removesalloftheelementsofthiscollectionthatsatisfythegivenpredicate。

thepredicateinyourexampleisalwaystruebecauseyoumapeachintegeriinyourlisttotruebytheexpression : I-true。

iaddedasimplerexamplewhichremovesallevenintegersandkeepsalloddintegersbythepredicatei %2==03:

Ugly setup:

List lists=new ArrayList () {}

add (new ArrayList (arrays.as list (1,2,3,4 ) )

add (new ArrayList (arrays.as list (2,4,6,8 ) )

add (new ArrayList (arrays.as list (1,3,5,7 ) )

};

Keep only odd numbers:

列表列表3360列表(for ) {

list.removeif(I-I%2==0);

}

Output:

[ 1,3 ]

[]

[ 1,3,5,7 ]

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。