Preferred Idiom for Joining a Collection of Strings in Java

I’d say the best way of doing this (if by best you don’t mean “most concise”) without using Guava is using the technique Guava uses internally, which for your example would look something like this: Iterator<String> iter = data.iterator(); StringBuilder sb = new StringBuilder(); if (iter.hasNext()) { sb.append(iter.next()); while (iter.hasNext()) { sb.append(separator).append(iter.next()); } } String … Read more

Getting list of currently active managed threads in .NET?

If you’re willing to replace your application’s Thread creations with another wrapper class, said wrapper class can track the active and inactive Threads for you. Here’s a minimal workable shell of such a wrapper: namespace ThreadTracker { using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading; public class TrackedThread { private static readonly IList<Thread> threadList = new List<Thread>(); … Read more

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

Is there a way to get the value of a HashMap randomly in Java?

This works: Random generator = new Random(); Object[] values = myHashMap.values().toArray(); Object randomValue = values[generator.nextInt(values.length)]; If you want the random value to be a type other than an Object simply add a cast to the last line. So if myHashMap was declared as: Map<Integer,String> myHashMap = new HashMap<Integer,String>(); The last line can be: String randomValue … Read more

Thread safe collections in .NET

The .NET 4.0 Framework introduces several thread-safe collections in the System.Collections.Concurrent Namespace: ConcurrentBag<T>       Represents a thread-safe, unordered collection of objects. ConcurrentDictionary<TKey, TValue>     Represents a thread-safe collection of key-value pairs that can be accessed by multiple threads concurrently. ConcurrentQueue<T>     Represents a thread-safe first in-first out (FIFO) collection. ConcurrentStack<T>   … Read more