JPA/hibernate sorted collection @OrderBy vs @Sort

If you want to avoid non-standard annotations, you could make kittens use some sorted Collection implementation. This would ensure that kittens is always in sorted order. Something like this: @Entity public class Cat { @OneToMany(mappedBy = “cat”, cascade = CascadeType.ALL) @OrderBy(“name ASC”) private SortedSet<Kitten> kittens = new TreeSet<>(); } Note that this approach also requires … Read more

.NET: ArrayList vs List

ArrayLists are essentially deprecated as they’re untyped – you need to use casts with them – and they’re slower and less space efficient for value types because they require the items to be boxed. Generic lists were introduced with .Net 2.0 and are the way to go. Often a List is better than an array, … Read more

How to add element in List while iterating in java?

You can’t use a foreach statement for that. The foreach is using internally an iterator: The iterators returned by this class’s iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator … Read more

Which Java Collections are synchronized(thread safe), which are not?

Thread safe Collections – ConcurrentHashMap Thread safe without having to synchronize the whole map Very fast reads while write is done with a lock No locking at the object level Uses multitude of locks. SynchronizedHashMap Object level synchronization Both read and writes acquire a lock Locking the collection has a performance drawback May cause contention … Read more

ArrayList vs LinkedList from memory allocation perspective

LinkedList might allocate fewer entries, but those entries are astronomically more expensive than they’d be for ArrayList — enough that even the worst-case ArrayList is cheaper as far as memory is concerned. (FYI, I think you’ve got it wrong; ArrayList grows by 1.5x when it’s full, not 2x.) See e.g. https://github.com/DimitrisAndreou/memory-measurer/blob/master/ElementCostInDataStructures.txt : LinkedList consumes 24 … Read more

Listing all collections in a mongo database within a nodejs script

In the 2.0 version of the MongoDB driver for node.js you can use listCollections to get a cursor that contains the information of all collections. You can then call toArray on the cursor to retrieve the info. db.listCollections().toArray(function(err, collInfos) { // collInfos is an array of collection info objects that look like: // { name: … Read more

Java: Composite key in hashmaps

You could have a custom object containing the two strings: class StringKey { private String str1; private String str2; } Problem is, you need to determine the equality test and the hash code for two such objects. Equality could be the match on both strings and the hashcode could be the hashcode of the concatenated … Read more

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