Cannot reach values in map [duplicate]

Asked
Active3 hr before
Viewed126 times

8 Answers

valuescannot
90%

Please stand by, while we are checking your browser...,Completing the CAPTCHA proves you are a human and gives you temporary access to the web property.,If you are at an office or shared network, you can ask the network administrator to run a scan across the network looking for misconfigured or infected devices.

Pretag
 Pretag team - issue, fix, solve, resolve
88%

Map does not supports duplicate keys. you can use collection as value against same key.,Because if the map previously contained a mapping for the key, the old value is replaced by the specified value.,you can use something like this.

Because if the map previously contained a mapping for the key, the old value is replaced by the specified value.

Map<Integer, List<String>>
load more v
72%

The problem is that I cannot form the above map because I am forgetting the fact that maps cannot have duplicate KEYS :( , Can you post an example script for the above map I am trying to understand but saturated and not able to think more :( – Rao Jan 31 '13 at 17:00 ,I have custom setting to check if the map has the key "product name" and then add up all the values

Map of lists maybe?

List<Account> fakeQuotes = new List<Account>{
    new Account(Name = 'Prod 1', AnnualRevenue = 100),
    new Account(Name = 'Prod 2', AnnualRevenue = 200),
    new Account(Name = 'Prod 1', AnnualRevenue = 300),
    new Account(Name = 'Prod 2', AnnualRevenue = 400),
    new Account(Name = 'Prod 1', AnnualRevenue = 500)
};

Map<String, List<Double>> listPrices = new Map<String, List<Double>>();
for(Account a : fakeQuotes){
    List<Double> temp = listPrices.get(a.Name);
    if(temp == null) {
        listPrices.put(a.Name, new List<Double>{a.AnnualRevenue});
    } else {
        temp.add(a.AnnualRevenue);
    }
}

System.debug(listPrices); // {Prod 1=(100.0, 300.0, 500.0), Prod 2=(200.0, 400.0)}
load more v
65%

public Object get(Object k):This method is used to return the value associated with the key k.,public Object remove(Object k): This method is used to delete an entry whose key equals k.,public Object put(Object key, Object value): This method is used to insert an entry in the map.

Pretag
 Pretag team - issue, fix, solve, resolve
75%

Pretag
 Pretag team - issue, fix, solve, resolve
40%

This flexibility provides a potent illustration of the power of an interface-based framework.,Suppose you want to remove from one Map all of the keys that have mappings in another.,values — The Collection of values contained in the Map. This Collection is not a Set, because multiple keys can map to the same value.

// Group employees by department
Map<Department, List<Employee>> byDept = employees.stream()
   .collect(Collectors.groupingBy(Employee::getDepartment));
load more v
22%

The map performs get, put, contains like operations constant in time irrespective of the Map size.,Maps allow one and only one null keys.,Baceause of this the normal put, get, contains etc operations are constant in time. Map takes same amount of time to put or get an object no matter how many elements are present in the Map.

Java 9 Provides, factory methods to create Immutable maps inline.

.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines) {white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}// Created Empty Immutable HashMap using Java 9 factory methods
Map<String, String> map = Map.of();

System.out.println(map.size())              // output : 0

map.put("1", "2");


// Output
// Exception java.lang.UnsupportedOperationException
//        at ImmutableCollections.uoe (ImmutableCollections.java:71)
//        at ImmutableCollections$AbstractImmutableMap.put (ImmutableCollections.java:714)
//        at (#3:1)Code language: Java (java)
load more v
60%

It is the default constructor which creates an instance of HashMap with an initial capacity of 16 and load factor of 0.75.,It creates a HashMap instance with a specified initial capacity and load factor of 0.75.,It creates a HashMap instance with a specified initial capacity and specified load factor.

load more v

Other "values-cannot" queries related to "Cannot reach values in map [duplicate]"