/** * Stores the contents of a map in an output stream, as part of serialization. It does not support * concurrent maps whose content may change while the method is running. * * <p>The serialized output consists of the number of entries, first key, first value, second key, * second value, and so on. */ static <K, V> void writeMap(Map<K, V> map, ObjectOutputStream stream) throws IOException { stream.writeInt(map.size()); for (Map.Entry<K, V> entry : map.entrySet()) { stream.writeObject(entry.getKey()); stream.writeObject(entry.getValue()); } }
private Map<String, List<LazyHeaderFactory>> copyHeaders() { Map<String, List<LazyHeaderFactory>> result = new HashMap<>(headers.size()); for (Map.Entry<String, List<LazyHeaderFactory>> entry : headers.entrySet()) { @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") List<LazyHeaderFactory> valueCopy = new ArrayList<>(entry.getValue()); result.put(entry.getKey(), valueCopy); } return result; }
private StompHeaders(Map<String, List<String>> headers, boolean readOnly) { Assert.notNull(headers, "'headers' must not be null"); if (readOnly) { Map<String, List<String>> map = new LinkedMultiValueMap<>(headers.size()); headers.forEach((key, value) -> map.put(key, Collections.unmodifiableList(value))); this.headers = Collections.unmodifiableMap(map); } else { this.headers = headers; } }
static <N, V> DirectedGraphConnections<N, V> ofImmutable( Set<N> predecessors, Map<N, V> successorValues) { Map<N, Object> adjacentNodeValues = new HashMap<>(); adjacentNodeValues.putAll(successorValues); for (N predecessor : predecessors) { Object value = adjacentNodeValues.put(predecessor, PRED); if (value != null) { adjacentNodeValues.put(predecessor, new PredAndSucc(value)); } } return new DirectedGraphConnections<>( ImmutableMap.copyOf(adjacentNodeValues), predecessors.size(), successorValues.size()); }
private Map<String, Class<?>> toClassMap(Map<String, ?> map) throws ClassNotFoundException { Map<String, Class<?>> result = new LinkedHashMap<>(map.size()); for (Map.Entry<String, ?> entry : map.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); Class<?> type; if (value instanceof Class) { type = (Class<?>) value; } else if (value instanceof String) { String className = (String) value; type = ClassUtils.forName(className, this.beanClassLoader); } else { throw new IllegalArgumentException("Unknown value [" + value + "] - expected String or Class"); } result.put(key, type); } return result; }
@Test public void testGroupByWithElementSelector() { Observable<String> source = Observable.just("one", "two", "three", "four", "five", "six"); Observable<GroupedObservable<Integer, Integer>> grouped = source.groupBy(length, length); Map<Integer, Collection<Integer>> map = toMap(grouped); assertEquals(3, map.size()); assertArrayEquals(Arrays.asList(3, 3, 3).toArray(), map.get(3).toArray()); assertArrayEquals(Arrays.asList(4, 4).toArray(), map.get(4).toArray()); assertArrayEquals(Arrays.asList(5).toArray(), map.get(5).toArray()); }
/** * Copy constructor which allows for ignoring certain entries. * Used for serialization without non-serializable entries. * @param original the MessageHeaders to copy * @param keysToIgnore the keys of the entries to ignore */ private MessageHeaders(MessageHeaders original, Set<String> keysToIgnore) { this.headers = new HashMap<>(original.headers.size()); original.headers.forEach((key, value) -> { if (!keysToIgnore.contains(key)) { this.headers.put(key, value); } }); }
private static Map<TopicPartition, Integer> createPartitionSizes(Map<TopicPartition, MemoryRecords> partitionRecords) { Map<TopicPartition, Integer> result = new HashMap<>(partitionRecords.size()); for (Map.Entry<TopicPartition, MemoryRecords> entry : partitionRecords.entrySet()) result.put(entry.getKey(), entry.getValue().sizeInBytes()); return result; }
@Test public void testGroupByWithElementSelector2() { Observable<String> source = Observable.just("one", "two", "three", "four", "five", "six"); Observable<GroupedObservable<Integer, Integer>> grouped = source.groupBy(length, length); Map<Integer, Collection<Integer>> map = toMap(grouped); assertEquals(3, map.size()); assertArrayEquals(Arrays.asList(3, 3, 3).toArray(), map.get(3).toArray()); assertArrayEquals(Arrays.asList(4, 4).toArray(), map.get(4).toArray()); assertArrayEquals(Arrays.asList(5).toArray(), map.get(5).toArray()); }
/** * For each element in the managed map, resolve reference if necessary. */ private Map<?, ?> resolveManagedMap(Object argName, Map<?, ?> mm) { Map<Object, Object> resolved = new LinkedHashMap<>(mm.size()); mm.forEach((key, value) -> { Object resolvedKey = resolveValueIfNecessary(argName, key); Object resolvedValue = resolveValueIfNecessary(new KeyedArgName(argName, key), value); resolved.put(resolvedKey, resolvedValue); }); return resolved; }
public WallSqlTableStat getTableStat(String tableName) { if (tableStats == null) { tableStats = new HashMap<String, WallSqlTableStat>(2); } String lowerCaseName = tableName.toLowerCase(); WallSqlTableStat stat = tableStats.get(lowerCaseName); if (stat == null) { if (tableStats.size() > 100) { return null; } stat = new WallSqlTableStat(); tableStats.put(tableName, stat); } return stat; }
private Map<String, Object> unprefixed(Map<String, Object> parsedConfig) { final Map<String, Object> unprefixedParsedConfig = new HashMap<>(parsedConfig.size()); for (Map.Entry<String, Object> e : parsedConfig.entrySet()) { if (e.getKey().startsWith(keyPrefix)) { unprefixedParsedConfig.put(unprefixed(e.getKey()), e.getValue()); } } return unprefixedParsedConfig; }
@Test public void testGroupBy() { Observable<String> source = Observable.just("one", "two", "three", "four", "five", "six"); Observable<GroupedObservable<Integer, String>> grouped = source.groupBy(length); Map<Integer, Collection<String>> map = toMap(grouped); assertEquals(3, map.size()); assertArrayEquals(Arrays.asList("one", "two", "six").toArray(), map.get(3).toArray()); assertArrayEquals(Arrays.asList("four", "five").toArray(), map.get(4).toArray()); assertArrayEquals(Arrays.asList("three").toArray(), map.get(5).toArray()); }