Replacing HashMap with TreeMap

时间:2016-04-12 00:37:30

标签: dictionary hashmap treemap

I am trying to replace HashMap used in my code to TreeMap as I need to have the keys sorted.

But just replacing the HashMap declaration with TreeMap is running into various ClassCast exception which were not arising before. I am debugging the code but I cannot follow why is it failing? It fails in the treemap.put(key, value); statement.

private static void insertIntoIndexFile(String datatype, String value, String columnName, String tableName,
            long offset) {
        // TODO Auto-generated method stub
        Map<Object, ArrayList<Long>> index = new TreeMap<Object, ArrayList<Long>>();
        Map<Object, ArrayList<Long>> result = new TreeMap<Object, ArrayList<Long>>();
        try{
            String indexTableFileName = SCHEMA+"."+tableName+"."+columnName+".ndx";
            File indexTableFileObject = new File(indexTableFileName);
            long indexfileLength = indexTableFileObject.length();
            RandomAccessFile indexTableFile = new RandomAccessFile(indexTableFileObject, "rw");
            boolean isValuePresent = false;

            if(indexfileLength>0){

                // returns sucessfully
                index = getIndexFileEntries(indexTableFileName, datatype);

                // checking if the key exists
                Set set1 = index.entrySet();
                Iterator iterator1 = set1.iterator();
                while(iterator1.hasNext()) {
                    Map.Entry me2 = (Map.Entry)iterator1.next();
                    Object key = me2.getKey();
                    ArrayList<Long> temp = new ArrayList<Long>();
                    if(key.toString().equals(value)){
                        System.out.println("Comparing the hashmap value to value " + key.toString());
                        isValuePresent = true;
                        temp = (ArrayList<Long>) me2.getValue();
                        long frequency = temp.get(0);
                        frequency++;
                        temp.set(0, frequency);
                        temp.add(offset);
                        index.put(key, temp);
                        break;
                    }
                }

                if(isValuePresent == false){
                    System.out.println("The file has values but this key was not found. Here is the updated hashmap");
                    ArrayList<Long> temp = new ArrayList<Long>();
                    temp.add(Long.parseLong("01"));
                    temp.add(offset);
                    Object key = (Object)value; 
                    index.put(key,temp); // this line throws error

                    writeMapToIndexFile(tableName, columnName, datatype, index);


                }else{
                    writeMapToIndexFile(tableName,columnName, datatype, index);
                }
        }catch(Exception e){
            e.printStackTrace();
        }

    }

Here is the stacktrace :

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
    at java.lang.String.compareTo(Unknown Source)
    at java.util.TreeMap.put(Unknown Source)
    at Database.insertIntoIndexFile(Database.java:433)
    at Database.insertIntoTable(Database.java:369)
    at Database.main(Database.java:1340)

Similar stacktrace for each of short, integer etc. I expect the key to be these datatypes. Therefore I have used Object type to store the keys. But I am not trying to cast them String before inserting into the map.

Is there anything that I am overlooking while replacing a hashmap with treemap. I agree its a very broad question but any help would really be great.

Thanks for your time.

0 个答案:

没有答案