Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.HashMap;

public class Main {
    /**
     * A static HashMap creator from a formatted string, 
     * the HashMap is specially used for the index mapper,
     * with the string(data of the node), and the integer
     * (index key of the node). 
     * @param str {k1=v1, k2=v2, ...}
     * @return
     */
    public static HashMap<Integer, String> StringToHashMap(String str) {
        HashMap<Integer, String> keyMap = new HashMap<Integer, String>();
        String[] entry = removeBracketPair(str).split(", ");
        for (String value : entry) {
            String[] kv = value.split("=");
            keyMap.put(Integer.parseInt(kv[0]), kv[1]);
        }
        return keyMap;
    }

    private static String removeBracketPair(String value) {
        String str = value.trim();
        // Assume existing both brackets
        return str.substring(1, str.length() - 1);
    }
}