Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (Integer key : map.keySet()) { Integer value = map.get(key); System.out.println("Key = " + key + ", Value = " + value); }
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); // Iterating over keys only for (Integer key : map.keySet()) { System.out.println("Key = " + key); } // Iterating over values only for (Integer value : map.values()) { System.out.println("Value = " + value); }
@SuppressWarnings("unchecked") public void addAfterComment(String comment) { if (attributes == null) { attributes = new HashMap<String, Object>(1); } List<String> comments = (List<String>) attributes.get("format.after_comment"); if (comments == null) { comments = new ArrayList<String>(2); attributes.put("format.after_comment", comments); } comments.add(comment); }
Map<String, String> map = new HashMap<String, String>(); map.put("dog", "type of animal"); System.out.println(map.get("dog"));
private List<Invoker<T>> toMergeInvokerList(List<Invoker<T>> invokers) { List<Invoker<T>> mergedInvokers = new ArrayList<>(); Map<String, List<Invoker<T>>> groupMap = new HashMap<String, List<Invoker<T>>>(); for (Invoker<T> invoker : invokers) { String group = invoker.getUrl().getParameter(Constants.GROUP_KEY, ""); groupMap.computeIfAbsent(group, k -> new ArrayList<>()); groupMap.get(group).add(invoker); } if (groupMap.size() == 1) { mergedInvokers.addAll(groupMap.values().iterator().next()); } else if (groupMap.size() > 1) { for (List<Invoker<T>> groupList : groupMap.values()) { StaticDirectory<T> staticDirectory = new StaticDirectory<>(groupList); staticDirectory.buildRouterChain(); mergedInvokers.add(cluster.join(staticDirectory)); } } else { mergedInvokers = invokers; } return mergedInvokers; }
public Map<CaseInsensitiveString, List<CaseInsensitiveString>> templatesWithPipelinesForUser(CaseInsensitiveString username) { HashMap<CaseInsensitiveString, List<CaseInsensitiveString>> templatesToPipelinesMap = new HashMap<>(); Map<CaseInsensitiveString, Map<CaseInsensitiveString, Authorization>> authMap = goConfigService.getCurrentConfig().templatesWithAssociatedPipelines(); for (CaseInsensitiveString templateName : authMap.keySet()) { if (securityService.isAuthorizedToViewTemplate(templateName, new Username(username))) { templatesToPipelinesMap.put(templateName, new ArrayList<>()); Map<CaseInsensitiveString, Authorization> authorizationMap = authMap.get(templateName); for (CaseInsensitiveString pipelineName : authorizationMap.keySet()) { templatesToPipelinesMap.get(templateName).add(pipelineName); } } } return templatesToPipelinesMap; }
void putSanitizeName(RType rType, String sanitizeName, String rawName) { HashMap<String, String> sanitizeNameMap; if (!sanitizeTypeMap.containsKey(rType)) { sanitizeNameMap = new HashMap<>(); sanitizeTypeMap.put(rType, sanitizeNameMap); } else { sanitizeNameMap = sanitizeTypeMap.get(rType); } if (!sanitizeNameMap.containsKey(sanitizeName)) { sanitizeNameMap.put(sanitizeName, rawName); } }
@Test public void testIsEmpty() { assertTrue(CollectionUtils.isEmpty((Set<Object>) null)); assertTrue(CollectionUtils.isEmpty((Map<String, String>) null)); assertTrue(CollectionUtils.isEmpty(new HashMap<String, String>())); assertTrue(CollectionUtils.isEmpty(new HashSet<>())); List<Object> list = new LinkedList<>(); list.add(new Object()); assertFalse(CollectionUtils.isEmpty(list)); Map<String, String> map = new HashMap<>(); map.put("foo", "bar"); assertFalse(CollectionUtils.isEmpty(map)); }
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; }
HashMap<String, Double> map = new HashMap<String, Double>(); ValueComparator bvc = new ValueComparator(map); TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc); map.put("A", 99.5); map.put("B", 67.4); map.put("C", 67.4); map.put("D", 67.3); System.out.println("unsorted map: " + map); sorted_map.putAll(map); System.out.println("results: " + sorted_map); if (base.get(a) >= base.get(b)) { return -1; } else {
@SuppressWarnings("SameParameterValue") private LottieComposition createComposition(int startFrame, int endFrame) { LottieComposition composition = new LottieComposition(); composition.init(new Rect(), startFrame, endFrame, 1000, new ArrayList<Layer>(), new LongSparseArray<Layer>(0), new HashMap<String, List<Layer>>(0), new HashMap<String, LottieImageAsset>(0), new SparseArrayCompat<FontCharacter>(0), new HashMap<String, Font>(0)); return composition; }
@Test public void doNotCopyAttributes() throws Exception { Map<String, Object> attributes = new HashMap<>(); WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class); this.servletRequest.setSession(new MockHttpSession(null, "123")); this.servletRequest.getSession().setAttribute("foo", "bar"); HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor(); interceptor.setCopyAllAttributes(false); interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes); assertEquals(1, attributes.size()); assertEquals("123", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME)); }
@Override public Map<Errors, Integer> errorCounts() { HashMap<Errors, Integer> counts = new HashMap<>(); for (ReplicaElectionResult result : data.replicaElectionResults()) { for (PartitionResult partitionResult : result.partitionResult()) { Errors error = Errors.forCode(partitionResult.errorCode()); counts.put(error, counts.getOrDefault(error, 0) + 1); } } return counts; }
@SuppressWarnings("unchecked") public void addBeforeComment(String comment) { if (comment == null) { return; } if (attributes == null) { attributes = new HashMap<String, Object>(1); } List<String> comments = (List<String>) attributes.get("format.before_comment"); if (comments == null) { comments = new ArrayList<String>(2); attributes.put("format.before_comment", comments); } comments.add(comment); }