Java HashMap

0
User Management System
Learn Kotlin, Retrofit, MVVM and MySQL using this all-in-one app. It is designed to be beginner friendly.

Java Hashmap Tutorial and Examples.

Quick Java HashMap Examples and HowTos

1. Putting Mime Types in a Hashmap
public class ContentTypeUtil {

    private static final Map<String, String> mimeMap;

    static {
        mimeMap=new HashMap<String, String>();
        mimeMap.put("jpg","image/jpeg");
        mimeMap.put("gif","image/gif");
        mimeMap.put("bmp","image/bmp");
        mimeMap.put("png","image/png");

    }

}

You can also put extensions in a HashMap:

private static final Map<String, String> extMap;

    static {
        extMap=new HashMap<String, String>();

        extMap.put("image/jpeg", ".jpg");
        extMap.put("image/gif", ".gif");
        extMap.put("image/bmp", ".bmp");
        extMap.put("image/png", ".png");
        extMap.put("image/cis-cod", ".cod");
        extMap.put("image/gif", ".gif");
        extMap.put("image/ief", ".ief");

        extMap.put("image/pipeg", ".jfif");
        extMap.put("image/svg+xml", ".svg");
        extMap.put("image/tiff", ".tif");
        extMap.put("image/x-cmu-raster", ".ras");
        extMap.put("image/x-cmx", ".cmx");
        extMap.put("image/x-icon", ".ico");
        extMap.put("image/x-portable-anymap", ".pnm");
        extMap.put("image/x-portable-bitmap", ".pbmv");
        extMap.put("image/x-portable-graymap", ".pgm");
        extMap.put("image/x-portable-pixmap", ".ppm");
        extMap.put("image/x-rgb", ".rgb");
        extMap.put("image/x-xbitmap", ".xbm");
        extMap.put("image/x-xpixmap", ".xpm");
        extMap.put("image/x-xwindowdump", ".xwd");

        extMap.put("video/mpeg",".mpeg");
        extMap.put("video/quicktime",".mov");
        extMap.put("video/x-la-asf",".lsf");
        extMap.put("video/x-la-asf",".lsx");
        extMap.put("video/x-ms-asf",".asf");
        extMap.put("video/x-ms-asf",".asr");
        extMap.put("video/x-ms-asf",".asx");
        extMap.put("video/x-msvideo",".avi");
        extMap.put("video/x-sgi-movie",".movie");
        extMap.put("video/mp4",".mp4");

    }
2. Search HashMap keys

For example we can search the media file extensions that we had populated into our HashMap above.

To search a HashMap you use the containsKey() method. Once we've found the key we use it to get the value which is the actual extension.

    public static String getExt(String contentType) {
        if(contentType==null){
            return "";
        }
        contentType=contentType.toLowerCase().trim().replaceAll(" +", " ");
        if(!extMap.containsKey(contentType)){
            return "";
        }

        String ext = extMap.get(contentType);
        return ext;

    }
Android MySQL Retrofit2 Multipart CRUD,Search,Pagination rating

When I was a 2nd year Software Engineering student, I buillt a now defunct online tool called Camposha(from Campus Share) using my then favorite language C#(ASP.NET) to compete OLX in my country(Kenya). The idea was to target campus students in Kenya. I got a few hundred signups but competing OLX proved too daunting. I decided to focus on my studies, learning other languages like Java,Python,Kotlin etc while meanwhile publishing tutorials at my YouTube Channel ProgrammingWizards TV which led to this site(camposha.info). Say hello or post me a suggestion: oclemmi@gmail.com . Follow me below; Github , and on my channel: ProgrammingWizards TV

We will be happy to hear your thoughts

Leave a reply

forty five − thirty nine =

Reset Password
Compare items
  • Total (0)
Compare
0
Shopping cart