Skip to content

Tag: guava

ImmutableMap.of vs Java Map.of

I have seen that ImmutableMap.of is used to create immutable map object. At the same time Java offers Map.of which creates immutable map as well. Is there any advantage of using ImmutableMap.of instead of Map.of from Java ? Edit: I am not asking differences between map and immutable map. I am asking why some …

Verify Hashing.sha256() generated hash

I have this code created using Google Guava: How I can verify the generated values is a properly generated hash? Answer SHA-256 and, in general, the family of SHA 2 algorithms is wonderfully described in Wikipedia and different RFCs, RFC 6234 and the superseded RFC 4634. All these sources dictate that the out…

Dynamic return type based on input argument type

I’m trying to write a small function that takes two lists and sorts one based on the elements of the other. So something like: would result in a sorted list [E, C, A, D, B]. However, valuesToSort might be a list of something different, like integers, floats or other lists. Ideally, I would want my progr…

compareTo: treat two nulls as equal

I would like my compare method to work so that in the case where field1 == null in both objects, comparation would not be determined, but field2 would be checked, and so on. How to do it in the simplest way? The method below does not work well: when the fields from both objects are null, the comparison is alw…

Guava Maps.difference with wildcard

I want to use Guava’s Maps.difference in java 11 to verify data from Json String which I mapped into a Map. What I have after a call (with different uid each time) : I want to verify that uid is correctly generated and name is “Jean” using a pattern like this : Of course Maps.difference retu…

How to thread-safe update loadingcache value guava map

For the test, in the addCache method, I created and added a map. The card has a key “a” and a value “1111”. And key “b” for LoadingCache. Next, I want to update the value “1111” to the value “2222”. To do this, I pass all the necessary parameters fro…

Stream return class type generic

I have a list like this and i have a method to find a house by its name, but i want it return the class type not the house interface, example when i do findHouse(“third”) i want it to return House.WithoutPlace not House, how i can do that? Answer You cannot do this at all, unless you know, which t…

Stream mapping multiple types

How can I convert this code to the one that uses streams? one line, if possible: Answer Well, IMO “in one line” is not a great point to use Stream because they came at a cost, but you can do something like this: return ImmutableList.of( houseConstructor.newInstance( 5, Houses.all() .entrySet() .st…