public static <T> Set<T> mapToSet( PrimitiveIntIterator iterator, IntFunction<T> map ) { Set<T> set = new HashSet<>(); while ( iterator.hasNext() ) { addUnique( set, map.apply( iterator.next() ) ); } return set; }
@Override public PrimitiveIntIterator iterator() { return PrimitiveIntCollections.emptyIterator(); }
/** * Pulls all items from the {@code iterator} and puts them into a {@link Set}, boxing each int. * Any duplicate value will throw {@link IllegalStateException}. * * @param iterator {@link PrimitiveIntIterator} to pull values from. * @return a {@link Set} containing all items. * @throws IllegalStateException for the first encountered duplicate. */ public static Set<Integer> toSet( PrimitiveIntIterator iterator ) { return mapToSet( iterator, Integer::new ); }
@Test void concatenateTwoIterators() { // GIVEN PrimitiveIntIterator firstItems = PrimitiveIntCollections.iterator( 10, 3, 203, 32 ); PrimitiveIntIterator otherItems = PrimitiveIntCollections.iterator( 1, 2, 5 ); // WHEN PrimitiveIntIterator iterator = PrimitiveIntCollections.concat( asList( firstItems, otherItems ).iterator() ); // THEN assertItems( iterator, 10, 3, 203, 32, 1, 2, 5 ); }
@Test void deduplicate() { // GIVEN PrimitiveIntIterator items = PrimitiveIntCollections.iterator( 1, 1, 2, 3, 2 ); // WHEN PrimitiveIntIterator deduped = PrimitiveIntCollections.deduplicate( items ); // THEN assertItems( deduped, 1, 2, 3 ); }
@Test void convertCollectionToLongArray() { PrimitiveIntSet heapSet = PrimitiveIntCollections.asSet( new int[]{1, 2, 3} ); PrimitiveIntSet offHeapIntSet = Primitive.offHeapIntSet( GlobalMemoryTracker.INSTANCE ); offHeapIntSet.add( 7 ); offHeapIntSet.add( 8 ); assertArrayEquals( new long[]{1, 2, 3}, PrimitiveIntCollections.asLongArray( heapSet ) ); assertArrayEquals( new long[]{7, 8}, PrimitiveIntCollections.asLongArray( offHeapIntSet ) ); }
@Test void iteratorAsSet() { // GIVEN PrimitiveIntIterator items = PrimitiveIntCollections.iterator( 1, 2, 3 ); // WHEN PrimitiveIntSet set = PrimitiveIntCollections.asSet( items ); // THEN assertTrue( set.contains( 1 ) ); assertTrue( set.contains( 2 ) ); assertTrue( set.contains( 3 ) ); assertFalse( set.contains( 4 ) ); assertThrows( IllegalStateException.class, () -> PrimitiveIntCollections.asSet( PrimitiveIntCollections.iterator( 1, 2, 1 ) ) ); }
@Test void shouldDeduplicate() { // GIVEN int[] array = new int[]{1, 6, 2, 5, 6, 1, 6}; // WHEN int[] deduped = PrimitiveIntCollections.deduplicate( array ); // THEN assertArrayEquals( new int[]{1, 6, 2, 5}, deduped ); }
@Test void arrayOfItemsAsIterator() { // GIVEN int[] items = new int[]{2, 5, 234}; // WHEN PrimitiveIntIterator iterator = PrimitiveIntCollections.iterator( items ); // THEN assertItems( iterator, items ); }
@Test void copyMap() { PrimitiveIntObjectMap<Object> originalMap = Primitive.intObjectMap(); originalMap.put( 1, "a" ); originalMap.put( 2, "b" ); originalMap.put( 3, "c" ); PrimitiveIntObjectMap<Object> copyMap = PrimitiveIntCollections.copy( originalMap ); assertNotSame( originalMap, copyMap ); assertEquals( 3, copyMap.size() ); assertEquals( "a", copyMap.get( 1 ) ); assertEquals( "b", copyMap.get( 2 ) ); assertEquals( "c", copyMap.get( 3 ) ); }
@Override public PrimitiveIntIterator iterator() { return PrimitiveIntCollections.emptyIterator(); }
/** * Pulls all items from the {@code iterator} and puts them into a {@link Set}, boxing each int. * Any duplicate value will throw {@link IllegalStateException}. * * @param iterator {@link PrimitiveIntIterator} to pull values from. * @return a {@link Set} containing all items. * @throws IllegalStateException for the first encountered duplicate. */ public static Set<Integer> toSet( PrimitiveIntIterator iterator ) { return mapToSet( iterator, Integer::new ); }
public static <T> Set<T> mapToSet( PrimitiveIntIterator iterator, IntFunction<T> map ) { Set<T> set = new HashSet<>(); while ( iterator.hasNext() ) { addUnique( set, map.apply( iterator.next() ) ); } return set; }
@Override public PrimitiveIntIterator iterator() { return PrimitiveIntCollections.emptyIterator(); }
/** * Pulls all items from the {@code iterator} and puts them into a {@link Set}, boxing each int. * Any duplicate value will throw {@link IllegalStateException}. * * @param iterator {@link PrimitiveIntIterator} to pull values from. * @return a {@link Set} containing all items. * @throws IllegalStateException for the first encountered duplicate. */ public static Set<Integer> toSet( PrimitiveIntIterator iterator ) { return mapToSet( iterator, Integer::new ); }
public static <T> Set<T> mapToSet( PrimitiveIntIterator iterator, IntFunction<T> map ) { Set<T> set = new HashSet<>(); while ( iterator.hasNext() ) { addUnique( set, map.apply( iterator.next() ) ); } return set; }