我需要一个Map,一旦一个键获得一个值,任何额外的尝试将值放在同一个键上将抛出异常.
例如:
map.put("John", 3); //OK
map.put("John", 7); // throws some exception
map.put("John", 11); // throws some exception
Run Code Online (Sandbox Code Playgroud)
当然,我可以在我自己实现这个(例如,通过扩展HashMap,或每次调用到周围put用if map.contains(key)),但我更喜欢使用一些现成的,保持我的代码清洁.
有人知道这样的实施吗?
使用窗口时,如何获取当前分区的大小(行数)?
例如,假设我有一个表,其中包含博客中帖子的评论。我想知道每个帖子的第一条评论、第二条评论、最后一条评论和评论数是什么(没有另一个子查询,我按 post 和 do 分组COUNT(*))。
查询应类似于:
SELECT DISTINCT
post_id.
first_value(comment_text) OVER wnd AS first_comment,
nth_value(comment_text, 2) OVER wnd AS second_comment,
last_value(comment_text) OVER wnd AS last_comment,
SOME_FUNCTION(comment_text) OVER wnd AS number_of_comments
FROM comments
WINDOW wnd AS (
PARTITION BY post_id
ORDER BY comment_created_at ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
);
Run Code Online (Sandbox Code Playgroud)
应该SOME_FUNCTION是什么?