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 ]