Java LinkedHashMap get first or last entry

The semantics of LinkedHashMap are still those of a Map, rather than that of a LinkedList. It retains insertion order, yes, but that’s an implementation detail, rather than an aspect of its interface. The quickest way to get the “first” entry is still entrySet().iterator().next(). Getting the “last” entry is possible, but will entail iterating over … Read more

Return first N key:value pairs from dict

There’s no such thing a the “first n” keys because a dict doesn’t remember which keys were inserted first. You can get any n key-value pairs though: n_items = take(n, d.iteritems()) This uses the implementation of take from the itertools recipes: from itertools import islice def take(n, iterable): “Return first n items of the iterable … Read more

Accessing items in an collections.OrderedDict by index

If its an OrderedDict() you can easily access the elements by indexing by getting the tuples of (key,value) pairs as follows >>> import collections >>> d = collections.OrderedDict() >>> d[‘foo’] = ‘python’ >>> d[‘bar’] = ‘spam’ >>> d.items() [(‘foo’, ‘python’), (‘bar’, ‘spam’)] >>> d.items()[0] (‘foo’, ‘python’) >>> d.items()[1] (‘bar’, ‘spam’) Note for Python 3.X dict.items … Read more

Is there a more elegant way of adding an item to a Dictionary safely?

Just use the indexer – it will overwrite if it’s already there, but it doesn’t have to be there first: Dictionary<string, object> currentViews = new Dictionary<string, object>(); currentViews[“Customers”] = “view1”; currentViews[“Customers”] = “view2”; currentViews[“Employees”] = “view1”; currentViews[“Reports”] = “view1”; Basically use Add if the existence of the key indicates a bug (so you want it … Read more

Calculate difference in keys contained in two Python dictionaries

You can use set operations on the keys: diff = set(dictb.keys()) – set(dicta.keys()) Here is a class to find all the possibilities: what was added, what was removed, which key-value pairs are the same, and which key-value pairs are changed. class DictDiffer(object): “”” Calculate the difference between two dictionaries as: (1) items added (2) items … Read more

String to Dictionary in Python [duplicate]

This data is JSON! You can deserialize it using the built-in json module if you’re on Python 2.6+, otherwise you can use the excellent third-party simplejson module. import json # or `import simplejson as json` if on Python < 2.6 json_string = u'{ “id”:”123456789″, … }’ obj = json.loads(json_string) # obj now contains a dict … Read more

How to remove a key from HashMap while iterating over it? [duplicate]

Try: Iterator<Map.Entry<String,String>> iter = testMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String,String> entry = iter.next(); if(“Sample”.equalsIgnoreCase(entry.getValue())){ iter.remove(); } } With Java 1.8 and onwards you can do the above in just one line: testMap.entrySet().removeIf(entry -> “Sample”.equalsIgnoreCase(entry.getValue()));

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)