From 55780ad397dca32126819a9195523216b6fb2bec Mon Sep 17 00:00:00 2001 From: REAndroid Date: Thu, 5 Dec 2024 20:43:41 +0100 Subject: [PATCH] Unique iterator --- .../reandroid/utils/collection/CollectionUtil.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/com/reandroid/utils/collection/CollectionUtil.java b/src/main/java/com/reandroid/utils/collection/CollectionUtil.java index fc1f3a44f..e0f53ba21 100644 --- a/src/main/java/com/reandroid/utils/collection/CollectionUtil.java +++ b/src/main/java/com/reandroid/utils/collection/CollectionUtil.java @@ -15,6 +15,8 @@ */ package com.reandroid.utils.collection; +import com.reandroid.utils.ObjectsUtil; + import java.util.*; import java.util.function.Predicate; @@ -199,6 +201,16 @@ public static Iterator copyOf(Iterator iterator){ List results = toList(iterator); return results.iterator(); } + public static Iterator uniqueOf(Iterator iterator){ + boolean hasNext = iterator.hasNext(); + if(!hasNext){ + return EmptyIterator.of(); + } + return new UniqueIterator(ObjectsUtil.cast(iterator)); + } + public static Iterator copyOfUniqueOf(Iterator iterator){ + return copyOf(uniqueOf(iterator)); + } public static Iterator reversedOf(Iterator iterator){ boolean hasNext = iterator.hasNext(); if (!hasNext) {