When we deal with in business, in many cases data needs to be layers of filtration, recently needed to remove a list of elements does not meet the conditions,
Would have felt quite simple, Google found that many methods are under the old method, did not meet my needs.
So the online reference method to write several methods
1 .Iterator removal
Iterator<AdmVipConfigs> iterator = admVipConfigs.iterator();
while (iterator.hasNext()){
AdmVipConfigs next = iterator.next();
String vipName1 = next.getVipName();
if (vipName1.equals("铂金会员")){
iterator.remove();
}
}
Although it can be removed but transformed into a list of the types of Iterator (also converts do not like)
This is not in line with my code style first pass
It has changed the way some older converting find defects are not small (trouble)
This time can no longer follow the old ideas have to go myself to think of a good way of
This time suddenly remembered flow though with not much but it's like this style
Stream how to deal with this problem then
Needless to say definitely to Google
And then found the flow really can handle and more powerful (good-looking)
2 stream to remove
admVipConfigs.stream () findFirst () the Map.. (VO -> {
IF (vo.getVipName () the equals ( "registered members")) {.
admVipConfigs.remove (VO);
}
return VO;
});
no change me the list format and more concise, looking quite satisfied
but when the stream processing remove time will traverse the entire list again to find
this element, there will be some loss of performance, how it can do it, I'm naked eye can see
loss rubbish Code (unless no time to change)
since we can only add to its index of
3 stream + index removal
IntStream.range (0, admVipConfigs.size ()). Filter (I->
admVipConfigs.get (i) .getVipName (). The equals ( "Gold Membership")).
Boxed (). FindFirst (). The Map (i -> admVipConfigs.remove ((int) i ));
which is boxed boxing operation
seemed simple point, is this the right to be!
// remove a certain element in the list @Test public void bb () { String vipName [] = { "registered members", "Gold Member", "Platinum Membership", "Diamond", "supreme members", " super Member " }; List <AdmVipConfigs> = admVipConfigs new new the ArrayList <> (); for ( int I =. 1; I <. 7; I ++ ) { AdmVipConfigs admVipConfig = new new AdmVipConfigs (); admVipConfig.setVipId (I); admVipConfig.setVipDel ( 0 ); admVipConfig.setVipDefaultValue ( 0 ); admVipConfig.setVipOrderNo(i); if (i == 1) { admVipConfig.setVipStatus(0); } else { admVipConfig.setVipStatus(1); } admVipConfig.setVipCondition(0); admVipConfig.setVipName(vipName[i - 1]); admVipConfigs.add(admVipConfig); } admVipConfigs.stream().findFirst().map(vo -> { if (vo.getVipName().equals("注册会员")){ admVipConfigs.remove(vo); } return vo; }); IntStream.range(0,admVipConfigs.size()).filter(i-> admVipConfigs.get(i).getVipName().equals("金卡会员")).boxed().findFirst().map(i->admVipConfigs.remove((int)i)); Iterator<AdmVipConfigs> iterator = admVipConfigs.iterator(); while (iterator.hasNext()){ AdmVipConfigs next = iterator.next(); String vipName1 = next.getVipName(); if (vipName1.equals("铂金会员")){ iterator.remove(); } } System.out.println(admVipConfigs); }