Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map: HashMap, TreeMap #32

Open
YuezhenQin opened this issue Dec 24, 2023 · 4 comments
Open

Map: HashMap, TreeMap #32

YuezhenQin opened this issue Dec 24, 2023 · 4 comments
Assignees

Comments

@YuezhenQin
Copy link
Owner

A Map is an object that maps keys to values, or is a collection of key-value pairs. It models the function abstraction in mathematics.

@YuezhenQin YuezhenQin converted this from a draft issue Dec 24, 2023
@YuezhenQin YuezhenQin self-assigned this Dec 24, 2023
@YuezhenQin
Copy link
Owner Author

YuezhenQin commented Feb 14, 2024

Anytime you need to count anything, think about using a hash map to do it.

  1. k distinct chars: Find the length of the longest substring that contains at most k distinct characters.
  2. top k: Find the k most frequent elements.

@YuezhenQin
Copy link
Owner Author

YuezhenQin commented Feb 15, 2024

How to increment a HashMap value in Java?

map.put(map.getOrDefault(key, 0) + 1)

@YuezhenQin YuezhenQin changed the title HashMap* HashMap Feb 16, 2024
@YuezhenQin YuezhenQin moved this from In Progress to Done in dsa-in-java Mar 14, 2024
@YuezhenQin YuezhenQin changed the title HashMap HashMap and TreeMap Mar 16, 2024
@YuezhenQin YuezhenQin changed the title HashMap and TreeMap Map: HashMap and TreeMap Mar 16, 2024
@YuezhenQin YuezhenQin changed the title Map: HashMap and TreeMap Map: HashMap, TreeMap Mar 17, 2024
@YuezhenQin
Copy link
Owner Author

TreeMap

@YuezhenQin
Copy link
Owner Author

YuezhenQin commented Mar 17, 2024

Find the Key associated with max Value in a Java Map

Map.Entry<Integer, Integer> maxEntry = null;

for (Map.Entry<Integer, Integer> entry : map.entrySet())
{
    if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
    {
        maxEntry = entry;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

1 participant