Java ArrayList and HashMap on-the-fly
List<String> list = new ArrayList<String>() { { add(“value1”); add(“value2”); } }; Map<String,String> map = new HashMap<String,String>() { { put(“key1”, “value1”); put(“key2”, “value2”); } };
List<String> list = new ArrayList<String>() { { add(“value1”); add(“value2”); } }; Map<String,String> map = new HashMap<String,String>() { { put(“key1”, “value1”); put(“key2”, “value2”); } };
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
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
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
You could do this element adding to 1. or last place: Adding to last place ► You just need to remove the previous entry from the map like this: map.remove(key); map.put(key,value); Adding to first place ► It’s a bit more complicated, you need to clone the map, clear it, put the 1. value to it, … Read more
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
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
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>>.
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
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);