Elegant way of counting occurrences in a java collection

Now let’s try some Java 8 code: static public Map<String, Integer> toMap(List<String> lst) { return lst.stream() .collect(HashMap<String, Integer>::new, (map, str) -> { if (!map.containsKey(str)) { map.put(str, 1); } else { map.put(str, map.get(str) + 1); } }, HashMap<String, Integer>::putAll); } static public Map<String, Integer> toMap(List<String> lst) { return lst.stream().collect(Collectors.groupingBy(s -> s, Collectors.counting())); } I think this … Read more

Publishing/subscribing multiple subsets of the same server collection

Could you not just use the same query client-side when you want to look at the items? In a lib directory: enabledItems = function() { return Items.find({enabled: true}); } processedItems = function() { return Items.find({processed: true}); } On the server: Meteor.publish(‘enabled_items’, function() { return enabledItems(); }); Meteor.publish(‘processed_items’, function() { return processedItems(); }); On the client … Read more

ArrayList vs LinkedList from memory allocation perspective

LinkedList might allocate fewer entries, but those entries are astronomically more expensive than they’d be for ArrayList — enough that even the worst-case ArrayList is cheaper as far as memory is concerned. (FYI, I think you’ve got it wrong; ArrayList grows by 1.5x when it’s full, not 2x.) See e.g. https://github.com/DimitrisAndreou/memory-measurer/blob/master/ElementCostInDataStructures.txt : LinkedList consumes 24 … Read more

Is there a way to check if two Collections contain the same elements, independent of order?

Apache commons-collections has CollectionUtils#isEqualCollection: Returns true if the given Collections contain exactly the same elements with exactly the same cardinality. That is, if the cardinality of e in a is equal to the cardinality of e in b, for each element e in a or b. Which is, I think, exactly what you’re after.

Is it possible to create some IGrouping object

If you really wanted to create your own IGrouping<TKey, TElement>, it is a simple interface to implement: public class Grouping<TKey, TElement> : List<TElement>, IGrouping<TKey, TElement> { public Grouping(TKey key) : base() => Key = key; public Grouping(TKey key, int capacity) : base(capacity) => Key = key; public Grouping(TKey key, IEnumerable<TElement> collection) : base(collection) => Key … Read more

IEnumerable and order

IEnumerable/IEnumerable<T> makes no guarantees about ordering, but the implementations that use IEnumerable/IEnumerable<T>may or may not guarantee ordering. For instance, if you enumerate List<T>, order is guaranteed, but if you enumerate HashSet<T> no such guarantee is provided, yet both will be enumerated using the IEnumerable<T> interface.

List, IList, IEnumerable, IQueryable, ICollection, which is most flexible return type?

Collections are not generally very useful for DAL returns, because a collection does not implicitly guarantee order. It’s just a bucket of items. An IList, on the other hand, does implicitly guarantee order. So we’re down to IEnumerable or IList. The next question would be: is the List object “live”? i.e., is it connected to … Read more