
org.eclipse.collections.api.map.MapIterable Maven / Gradle / Ivy
/*
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.api.map;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BiConsumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.Function3;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.multimap.Multimap;
import org.eclipse.collections.api.tuple.Pair;
/**
* A Read-only Map API, with the minor exception inherited from java.lang.Iterable. The method map.iterator().remove()
* will throw an UnsupportedOperationException.
*/
public interface MapIterable extends RichIterable
{
/**
* @see Map#get(Object)
*/
V get(Object key);
/**
* @see Map#containsKey(Object)
*/
boolean containsKey(Object key);
/**
* @see Map#containsValue(Object)
*/
boolean containsValue(Object value);
/**
* Calls the procedure with each value of the map.
*
* Set<String> result = UnifiedSet.newSet();
* MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three", 4, "Four");
* map.forEachValue(new CollectionAddProcedure<String>(result));
* Verify.assertSetsEqual(UnifiedSet.newSetWith("One", "Two", "Three", "Four"), result);
*
*/
void forEachValue(Procedure super V> procedure);
/**
* Executes the Procedure for each value of the map and returns {@code this}.
*
*
* return peopleByCity.tap(person -> LOGGER.info(person.getName()));
*
*
* @see #forEach(Procedure)
* @since 6.0
*/
@Override
MapIterable tap(Procedure super V> procedure);
/**
* Calls the {@code procedure} with each key of the map.
*
* final Collection<Integer> result = new ArrayList<Integer>();
* MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
* map.forEachKey(new CollectionAddProcedure<Integer>(result));
* Verify.assertContainsAll(result, 1, 2, 3);
*
*/
void forEachKey(Procedure super K> procedure);
/**
* Calls the {@code procedure} with each key-value pair of the map.
*
* final Collection<String> collection = new ArrayList<String>();
* MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three");
* map.forEachKeyValue((Integer key, String value) -> collection.add(String.valueOf(key) + value));
* Verify.assertContainsAll(collection, "1One", "2Two", "3Three");
*
*/
void forEachKeyValue(Procedure2 super K, ? super V> procedure);
/**
* Implements the {@code injectInto} pattern with each key-value pair of the map.
*
* MapIterable<Integer, Integer> map1 = Maps.immutable.with(3, 3, 2, 2, 1, 1);
* Integer sum1 = map1.injectIntoKeyValue(0, (sum, key, value) -> sum + key + value);
* assertEquals(12, sum1);
*
*
* @since 11.1
*/
default IV injectIntoKeyValue(
IV injectedValue,
Function3 super IV, ? super K, ? super V, ? extends IV> function)
{
IV[] result = (IV[]) new Object[]{injectedValue};
this.forEachKeyValue((key, value) -> result[0] = function.value(result[0], key, value));
return result[0];
}
/**
* Return the MapIterable that is obtained by flipping the direction of this map and making the associations
* from value to key.
*
* MapIterable<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
* MapIterable<String, Integer> result = map.flipUniqueValues();
* Assert.assertTrue(result.equals(UnifiedMap.newWithKeysValues("1", 1, "2", 2, "3", 3)));
*
*
* @throws IllegalStateException if the MapIterable contains duplicate values.
* @since 5.0
*/
MapIterable flipUniqueValues();
default V getOrDefault(Object key, V defaultValue)
{
return this.getIfAbsentValue((K) key, defaultValue);
}
/**
* Return the value in the Map that corresponds to the specified key, or if there is no value at the key, return the
* result of evaluating the specified Function0.
*/
V getIfAbsent(K key, Function0 extends V> function);
/**
* Return the value in the Map that corresponds to the specified key, or if there is no value at the key, return {@code value}.
*/
V getIfAbsentValue(K key, V value);
/**
* Return the value in the Map that corresponds to the specified key, or if there is no value at the key, return the
* result of evaluating the specified function and parameter.
*/
© 2015 - 2025 Weber Informatics LLC | Privacy Policy