How to copy a java.util.List into another java.util.List

Just use this: List<SomeBean> newList = new ArrayList<SomeBean>(otherList); Note: still not thread safe, if you modify otherList from another thread, then you may want to make that otherList (and even newList) a CopyOnWriteArrayList, for instance — or use a lock primitive, such as ReentrantReadWriteLock to serialize read/write access to whatever lists are concurrently accessed.

Sorted collection in Java

This comes very late, but there is a class in the JDK just for the purpose of having a sorted list. It is named (somewhat out of order with the other Sorted* interfaces) “java.util.PriorityQueue“. It can sort either Comparable<?>s or using a Comparator. The difference with a List sorted using Collections.sort(…) is that this will … Read more

Java 8: performance of Streams vs Collections

Stop using LinkedList for anything but heavy removing from the middle of the list using iterator. Stop writing benchmarking code by hand, use JMH. Proper benchmarks: @OutputTimeUnit(TimeUnit.NANOSECONDS) @BenchmarkMode(Mode.AverageTime) @OperationsPerInvocation(StreamVsVanilla.N) public class StreamVsVanilla { public static final int N = 10000; static List<Integer> sourceList = new ArrayList<>(); static { for (int i = 0; i < … Read more

Creating a blocking Queue in .NET?

That looks very unsafe (very little synchronization); how about something like: class SizeQueue<T> { private readonly Queue<T> queue = new Queue<T>(); private readonly int maxSize; public SizeQueue(int maxSize) { this.maxSize = maxSize; } public void Enqueue(T item) { lock (queue) { while (queue.Count >= maxSize) { Monitor.Wait(queue); } queue.Enqueue(item); if (queue.Count == 1) { // … Read more

Arrays.asList() vs Collections.singletonList()

Collections.singletonList(something) is immutable whereas Arrays.asList(something) is a fixed size List representation of an Array where the List and Array gets joined in the heap. Arrays.asList(something) allows non-structural changes made to it, which gets reflected to both the List and the conjoined array. It throws UnsupportedOperationException for adding, removing elements although you can set an element … Read more

Printing HashMap In Java

keySet() only returns a set of keys from your hash map, you should iterate this key set and the get the value from the hash map using these keys. In your example, the type of the hash map’s key is TypeKey, but you specified TypeValue in your generic for-loop, so it cannot be compiled. You … Read more

Why is HashSet so much slower than HashSet?

There are two perf problems induced by the Point struct. Something you can see when you add Console.WriteLine(GC.CollectionCount(0)); to the test code. You’ll see that the Point test requires ~3720 collections but the string test only needs ~18 collections. Not for free. When you see a value type induce so many collections then you need … Read more

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