groovy: safely find a key in a map and return its value

The whole point of using Maps is direct access. If you know for sure that the value in a map will never be Groovy-false, then you can do this: def mymap = [name:”Gromit”, likes:”cheese”, id:1234] def key = “likes” if(mymap[key]) { println mymap[key] } However, if the value could potentially be Groovy-false, you should use: … Read more

Get an arbitrary key/item from a map

Whether getting an arbitrary key from a hash table is a common requirement may be discussed. Other language map implementations often lack this feature (eg. Dictionary in C# ) However, your solution is probably the fastest one, but you will be left with a pseudo-random algorithm that you do not control. And while the current … Read more

Can a JPA Query return results as a Java Map?

Returning a Map result using JPA Query getResultStream Since the JPA 2.2 version, you can use the getResultStream Query method to transform the List<Tuple> result into a Map<Integer, Integer>: Map<Integer, Integer> postCountByYearMap = entityManager.createQuery(“”” select YEAR(p.createdOn) as year, count(p) as postCount from Post p group by YEAR(p.createdOn) “””, Tuple.class) .getResultStream() .collect( Collectors.toMap( tuple -> ((Number) … Read more

create pandas dataframe from dictionary of dictionaries

You can pass the dict of dict to the DataFrame constructor: In [11]: d = {‘Jill’: {‘Django Unchained’: 6.5, ‘Gone Girl’: 9.0, ‘Kill the Messenger’: 8.0, ‘Avenger: Age of Ultron’: 7.0}, ‘Toby’: {‘Django Unchained’: 9.0, ‘Zoolander’: 2.0, ‘Avenger: Age of Ultron’: 8.5}} In [12]: pd.DataFrame(d) Out[12]: Jill Toby Avenger: Age of Ultron 7.0 8.5 Django … Read more

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