Sentry Sprint Boot Global Handler
Adding sentry.io is quite straightforward, but how do you add a global handler?
Adding sentry.io is quite straightforward, but how do you add a global handler?
The Problem I would like to summarize Person objects. Use the lastName if it is not null; if null, then use the firstName; else use the birthDay. If all these are null , then use the default text UNKNOWN. Usually, you write something like this: String summary = ""; if(p.getLastName() != null){ summary = p.getLastName(); } else if(p.getFirstName() != null){ summary = p.getFirstName(); } else if (p.getBirthDate() != null){ summary = p....
The problem I have a list of Person objects. I would like to group them per birth month, and only keep a List of lastnames. I came up with this code from memory Map<String, List<Person>> personsByMonth = persons.stream() .collect(Collectors.groupingBy(Person::getBirthMonth)); System.out.println(personsByMonth); And you get the following output (truncated for readability): {JUNE=[Person{lastName='Doe 11', birthDate=2022-06-07}], JANUARY=[Person{lastName='Doe 10', birthDate=2023-01-26}]} But how do I get rid of the redundant information? The solution The Collectors class provides quite a lot of variation of the groupingBy method....
Sorting collections has been quite easy with the Comparator class. But how do you deal with possible null values in for instance a List? You probably see already the NullPointerExceptions fly around, but fear not!
I would like to wrap up with some snippets I use a lot! Collect to a map When you want to collect to a Map, you need a function for the key and a function for the value. Suppose you have a list of Person objects with a firstname and last name. If you want to map the firstname to the lastname, you would write something like this: Map<String, String> firstToLastMap = persons....
Next to collectors, Streams offer reducers as well. You can consider it as building a result, starting from a partial solution and building on top of that. It is actually comparable to how the result is built up when using recursion. As an example, let’s take the sum of all integers starting from 1 up until n. The trivial solution is 0 for the ‘empty case’, this is the value before you start looping....
Streams are quite fun - in short: it’s a different approach to writing loops. Suppose you have a List of Strings and you want to filter out the String starting with an ’s' and put them in a new List. List<String> sStrings = new ArrayList<>(); for(int i = 0; i < strings.size(); i++){ if(strings.get(i).startsWith('s')){ //do something with strings starting with s sStrings.add(strings.get(i)); } } Although still readable, there is a lot of redundant code: the for loop itself with the counter and condition, the if statement....