Mar. 13th, 2019

jbanana: Badly drawn banana (Default)
You can turn a Java String into an IntStream, but that's annoyingly different to a regular Stream.

Getting a map of character counts:
s.chars()
    .collect(
        HashMap<Integer,Integer>::new,
        (map, value)->{
            Integer oldCount = map.get(value);
            map.put(value, null == oldCount ? 1 : oldCount + 1 );
        },
        HashMap::putAll
    )

Hmm, that's a lot of map-making boilerplate inside the collect bit. We can make it a Stream<Integer> and it's much simpler:
s.chars()
    .boxed()  // this bit makes collect easier
    .collect(
        Collectors.toMap(
            i->i,
            i->1,
            (i,b)->i+1
        )
    )

It's funny how the first example is now considered "boilerplate". Imagine doing it with anonymous inner classes instead of lambdas and method references.

It's also funny how chaining methods is the new normal. Some places where I worked would have to change their style guide.
jbanana: Badly drawn banana (Default)
We have learned how to run one test from Maven, and how to tell Maven you want to debug the test.

But if you want to use jdb as your debugger, you need to connect it something like this:
jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=5005 -sourcepath src\test\java\;src\main\java\

August 2025

M T W T F S S
    123
45678910
11121314151617
1819202122 2324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Oct. 2nd, 2025 02:31 am
Powered by Dreamwidth Studios