why is map.get() function null? - java

My code iterates over a char array (char[] charArray), and finds for the key. The key is then used to find the value in a Map. However, the map.get() function is giving me a null even thought I set it as a value and i am not sure why. Thanks!
parameters for this method are:
LinkedBinaryTree<String> and HashMap<String, Integer>
for (int i = 0; i < charArray.length; i++) {
if (Character.isLetter(charArray[i])) {
// Why is map.get(charArray[i]) null??
String char2String = Character.toString(charArray[i]);
if (map.get(char2String) == null) {
throw new IllegalArgumentException("int is null");
}
int k = map.get(char2String);
charArray[i] = Integer.toString(k).charAt(0);
}
}
This is a testcase i wrote to test the method.
#Test
public void testSubstituteMap() {
LinkedBinaryTree<String> tree2 = Assignment.prefix2tree("- x + 1 + x x");
HashMap<String, Integer> Map = new HashMap<String, Integer>();
Map.put("x", 5);
tree2 = Assignment.substitute(tree2, Map);
LinkedBinaryTree<String> expected = Assignment.prefix2tree("- 5 + 1 + 5 5");
assertEquals(Assignment.tree2prefix(expected), Assignment.tree2prefix(tree2));
}
Which results in:

You are putting String keys and trying to get the same with a char key.
Note that in Java "x" is not equals to 'x'
You can either change your Map to char keys or use String array. Since they are single letters, I suggest to keep them with Char's.

Related

Creating a HashMap of chars in a string and integers in an ArrayList<integer>

I have to create a HashMap that records the letters in a string and their index values in a ArrayList, so that if the HashMap is called with some string key, each related index integer is returned, and so that the map can be called by itself such that each key is shown with their indexes, For example for the string "Hello World", the map would look something like:
d=[9], o=[4, 6], r=[7], W=[5], H=[0], l=[2, 3, 8], e=[1].
I'm really confused by the requirement of the inputs as String and ArrayList, rather than chars and integers. Could you explain to me the relationship of the map to those objects, and to their components which are ultimately what are recorded as keys and values? When trying to debug, it stops processing before the map call.
The error message is:
java.lang.AssertionError: Wrong number of entries in Concordance. Expected: 5. Got: 1
Expected :1
Actual :5
But I really think I'm not grasping HashMap very well, so I'd appreciate if anyone could guide me through the basics, or provide anything educational about using HashMap, especially ones that use ArrayList.
public HashMap<String, ArrayList<Integer>> concordanceForString(String s) {
HashMap<String, ArrayList<Integer>> sMap = new HashMap<>();//create map "sMap"
char[] sArray = new char[s.length()]; //create character array, "sArray", for string conversion
ArrayList<Integer> sCharIndex = new ArrayList<Integer>();
for (int i = 0; i < s.length(); i++) {
sArray[i] = s.charAt(i); // convert string into array
}
for (int j = 0; j < sArray.length; j++){
sCharIndex.add(j); // add char indexes to index ArrayList
}
sMap.put(s, sCharIndex); //add the String and ArrayList
return sMap; // I feel like this should be sMap.get(s) but when I do, it gives me the zigzag red underline.
}
Here is a way to do it:
String input = "hello world";
Map<String, List<Integer>> letters = new HashMap<String, List<Integer>>();
// remove all whitespace characters - since it appears you are doing that
String string = input.replaceAll("\\s", "");
// loop over the length of the string
for (int i = 0; i < string.length(); i++) {
// add the entry to the map
// if it does not exist, then a new List with value of i is added
// if the key does exist, then the new List of i is added to the
// existing List
letters.merge(string.substring(i, i + 1),
Arrays.asList(i),
(o, n) -> Stream.concat(o.stream(), n.stream()).collect(Collectors.toList()));
}
System.out.println(letters);
that gives this output:
{r=[7], d=[9], e=[1], w=[5], h=[0], l=[2, 3, 8], o=[4, 6]}
EDIT - this uses a Character as the key to the map:
String input = "hello world";
Map<Character, List<Integer>> letters = new HashMap<Character, List<Integer>>();
String string = input.replaceAll("\\s", "");
for (int i = 0; i < string.length(); i++) {
letters.merge(string.charAt(i), Arrays.asList(i), (o, n) ->
Stream.concat(o.stream(), n.stream()).collect(Collectors.toList()));
}
System.out.println(letters);
Essentially, this is what you want to do.
This presumes a HashMap<String, List<Integer>>
List<Integer> sCharIndex;
for (int i = 0; i < s.length(); i++) {
// get the character
char ch = s.charAt(i);
if (!Character.isLetter(ch)) {
// only check letters
continue;
}
ch = ch+""; // to string
// get the list for character
sCharIndex = sMap.get(ch);
// if it is null, create one and add it
if (sCharIndex == null) {
// create list
sCharIndex = new ArrayList<>();
// put list in map
sMap.put(ch, sCharIndex);
}
// at this point you have the list so
// add the index to it.
sCharIndex.add(i);
}
return sMap;
A hashMap is nothing more than a special data structure that takes an object as a key. Think of an array that takes a digit as an index and you can store anything there.
A hashMap can take anything as a key (like an index but it is called a key) and it can also store anything.
Note that your key to hashMap is a String but you're using a character which is not the same. So you need to decide which you want.
HashMap<String, List<Integer>> or HashMap<Character, List<Integer>>
There are also easier ways to do this but this is how most would accomplish this prior to Java 8.
Here is a much more compact way using streams. No loops required.
Map<String, List<Integer>> map2 = IntStream
.range(0,s.length())
// only look for letters.
.filter(i->Character.isLetter(s.charAt(i)))
.boxed()
// stream the Integers from 0 to length
// and group them by character in a list of indices.
.collect(Collectors.groupingBy(i->s.charAt(i)+""));
But I recommend you become familiar with the basics before delving into streams (or until your instructor recommends to do so).
For more information check out The Java Tutorials
Check out this code :
public static void main(String []args){
//Create map of respective keys and values
HashMap<Character, ArrayList<Integer>> map = new HashMap();
String str = "Hello world"; //test string
int length = str.length(); //length of string
for(int i = 0; i < length; i++){
ArrayList<Integer> indexes = new ArrayList(); //empty list of indexes
//character of test string at particular position
Character ch = str.charAt(i);
//if key is already present in the map, then add the previous index associated with the character to the indexes list
if(map.containsKey(ch)){
//adding previous indexes to the list
indexes.addAll(map.get(ch));
}
//add the current index of the character to the respective key in map
indexes.add(i);
//put the indexes in the map and map it to the current character
map.put(ch, indexes);
}
//print the indexes of 'l' character
System.out.print(map.get('l'));
}
The code is self explanatory.
public class Array {
public static void main(String[] args) {
printSortedMap(concordanceForString("Hello world")); // r[7] d[9] e[1] w[5] H[0] l[2, 3, 8] o[4, 6]
}
public static HashMap<String, ArrayList<Integer>> concordanceForString(String s) {
HashMap<String, ArrayList<Integer>> sMap = new HashMap<>();
String str = s.replace(" ", "");
for (int i = 0; i < str.length(); i++) {
ArrayList<Integer> sCharIndex = new ArrayList<Integer>();
for (int j = 0; j < str.length(); j++) {
if ( str.charAt(i) == str.charAt(j) ) {
sCharIndex.add(j);
}
}
sMap.put(str.substring(i,i+1), sCharIndex);
}
return sMap;
}
public static void printSortedMap(HashMap<String, ArrayList<Integer>> sMap) {
for (Map.Entry<String, ArrayList<Integer>> entry : sMap.entrySet()) {
System.out.println(entry.getKey() + entry.getValue());
}
}

how to remove duplicate results generating from hashmap

I was given a problem - there is a box containing label and has value in it. for example - box with label 'a' contains value 2, box with label 'b' contains value 7 etc. I was asked to find the pairs that adds to sum 10. I wrote below code to find the pair using hashmap. But it contains same result twice with only difference of position of letter.
For example - (a,e) is same as (e,a)
How can I keep/print only the unique pair?
Below is my code :
public class BoxValue {
public void pair()
{
int sum = 10;
HashMap<Integer, Character> m = new HashMap<Integer, Character>();
m.put(2, 'a');
m.put(7, 'b');
m.put(4, 'c');
m.put(3, 'd');
m.put(8, 'e');
m.put(6, 'f');
Set<Integer> s = m.keySet();
for(Integer in : s)
{
if(m.containsKey(sum-in))
System.out.println(m.get(in)+","+m.get(sum-in));
}
}
public static void main(String[] args) {
BoxValue bv = new BoxValue();
bv.pair();
}
}
I would just use a Set to keep track of which complements you've already seen:
Set<Integer> seen = new HashSet<>();
for (Integer in : m.keySet())
{
if(m.containsKey(sum-in) && !seen.contains(in))
{
System.out.println(m.get(in) + "," + m.get(sum-in));
seen.add(sum-in);
}
}
You can do it without any additional storage if you're able to modify the map, at the loss of some clarity:
for (Integer in : m.keySet())
{
if(m.containsKey(sum-in) && m.get(in) != null)
{
System.out.println(m.get(in) + "," + m.get(sum-in));
m.put(sum-in, null);
}
}
Output:
a,e
d,b
c,f
You can use a map for keys
List<Integer> s = new ArrayList<>(m.keySet()); // Create a key list
HashMap<Integer, Boolean> pos = new HashMap<Integer, Boolean>(); // Map for used key
for (int i = 0; i < s.size(); i++) {
if (m.containsKey(sum - s.get(i)) && !pos.containsKey(sum - s.get(i)))
System.out.println(m.get(s.get(i)) + "," + m.get(sum - s.get(i)));
pos.put(s.get(i), true); // Update map
}

java- How to find intersection of values in Hashset in loop?

I wrote a program in Java where it retrieves records from a database table and stored them in a hashmap.
The keys and values of them are like the following:
Key(represent words) Values(represent filename)
w1 file1
w2 file1
w3 file2
w4 file1
w5 file2
w6 file1,file2
............
The list goes on and on but this is just an idea of how it looks like.As you can see, there's no duplicate for words and they are unique.
Given that I have this hashmap info,I need to find the intersection of the key and it's next key and return the results of the intersection. The idea looks like this:
w1∩w2= file1
w2∩w3= empty
w3∩w4= empty
........and it keeps going until it reaches the finishes the final pair of keys in the hashmap.
Since the pair of intersection results depends on the number of keys in the hashmap,I am guessing I'll need to use some loop to keep it iterating to return all the results.
Is there a way on how to get the intersection of each subsequent keys and also a way that is optimize regardless the size of the hashmap?
I appreciate any suggestion.
Make a variable that will hold all those intersections. In your loop retrieve 2 keys at a time. Compare each values of the 2 keys and if they are the same add the value to your intersection holder. Repeat the steps until there is no more pairs.
Here is the code.
Add this below your try/catch
LinkedHashmap<String, Set<String>> intersectionMap = new LinkedHashmap<>();
if (map.keySet() != null) {
String[] keys = map.keySet().toArray(new String[map.keySet().size()]);
for (int i = 0; i < keys.length - 1; i++) {
String key1 = keys[i];
String key2 = keys[i + 1];
TreeSet<String> interSection = intersection(map.get(key1), map.get(key2));
intersectionMap.put(key1 + "∩" + key2, interSection);
}
}
Add this helper method. This method will find the intersection of the two sets. This will be the key in solving your problem.
public static TreeSet<String> intersection(TreeSet<String> setA, TreeSet<String> setB) {
// An optimization to iterate over the smaller set
if (setA.size() > setB.size()) {
return intersection(setB, setA);
}
TreeSet<String> results = new TreeSet<>();
for (String element : setA) {
if (setB.contains(element)) {
results.add(element);
}
}
return results;
}
Yet another version with set operations:
Map<String>, Set<String>> intersections(Map<String, TreeSet<String>> map) {
Map<String>, Set<String>> result = new TreeMap<>();
List<String> words = new ArrayList<>(map.keySet());
words.sort();
for (int i = 0; i < words.size() - 1; ++i) {
String wordI = words.get(i);
Set<String> valueI = map.get(wordI);
for (int j = i + 1, j < words.size(); ++j) {
String wordJ = words.get(j);
Set<String> valueJ = map.get(wordJ);
String word = wordi + "∩" + words[j];
Set<String> value = new TreeSet<>(valueI);
value.retainAll(valueJ);
result.put(word, value);
}
}
return result;
}

Find the most common String in ArrayList()

Is there a way to find the most common String in an ArrayList?
ArrayList<String> list = new ArrayList<>();
list.add("test");
list.add("test");
list.add("hello");
list.add("test");
Should find the word "test" from this list ["test","test","hello","test"]
Don't reinvent the wheel and use the frequency method of the Collections class:
public static int frequency(Collection<?> c, Object o)
Returns the number of elements in the specified collection equal to
the specified object. More formally, returns the number of elements e
in the collection such that (o == null ? e == null : o.equals(e)).
If you need to count the occurrences for all elements, use a Map and loop cleverly :)
Or put your list in a Set and loop on each element of the set with the frequency method above. HTH
EDIT / Java 8: If you fancy a more functional, Java 8 one-liner solution with lambdas, try:
Map<String, Long> occurrences =
list.stream().collect(Collectors.groupingBy(w -> w, Collectors.counting()));
In statistics, this is called the "mode". A vanilla Java 8 solution looks like this:
Stream.of("test","test","hello","test")
.collect(Collectors.groupingBy(s -> s, Collectors.counting()))
.entrySet()
.stream()
.max(Comparator.comparing(Entry::getValue))
.ifPresent(System.out::println);
Which yields:
test=3
jOOλ is a library that supports mode() on streams. The following program:
System.out.println(
Seq.of("test","test","hello","test")
.mode()
);
Yields:
Optional[test]
(disclaimer: I work for the company behind jOOλ)
As per question, Specifically just to get word, not the number of times (i.e. value of key).
String mostRepeatedWord
= list.stream()
.collect(Collectors.groupingBy(w -> w, Collectors.counting()))
.entrySet()
.stream()
.max(Comparator.comparing(Entry::getValue))
.get()
.getKey();
You can make a HashMap<String,Integer>. If the String already appears in the map, increment its key by one, otherwise, add it to the map.
For example:
put("someValue", 1);
Then, assume it's "someValue" again, you can do:
put("someValue", get("someValue") + 1);
Since the key of "someValue" is 1, now when you put it, the key will be 2.
After that you can easily go through the map and extract the key that has the highest value.
I didn't write a full solution, try to construct one, if you have problems post it in another question. Best practice is to learn by yourself.
I think the best way to do it is using maps containing counts.
Map<String, Integer> stringsCount = new HashMap<>();
And iterate over your array filling this map:
for(String s: list)
{
Integer c = stringsCount.get(s);
if(c == null) c = new Integer(0);
c++;
stringsCount.put(s,c);
}
Finally, you can get the most repeated element iterating over the map:
Map.Entry<String,Integer> mostRepeated = null;
for(Map.Entry<String, Integer> e: stringsCount.entrySet())
{
if(mostRepeated == null || mostRepeated.getValue()<e.getValue())
mostRepeated = e;
}
And show the most common string:
if(mostRepeated != null)
System.out.println("Most common string: " + mostRepeated.getKey());
You could use a HashMap<String,Integer>. Looping through the array, you can check for each String if it is not already a Key of your HashMap, add it and set the value to 1, if it is, increase its value by 1.
Then you have a HashMap with all unique Strings and an associated number stating their amount in the array.
If somebody need to find most popular from usual String[] array (using Lists):
public String findPopular (String[] array) {
List<String> list = Arrays.asList(array);
Map<String, Integer> stringsCount = new HashMap<String, Integer>();
for(String string: list)
{
if (string.length() > 0) {
string = string.toLowerCase();
Integer count = stringsCount.get(string);
if(count == null) count = new Integer(0);
count++;
stringsCount.put(string,count);
}
}
Map.Entry<String,Integer> mostRepeated = null;
for(Map.Entry<String, Integer> e: stringsCount.entrySet())
{
if(mostRepeated == null || mostRepeated.getValue()<e.getValue())
mostRepeated = e;
}
try {
return mostRepeated.getKey();
} catch (NullPointerException e) {
System.out.println("Cannot find most popular value at the List. Maybe all strings are empty");
return "";
}
}
case non-sensitive
i know this takes more time to implement but you can use heap data structure by storing in the nodes the count and the string information
You can use Guava's Multiset:
ArrayList<String> names = ...
// count names
HashMultiset<String> namesCounts = HashMultiset.create(names);
Set<Multiset.Entry<String>> namesAndCounts = namesCounts.entrySet();
// find one most common
Multiset.Entry<String> maxNameByCount = Collections.max(namesAndCounts, Comparator.comparing(Multiset.Entry::getCount));
// pick all with the same number of occurrences
List<String> mostCommonNames = new ArrayList<>();
for (Multiset.Entry<String> nameAndCount : namesAndCounts) {
if (nameAndCount.getCount() == maxNameByCount.getCount()) {
mostCommonNames.add(nameAndCount.getElement());
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class StringChecker {
public static void main(String[] args) {
ArrayList<String> string;
string = new ArrayList<>(Arrays.asList("Mah", "Bob", "mah", "bat", "MAh", "BOb"));
Map<String, Integer> wordMap = new HashMap<String, Integer>();
for (String st : string) {
String input = st.toUpperCase();
if (wordMap.get(input) != null) {
Integer count = wordMap.get(input) + 1;
wordMap.put(input, count);
} else {
wordMap.put(input, 1);
}
}
System.out.println(wordMap);
Object maxEntry = Collections.max(wordMap.entrySet(), Map.Entry.comparingByValue()).getKey();
System.out.println("maxEntry = " + maxEntry);
}
With this method, if there is more than one most common elements in your ArrayList, you get back all of them by adding them to a new ArrayList.
public static void main(String[] args) {
List <String> words = new ArrayList<>() ;
words.add("cat") ;
words.add("dog") ;
words.add("egg") ;
words.add("chair") ;
words.add("chair") ;
words.add("chair") ;
words.add("dog") ;
words.add("dog") ;
Map<String,Integer> count = new HashMap<>() ;
for (String word : words) { /* Counts the quantity of each
element */
if (! count.containsKey(word)) {
count.put(word, 1 ) ;
}
else {
int value = count.get(word) ;
value++ ;
count.put(word, value) ;
}
}
List <String> mostCommons = new ArrayList<>() ; /* Max elements */
for ( Map.Entry<String,Integer> e : count.entrySet() ) {
if (e.getValue() == Collections.max(count.values() )){
/* The max value of count */
mostCommons.add(e.getKey()) ;
}
}
System.out.println(mostCommons);
}
}
There are a lot of answers suggesting HashMaps. I really don't like them, because you have to iterate through them once again anyway. Rather, I would sort the List
Collections.sort(list);
and then loop through it. Something similar to
String prev = null, mostCommon=null;
int num = 0, max = 0;
for (String str:list) {
if (str.equals(prev)) {
num++;
} else {
if (num>max) {
max = num;
mostCommon = str;
}
num = 1;
prev = str;
}
}
should do it.

Removing a key-value pair from HashMap is giving an error during for loop

I have a set of data stored in a HashMap. The elements in the data are compared and if the condition is satisfied, they are removed from the elements. However, I am using a for loop to iterate the elements and it is giving me a Java Null Pointer error.
Example of comparisons:
Item: 0-1
Item: 0-2
Item: 0-3
Item: 0-4
Item: 1-2
Item: 1-3
Item: 1-4
Item: 2-3
Item: 2-4
Item: 3-4
Condition: IF Item 0-1 > (1-2, 1-3 and 1-4): store value 0-1 in another array
then remove Item 0-1, 1-2, 1-3 and 1-4 from HahsMap list. ELSE continue to next set
Condition: IF Item 0-2 > (2-3 and 2-4): store value 0-2 in another array
then removed Item 0-2, 2-3 and 2-4 from HahsMap list. ELSE continue to next set.
import java.util.HashMap;
import java.util.Map;
public class TestHashMapLoop {
public static void main(String[] args)
{
Map<String, Integer> myMap = new HashMap<String, Integer>();
myMap.put("0-1", 33);
myMap.put("0-2", 29);
myMap.put("0-3", 14);
myMap.put("0-4", 8);
myMap.put("0-5", 18);
myMap.put("1-2", 41);
myMap.put("1-3", 15);
myMap.put("1-4", 17);
myMap.put("1-5", 28);
myMap.put("2-3", 1);
myMap.put("2-4", 16);
myMap.put("2-5", 81);
myMap.put("3-4", 12);
myMap.put("3-5", 11);
myMap.put("4-5", 21);
int myMapCount = 6;
for(int i = 0; i < myMapCount; i++)
{
for(int j = i+1; j < myMapCount; j++)
{
String indexKey = i+"-"+j;
for(int k = 0; k < myMapCount; k++)
{
String compareKey = j+"-"+k;
System.out.println("Index " + indexKey + " : " + compareKey);
if((myMap.get(indexKey)) > (myMap.get(compareKey)))
{
//Store value indexKey in another array (not shown here)
System.out.println("Index" + myMap.get(compareKey) + " is removed..");
myMap.remove(compareKey);
}
System.out.println("Index " + myMap.get(indexKey) + " is removed..");
myMap.remove(indexKey);
}
}
}
}
}
Can anyone advise on how to get the loop going even though the elements are removed or is there a better way to do this?
On the first iteration
indexKey = 0-1;
compareKey=1-0;
if((myMap.get(indexKey)) > (myMap.get(compareKey)))
your mymap.get("1-0") would return null
EDIT:
As Fildor said in the comments:
check if myMap.get(indexKey) and myMap.get(compareKey) are NUll
IF Null
Continue your innermost loop
else continue what ever you were doing .
Removing all the keys whose value is less than a particular key-value pair will be a trivial task.
Because you have to compare "0-1" with all those elements starting with "1-", and if you find that all the elements are less than "0-1", then only you will remove them.. So, you will have to iterate over your map, to remove them..
A better way would be to create another map, where you can put the element "0-1" after you find that it is greater..
I would rather use enhanced-for loop..
public class TestHashMapLoop {
public static void main(String[] args)
{
Map<String, Integer> myMap = new HashMap<String, Integer>();
Map<String, Integer> newMap = new HashMap<String, Integer>();
/** Initialize Map **/
boolean flag = true;
Set<String> keySet = myMap.keySet();
for (String key: keySet) {
flag = true;
for (String innerKey: keySet) {
if (innerKey.startsWith(String.valueOf(key.charAt(2)))) {
if (myMap.get(key) > myMap.get(innerKey)) {
continue;
} else {
flag = false;
break;
}
}
}
if (flag) {
newMap.put(key, myMap.get(key));
}
}
System.out.println(newMap);
}
}
But, this is also not a very good way. Notice that this way you are iterating over a map with n keys :- n * n times
You would rather want to find a better way than using a HashMap for what you want to do..

Categories