How to apply function that returns Result to each element of HashSet in Rust

As a language that aims for safety and performance, Rust provides a powerful data structure called HashSet that provides a fast and efficient way to store and retrieve unique values. HashSet is optimized for scenarios where you need to check for the presence of a value and avoid duplicates, making it a popular choice among… Read More How to apply function that returns Result to each element of HashSet in Rust

Puzzling about how computeIfAbsent works to update duplicate key->list value pair

From this guide, computeIfAbsent is used to create and update the value list, but how is it achieved? Here for map.computeIfAbsent("key1", k -> new ArrayList<>()).add("value2"); I was expecting no updates since key1 exist, however it ends up adding "value2" to the value list Map<String, List<String>> map = new HashMap<>(); map.computeIfAbsent("key1", k -> new ArrayList<>()).add("value1"); map.computeIfAbsent("key1",… Read More Puzzling about how computeIfAbsent works to update duplicate key->list value pair

Flutter entering to sub-sub collection in firebase

my nested collections looks like this: User–> drugs–> drugsTime I want to update data in document in the subsub-collection (drugsTime) however I dont know the drug document id and the drugtime document id so I did the following : ConfirmDrugTaken(DateTime d) async { final User? user = FirebaseAuth.instance.currentUser; final _uid = user?.uid; var drugID=""; //… Read More Flutter entering to sub-sub collection in firebase

How to remove two elements of collection at the same time? Task about cyclopes lenses

I cant solve this task. There are N cyclopes and an array of N elements. Every element is eyesight value of single cyclop. Every cyclop needs a lens with a value K but he will be okay with lens of value K+1 or K-1. Cyclopes always buy lenses in pairs. For example 5 cyclopes with… Read More How to remove two elements of collection at the same time? Task about cyclopes lenses

Remove a part of string based on its given length with replaceAll in Java

I know that there are different ways to solve this task, but I need a particular way using replaceAll() method. I just stuck with right condition in the expression. So I have a method like this: public static void handleComments(List<Comment> comments, int maxTextLength) { comments.replaceAll(comment -> comment.getText().length() > maxTextLength ? *what should be here?* :… Read More Remove a part of string based on its given length with replaceAll in Java

Throw custom exception when item not found in a Kotlin collection

I want to find a person from a list, or throw a PersonNotFoundException otherwise. The first() function from kotlin.collections throws NoSuchElementException in this scenario, but I want to throw my custom exception i.e. PersonNotFoundException. Currently I’m using the following logic: val persons: List<Person> = listOf(…) val name: String = "Bob" persons.firstOrNull { it.name == name… Read More Throw custom exception when item not found in a Kotlin collection