Performance differences… so dramatic?

Concerning 1: Stack<T>‘s and List<T>‘s performance being similar isn’t surprising. I’d expect both of them to use arrays with a doubling strategy. This leads to amortized constant-time additions. You can use List<T> everywhere you can use Stack<T>, but it leads to less expressive code. Concerning 2: I think I know why List<T> doesn’t handle the … Read more

Is it possible in java make something like Comparator but for implementing custom equals() and hashCode()

Yes it is possible to do such a thing. (And people have done it.) But it won’t allow you to put your objects into a HashMap, HashSet, etc. That is because the standard collection classes expect the key objects themselves to provide the equals and hashCode methods. (That is the way they are designed to … Read more

filtering a list using LINQ

EDIT: better yet, do it like that: var filteredProjects = projects.Where(p => filteredTags.All(tag => p.Tags.Contains(tag))); EDIT2: Honestly, I don’t know which one is better, so if performance is not critical, choose the one you think is more readable. If it is, you’ll have to benchmark it somehow. Probably Intersect is the way to go: void … Read more

The correct way to return the only element from a set

You can use an Iterator to both obtain the only element as well as verify that the collection only contains one element (thereby avoiding the size() call and the unnecessary list creation): Iterator<Element> iterator = set.iterator(); if (!iterator.hasNext()) { throw new RuntimeException(“Collection is empty”); } Element element = iterator.next(); if (iterator.hasNext()) { throw new RuntimeException(“Collection … Read more

What does Collection.Contains() use to check for existing objects?

List<T>.Contains uses EqualityComparer<T>.Default, which in turn uses IEquatable<T> if the type implements it, or object.Equals otherwise. You could just implement IEquatable<T> but it’s a good idea to override object.Equals if you do so, and a very good idea to override GetHashCode() if you do that: public class SomeIDdClass : IEquatable<SomeIDdClass> { private readonly int _id; … Read more

Convert dictionary to List

To convert a Dictionary<TKey, TValue> to a List<KeyValuePair<TKey, TValue>> you can just say var list = dictionary.ToList(); or the more verbose var list = dictionary.ToList<KeyValuePair<TKey, TValue>>(); This is because Dictionary<TKey, TValue> implements IEnumerable<KeyValuePair<TKey, TValue>>.

Javascript collection

Literals The [] and {} are called the array and object literals respectively. var x = [] is short for var x = new Array(); and var y = {} is short for var y = new Object(); Arrays Arrays are structures with a length property. You can access values via their numeric index. var … Read more

How to remove duplicates from a list?

Assuming you want to keep the current order and don’t want a Set, perhaps the easiest is: List<Customer> depdupeCustomers = new ArrayList<>(new LinkedHashSet<>(customers)); If you want to change the original list: Set<Customer> depdupeCustomers = new LinkedHashSet<>(customers); customers.clear(); customers.addAll(dedupeCustomers);

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