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

How do I sort a Set to a List in Java?

The answer provided by the OP is not the best. It is inefficient, as it creates a new List and an unnecessary new array. Also, it raises “unchecked” warnings because of the type safety issues around generic arrays. Instead, use something like this: public static <T extends Comparable<? super T>> List<T> asSortedList(Collection<T> c) { List<T> … Read more

Change priorityQueue to max priorityqueue

How about like this: PriorityQueue<Integer> queue = new PriorityQueue<>(10, Collections.reverseOrder()); queue.offer(1); queue.offer(2); queue.offer(3); //… Integer val = null; while( (val = queue.poll()) != null) { System.out.println(val); } The Collections.reverseOrder() provides a Comparator that would sort the elements in the PriorityQueue in a the oposite order to their natural order in this case.

Collection versus List what should you use on your interfaces?

To answer the “why” part of the question as to why not List<T>, The reasons are future-proofing and API simplicity. Future-proofing List<T> is not designed to be easily extensible by subclassing it; it is designed to be fast for internal implementations. You’ll notice the methods on it are not virtual and so cannot be overridden, … Read more

Comparing two collections for equality irrespective of the order of items in them

It turns out Microsoft already has this covered in its testing framework: CollectionAssert.AreEquivalent Remarks Two collections are equivalent if they have the same elements in the same quantity, but in any order. Elements are equal if their values are equal, not if they refer to the same object. Using reflector, I modified the code behind … Read more

Filtering collections in C#

If you’re using C# 3.0 you can use linq, which is way better and way more elegant: List<int> myList = GetListOfIntsFromSomewhere(); // This will filter ints that are not > 7 out of the list; Where returns an // IEnumerable<T>, so call ToList to convert back to a List<T>. List<int> filteredList = myList.Where(x => x … Read more

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