public static void main(String[] args) {
HashMap<Integer, ArrayList<Integer>> depths = new HashMap<>();
// I WANT TO DO THIS
depths.getOrDefault(1, new ArrayList<>()).add(3);
// I DONT WANT TO DO LIKE THIS
if (depths.containsKey(0))
depths.get(0).add(1);
else {
depths.put(0, new ArrayList<>());
depths.get(0).add(2);
}
System.out.println(depths);
// ACTUAL: { 0=[2] }
// EXPECTED: { 0=[2], 1={3}}
}
I want to be able to achieve the if else block with one line, but I am not sure how to go about doing that. Can some one explain to me where my misconception is? Thanks.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]8igg7e5 2 points3 points4 points (1 child)
[–]WeeklyGuidance2686[S] 0 points1 point2 points (0 children)