methods for iterators and filters

This commit is contained in:
Alex Tkachman
2011-12-20 15:56:10 +02:00
parent 117f03cbbf
commit d37c6976b1
3 changed files with 102 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
namespace std.util
import java.util.*
import java.util.Iterator
/*
Add iterated elements to given container
*/
fun <T,U: Collection<in T>> java.util.Iterator<T>.to(container: U) : U {
while(hasNext())
container.add(next())
return container
}
/*
Add iterated elements to java.util.ArrayList
*/
inline fun <T> java.util.Iterator<T>.toArrayList() = to(ArrayList<T>())
/*
Add iterated elements to java.util.LinkedList
*/
inline fun <T> java.util.Iterator<T>.toLinkedList() = to(LinkedList<T>())
/*
Add iterated elements to java.util.HashSet
*/
inline fun <T> java.util.Iterator<T>.toHashSet() = to(HashSet<T>())
/*
Add iterated elements to java.util.LinkedHashSet
*/
inline fun <T> java.util.Iterator<T>.toLinkedHashSet() = to(LinkedHashSet<T>())
/*
Add iterated elements to java.util.TreeSet
*/
inline fun <T> java.util.Iterator<T>.toTreeSet() = to(TreeSet<T>())