Java 8 數(shù)據(jù)過濾,removeIf 和 filter 別用錯了?。?/h1>
filter是Java8 Stream的方法:
Stream?filter(Predicate?super?T>?predicate)
removeIf是Java8 Collecttion的一個默認方法。
default?boolean?removeIf(Predicate?super?E>?filter)
removeIf和filter方法都能達到過濾/刪除元素的作用。
從功能是實現(xiàn)上,removeIf是條件為true則過濾此元素,false則保留。而filter則是條件為false過濾此元素,而true則保留。
從時間上,要看場景,因為removeIf返回的是boolean,而filter是個intermediate operation,需要terminal operation最終來處理流,如果只是單單只做刪除/過濾操作, removeIf會快些。
public?static?void?main(String[]?args)?{
????????List?list?=?new?ArrayList(Arrays.asList(1,2,3,4,5));
????????long?last?=?System.currentTimeMillis();
????????list.removeIf(a?->?a.equals(2));
????????System.out.println(System.currentTimeMillis()?-?last);//37~38
????}
public?static?void?main(String[]?args)?{
????????List?list?=?new?ArrayList(Arrays.asList(1,2,3,4,5));
????????long?last?=?System.currentTimeMillis();
????????list.stream().filter(a?->?!a.equals(2)).collect(Collectors.toList());
????????System.out.println(System.currentTimeMillis()?-?last);//41~44
????}
removeIf是Collection接口的默認方法(Java8新增),底層實現(xiàn)是通過獲得迭代器迭代每一個元素,滿足條件的通過remove()方法刪除,直到迭代完返回true,迭代完都沒有滿足條件的元素則返回false。如果是多個中間流參與,那么還是推薦使用filter方便。
default?boolean?removeIf(Predicate?super?E>?filter)?{
??//判斷是否為null
????????Objects.requireNonNull(filter);
????????boolean?removed?=?false;
????????final?Iterator?each?=?iterator();
????????while?(each.hasNext())?{
?????????//迭代出現(xiàn)運行時異?;蛘咤e誤由由Predicate被轉發(fā)給調用者
????????????if?(filter.test(each.next()))?{
?????????????//remove底層調用的是System.arraycopy方法,是個C++編寫的native方法,操作的是指針,所有比較快
????????????????each.remove();
????????????????removed?=?true;
????????????}
????????}
????????return?removed;
????}
每次filter都產(chǎn)生一個新的StatelessOp,也就新的流,通過opWrapSink不斷匹配條件,當為false時則標記此流元素可過濾
?@Override
????public?final?Stream?filter(Predicate?super?P_OUT>?predicate)?{
?????//判斷是否為null
????????Objects.requireNonNull(predicate);
????????//將無狀態(tài)的中間操作附加到現(xiàn)有流中來構造新流。StreamOpFlag.NOT_SIZED表示要清除的位值
????????return?new?StatelessOp(this,?StreamShape.REFERENCE,
?????????????????????????????????????StreamOpFlag.NOT_SIZED)?{
????????????@Override
????????????Sink?opWrapSink(int?flags,?Sink?sink)?{
????????????????return?new?Sink.ChainedReference(sink)?{
????????????????????@Override
????????????????????public?void?begin(long?size)?{
????????????????????????downstream.begin(-1);
????????????????????}
????????????????????@Override
????????????????????public?void?accept(P_OUT?u)?{
????????????????????????if?(predicate.test(u))
????????????????????????????downstream.accept(u);
????????????????????}
????????????????};
????????????}
????????};
????}
//要清除的位值
static?final?int?NOT_SIZED?=?SIZED.clear;
SIZED(3,
??????????set(Type.SPLITERATOR).set(Type.STREAM).clear(Type.OP)),
來源:blog.csdn.net/Butterfly_resting/article/details/100045181

怎么接私活?這個渠道你100%有用!請收藏!
喜歡文章,點個在看?
瀏覽
30
filter是Java8 Stream的方法:
Stream?filter(Predicate?super?T>?predicate)
removeIf是Java8 Collecttion的一個默認方法。
default?boolean?removeIf(Predicate?super?E>?filter)
removeIf和filter方法都能達到過濾/刪除元素的作用。
從功能是實現(xiàn)上,removeIf是條件為true則過濾此元素,false則保留。而filter則是條件為false過濾此元素,而true則保留。
從時間上,要看場景,因為removeIf返回的是boolean,而filter是個intermediate operation,需要terminal operation最終來處理流,如果只是單單只做刪除/過濾操作, removeIf會快些。
public?static?void?main(String[]?args)?{
????????List?list?=?new?ArrayList(Arrays.asList(1,2,3,4,5));
????????long?last?=?System.currentTimeMillis();
????????list.removeIf(a?->?a.equals(2));
????????System.out.println(System.currentTimeMillis()?-?last);//37~38
????}
public?static?void?main(String[]?args)?{
????????List?list?=?new?ArrayList(Arrays.asList(1,2,3,4,5));
????????long?last?=?System.currentTimeMillis();
????????list.stream().filter(a?->?!a.equals(2)).collect(Collectors.toList());
????????System.out.println(System.currentTimeMillis()?-?last);//41~44
????}
removeIf是Collection接口的默認方法(Java8新增),底層實現(xiàn)是通過獲得迭代器迭代每一個元素,滿足條件的通過remove()方法刪除,直到迭代完返回true,迭代完都沒有滿足條件的元素則返回false。如果是多個中間流參與,那么還是推薦使用filter方便。
default?boolean?removeIf(Predicate?super?E>?filter)?{
??//判斷是否為null
????????Objects.requireNonNull(filter);
????????boolean?removed?=?false;
????????final?Iterator?each?=?iterator();
????????while?(each.hasNext())?{
?????????//迭代出現(xiàn)運行時異?;蛘咤e誤由由Predicate被轉發(fā)給調用者
????????????if?(filter.test(each.next()))?{
?????????????//remove底層調用的是System.arraycopy方法,是個C++編寫的native方法,操作的是指針,所有比較快
????????????????each.remove();
????????????????removed?=?true;
????????????}
????????}
????????return?removed;
????}
每次filter都產(chǎn)生一個新的StatelessOp,也就新的流,通過opWrapSink不斷匹配條件,當為false時則標記此流元素可過濾
?@Override
????public?final?Stream?filter(Predicate?super?P_OUT>?predicate)?{
?????//判斷是否為null
????????Objects.requireNonNull(predicate);
????????//將無狀態(tài)的中間操作附加到現(xiàn)有流中來構造新流。StreamOpFlag.NOT_SIZED表示要清除的位值
????????return?new?StatelessOp(this,?StreamShape.REFERENCE,
?????????????????????????????????????StreamOpFlag.NOT_SIZED)?{
????????????@Override
????????????Sink?opWrapSink(int?flags,?Sink?sink)?{
????????????????return?new?Sink.ChainedReference(sink)?{
????????????????????@Override
????????????????????public?void?begin(long?size)?{
????????????????????????downstream.begin(-1);
????????????????????}
????????????????????@Override
????????????????????public?void?accept(P_OUT?u)?{
????????????????????????if?(predicate.test(u))
????????????????????????????downstream.accept(u);
????????????????????}
????????????????};
????????????}
????????};
????}
//要清除的位值
static?final?int?NOT_SIZED?=?SIZED.clear;
SIZED(3,
??????????set(Type.SPLITERATOR).set(Type.STREAM).clear(Type.OP)),
來源:blog.csdn.net/Butterfly_resting/article/details/100045181
怎么接私活?這個渠道你100%有用!請收藏!
喜歡文章,點個在看?
評論
圖片
表情
