/** * Adds the given {@code entry} to the map, making it immutable if necessary. Duplicate keys are * not allowed, and will cause {@link #build} to fail. * * @since 11.0 */ @CanIgnoreReturnValue public Builder<K, V> put(Entry<? extends K, ? extends V> entry) { return put(entry.getKey(), entry.getValue()); }
static <K extends Enum<K>, V> ImmutableMap<K, V> asImmutable(EnumMap<K, V> map) { switch (map.size()) { case 0: return ImmutableMap.of(); case 1: Entry<K, V> entry = Iterables.getOnlyElement(map.entrySet()); return ImmutableMap.of(entry.getKey(), entry.getValue()); default: return new ImmutableEnumMap<>(map); } }
public void testBuilderPutAllEntryListJdkBacked() { List<Entry<String, String>> entryList = Arrays.asList( new AbstractMap.SimpleEntry<>("a", "1"), new AbstractMap.SimpleEntry<>("b", "2")); ImmutableMap<String, String> map = ImmutableMap.<String, String>builder().putAll(entryList).buildJdkBacked(); assertThat(map).containsExactly("a", "1", "b", "2").inOrder(); entryList.get(0).setValue("3"); assertThat(map).containsExactly("a", "1", "b", "2").inOrder(); } }
public void testBiMapEntrySetIteratorRemove() { BiMap<Integer, String> map = HashBiMap.create(); map.put(1, "one"); Set<Entry<Integer, String>> entries = map.entrySet(); Iterator<Entry<Integer, String>> iterator = entries.iterator(); Entry<Integer, String> entry = iterator.next(); entry.setValue("two"); // changes the iterator's current entry value assertEquals("two", map.get(1)); assertEquals(Integer.valueOf(1), map.inverse().get("two")); iterator.remove(); // removes the updated entry assertTrue(map.isEmpty()); }
public void testEntrySetValueAfterRemoved() { CompactHashMap<Integer, String> map = CompactHashMap.create(); map.put(1, "1"); Entry<Integer, String> entry = getOnlyElement(map.entrySet()); map.remove(1); entry.setValue("one"); assertThat(map).containsEntry(1, "one"); } }
public void testCopyOfMutableEntryList() { List<Entry<String, String>> entryList = Arrays.asList( new AbstractMap.SimpleEntry<>("a", "1"), new AbstractMap.SimpleEntry<>("b", "2")); ImmutableMap<String, String> map = ImmutableMap.copyOf(entryList); assertThat(map).containsExactly("a", "1", "b", "2").inOrder(); entryList.get(0).setValue("3"); assertThat(map).containsExactly("a", "1", "b", "2").inOrder(); }
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) @CollectionSize.Require(absent = ZERO) public void testSetValueNullUnsupported() { for (Entry<K, V> entry : getMap().entrySet()) { try { entry.setValue(null); fail("Expected NullPointerException"); } catch (NullPointerException expected) { } expectUnchanged(); } }
@Override Cell<R, C, V> getCell(int index) { int rowIndex = cellRowIndices[index]; Entry<R, ImmutableMap<C, V>> rowEntry = rowMap.entrySet().asList().get(rowIndex); ImmutableMap<C, V> row = rowEntry.getValue(); int columnIndex = cellColumnInRowIndices[index]; Entry<C, V> colEntry = row.entrySet().asList().get(columnIndex); return cellOf(rowEntry.getKey(), colEntry.getKey(), colEntry.getValue()); }
static <T extends StandardTable<Object, Object, Object>> T populate( SerializationStreamReader reader, T table) throws SerializationException { Map<?, ?> hashMap = (Map<?, ?>) reader.readObject(); for (Entry<?, ?> row : hashMap.entrySet()) { table.row(row.getKey()).putAll((Map<?, ?>) row.getValue()); } return table; }