Given a string, calculate the frequency of characters, output the array with the letter and frequency. (such as: for “abbcdc”, the output should be (a,1),(b,2),(c,2),(d,1))



import java.util.LinkedHashMap;
import java.util.Map;


public class StringUtils {

    public static void main(String[] args) {
        String str = "abbcdc";
        Map charFreqMap = new LinkedHashMap();
        char[] charArray = str.toCharArray();
        for (Character ch : charArray) {
            if (charFreqMap.get(ch) != null) {
                charFreqMap.put(ch, charFreqMap.get(ch) + 1);
            } else {
                charFreqMap.put(ch, 1);
            }
        }
        for (Map.Entry mapEntry : charFreqMap.entrySet()) {
            System.out.print("(" + mapEntry.getKey() + "," + mapEntry.getValue() + ")");
            System.out.print(",");
        }
    }
}

Advertisements

Equals and HashCode

Consider the below example,
It’s a simple java class with one String field “key”. Inside the Main() method, We have created a HashMap and use this class object as a key and store a string as value.

Then we try to retrieve the value from that HashMap with that object. As this class does not override the hashCode() method, so invoking the hashCode() method on this class object will produce a different hashCode. So we would not able to retrieve the value with that object.


import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.util.HashMap;
import java.util.Map;

public class Ckey {

    private String key;

    public Ckey(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }


    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("Ckey", key)
                .toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        Ckey keyMap = (Ckey) o;

        return new EqualsBuilder()
                .append(key, keyMap.key)
                .isEquals();
    }

      /*  @Override
        public int hashCode() {
            return new HashCodeBuilder(17, 37)
                    .append(key)
                    .toHashCode();
        }*/

    public static void main(String[] args) {
        Map hashMap = new HashMap();
        Ckey k1 = new Ckey("Key1");
        hashMap.put(k1, "key1");
        System.out.println(hashMap);
        //Try to retrive the value with the same Key. You will end with null.since the hashCode are not implemented.
        // So invoking the hashCode will produce a default code which is different for every call. So you will not get the value
        System.out.println(hashMap.get(new Ckey("Key1")));
    }
}


As per the contract
1) If two objects are equal, then they must have the same hash code.
2) If two objects have the same hash code, they may or may not be equal.

If you dont override the equals() method, then the compare operation will be failed. Hence it will return a Null value. So we should override both these methods to avoid these kind of issues

Reverse a String using recursion


public class StringUtils {

    public static void main(String[] args) {
        String input = "reverse";
        System.out.println("Input String is " + input);
        String reversed = reverse(input);
        System.out.println("Reversed String is " + reversed);
    }

    private static String reverse(String input) {

        if (input.length() == 1) {
            return input;
        } else {
            return reverse(input.substring(1, input.length())) + input.charAt(0);
        }
    }
}