Why is my Entity Framework Code First proxy collection null and why can’t I set it?

As you correctly observed in the answer to your own question, removing the “virtual” keyword from the collection properties works around the problem, by preventing the Entity Framework from creating a change tracking proxy. However, this is not a solution for many people, because change tracking proxies can be really convenient and can help prevent … Read more

Converting Clojure data structures to Java collections

Clojure vector, set and list classes implement the java.util.Collection interface and ArrayList, HashSet and LinkedList can take a java.util.Collection constructor argument. So you can simply do: user=> (java.util.ArrayList. [1 2 3]) #<ArrayList [1, 2, 3]> user=> (.get (java.util.ArrayList. [1 2 3]) 0) 1 Similarly, Clojure map class implements java.util.Map interface and HashMap takes a java.util.Map … Read more

What is the best way get the symmetric difference between two sets in java?

You can use some functions from the Google Guava library (which is really great, I strongly recommend it!): Sets.difference(s1, s2); Sets.symmetricDifference(s1, s2); Javadocs for difference() and symmetricDifference() symmetricDifference() does exactly what you are asking for, but difference() is also often helpful. Both methods return a live view, but you can for example call .immutableCopy() on … Read more

How to handle nulls when using Java collection sort

Naturally, it’s your choice. Whatever logic you write, it will define sorting rules. So ‘should’ isn’t really the right word here. If you want null to appear before any other element, something like this could do public int compare(MyBean o1, MyBean o2) { if (o1.getDate() == null) { return (o2.getDate() == null) ? 0 : … Read more

HashSet.remove() and Iterator.remove() not working

I was very curious about this one still, and wrote the following test: import java.util.HashSet; import java.util.Iterator; import java.util.Random; import java.util.Set; public class HashCodeTest { private int hashCode = 0; @Override public int hashCode() { return hashCode ++; } public static void main(String[] args) { Set<HashCodeTest> set = new HashSet<HashCodeTest>(); set.add(new HashCodeTest()); System.out.println(set.size()); for (Iterator<HashCodeTest> … Read more

Is there a way to check if two Collections contain the same elements, independent of order?

Apache commons-collections has CollectionUtils#isEqualCollection: Returns true if the given Collections contain exactly the same elements with exactly the same cardinality. That is, if the cardinality of e in a is equal to the cardinality of e in b, for each element e in a or b. Which is, I think, exactly what you’re after.

Removing range (tail) from a List

subList(list.size() – N, list.size()).clear() is the recommended way to remove the last N elements. Indeed, the Javadoc for subList specifically recommends this idiom: This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing … Read more

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