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

Finding occurrences of a word in a string in python 3

If you’re going for efficiency: import re count = sum(1 for _ in re.finditer(r’\b%s\b’ % re.escape(word), input_string)) This doesn’t need to create any intermediate lists (unlike split()) and thus will work efficiently for large input_string values. It also has the benefit of working correctly with punctuation – it will properly return 1 as the count … Read more

SQL: Count() based on column value

You can use a CASE expression with your aggregate to get a total based on the outcomeId value: select companyId, sum(case when outcomeid = 36 then 1 else 0 end) SalesCount, sum(case when outcomeid <> 36 then 1 else 0 end) NonSalesCount from yourtable group by companyId; See SQL Fiddle with Demo

PowerShell: how to count number of rows in csv file?

Get-Content and Measure-Object are fine for small files, but both are super inefficient with memory. I had real problems with large files. When counting rows in a 1GB file using either method, Powershell gobbled up all available memory on the server (8GB), then started paging to disk. I left it over an hour, but it … Read more

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