2
MultivaluedMap map= new MultivaluedMapImpl();
map.add("Accept-Encoding", "compress;q=0.5");
map.add("Accept-Encoding", "gzip;q=1.1");
map.add("Accept-Encoding", "gzip;q=1.2");
map.add("Accept-Encoding", "gzip;q=1.3");

How can I find the size of the key "Accept-Encoding"?

Rakesh B
  • 47
  • 1
  • 1
  • 9

2 Answers2

2

MultivaluedMap<K, V> implements Map<K,List<V>> so

((Map<String,List<String>>)map).get("Accept-Encoding").size();

Now I see that you use non generic version (which is not a good idea):

Map tmp = map instanceof Map ? (Map) map : new HashMap();
// because you don't use generic you should get value as an Object instance
Object value = tmp.get("Accept-Encoding");
// by default we assume that there the value is not a List instance
int size = 0;    

// for security we check that the value instance implements List interface
if (value instanceof List) {
    // we check size of that list
    size = ((List) value).size();
}

Short version (and without checking):

((List) ((Map) map).get("Accept-Encoding")).size();

You should see why using generic version is better than raw version.

lukaslew
  • 455
  • 1
  • 6
  • 13
0

Use map.get(itr.next()).size()

Code:

MultivaluedMap map= new MultivaluedMapImpl();
map.add("Accept-Encoding", "compress;q=0.5");
map.add("Accept-Encoding", "gzip;q=1.1");
map.add("Accept-Encoding", "gzip;q=1.2");
map.add("Accept-Encoding", "gzip;q=1.3");

Iterator itr = map.keySet().iterator();
        while (itr.hasNext()) {
            System.out.println(map.get(itr.next()).size());
        }
rhitz
  • 1,892
  • 2
  • 21
  • 26