Skip to content

HTbuilderKid/HashMap-HashSet-classes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

24. A HashMap in Java is basically a fast key => value storage, kind of like a dictionary. You give it a key (like a String or an int), it hashes it into a bucket, and then lets you quickly store or retrieve an associated value. Most of the time, it does this really fast, and the time for put() and get() are constant. It comes from the Map interface, which means that it supports core operations like adding things (put), looking them up (get), checking to see if something exists (containKey), looping through everything (entrySet/keySet), etc. 
However, it doesn't keep any specific order of the elements, which is bad if you want to sort items.

25. Since there are a lot of methods that depend on the type used to parametize it, I won't be listing all of them, or I'd run out of space.
But any method that accepts or returns K or V depends on the parameterized types.
Some of these methods include:
-V put(K key, V value)
-V get(Object key)
-V remove(Object key)
-V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)

26. We can check for the amount of entries contained in a map with the size method: map.size()

28. If you do that, the old value will get replaced with the new one

29. Both entries will be stored separately, and each will have a unique value

30. As mentioned in question 24, we just have to check it using containsKey()

31. Then the get() method will return null

32. We can use keySet() to loop through it

34. In an ArrayList, duplicates are allowed, while they are automatically removed in a HashSet. Also, you can access an element in an ArrayList by index, but the elements in a HashSet aren't indexed.

35. When using ArrayList, the words you added appear in the exact order that you added them, while in HashSet, there is no specific order, and any dupplicate gets automatically removed. 
So, for examplem, if we inserted ["red", "blue", "red"], ArrayList: ["red", "blue", "red"], while HashSet: ["red", "blue] or ["blue", red"]

36. We can use a regular expression that treats multiple spaces as one to solve the problem: String[] words = text.split("\\s+");

About

project for the fifth lab

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages