MySQL – Select from a list of numbers those without a counterpart in the id field of a table

This is a problem that is pretty common: generating a relation on the fly without creating a table. SQL solutions for this problem are pretty awkward. One example using a derived table: SELECT n.id FROM (SELECT 2 AS id UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7) AS … Read more

List.shuffle() in Dart?

There is a shuffle method in the List class. The methods shuffles the list in place. You can call it without an argument or provide a random number generator instance: var list = [‘a’, ‘b’, ‘c’, ‘d’]; list.shuffle(); print(‘$list’); The collection package comes with a shuffle function/extension that also supports specifying a sub range to … Read more

Doctrine 2 OneToMany Cascade SET NULL

You should add the option onDelete=”SET NULL” in the annotation of your entity Publication like this: class Publication { /** * @ORM\ManyToOne(targetEntity=”Teacher”, inversedBy=”publications”) * @ORM\JoinColumn(name=”teacher_id”, referencedColumnName=”id”, onDelete=”SET NULL”) */ protected $teacher; } This modification is registered in the table declaration itself. Hence, you need a database migration or a schema change for this to take … Read more

Performance and Memory allocation comparison between List and Set

HashSet consumes about 5.5 times more memory than ArrayList for the same number of elements (although they’re both still linear), and has significantly slower iteration (albeit with the same asymptotics); a quick Google search suggests a 2-3x slowdown for HashSet iteration versus ArrayList. If you don’t care about uniqueness or the performance of contains, then … Read more

How to sort a HashSet?

A HashSet does not guarantee any order of its elements. If you need this guarantee, consider using a TreeSet to hold your elements. However if you just need your elements sorted for this one occurrence, then just temporarily create a List and sort that: Set<?> yourHashSet = new HashSet<>(); … List<?> sortedList = new ArrayList<>(yourHashSet); … Read more

HashSet contains duplicate entries

The problem is that your Element class has not overridden the equals and hashCode methods or these implementations are broken. From Object#equals method javadoc: The equals method implements an equivalence relation on non-null object references: It is reflexive: for any non-null reference value x, x.equals(x) should return true. It is symmetric: for any non-null reference … Read more

Set iteration order varies from run to run

The reason the set iteration order changes from run-to-run appears to be because Python uses hash seed randomization by default. (See command option -R.) Thus set iteration is not only arbitrary (because of hashing), but also non-deterministic (because of the random seed). You can override the random seed with a fixed value by setting the … Read more