get TreeMap - Java Collection Framework

Java examples for Collection Framework:TreeMap

Description

get TreeMap

Demo Code


//package com.java2s;
import java.util.Comparator;

import java.util.TreeMap;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getTreeMap());
    }//from ww  w  . j a v  a2 s .  c  om

    public static TreeMap<Object, Object> getTreeMap() {
        return new TreeMap<Object, Object>(getComparator());
    }

    private static Comparator<Object> getComparator() {
        return new Comparator<Object>() {
            @SuppressWarnings("unchecked")
            @Override
            public int compare(final Object o1, final Object o2) {
                final Comparable<Object> c1 = (Comparable<Object>) o1;
                final Comparable<Object> c2 = (Comparable<Object>) o2;
                if (o1.getClass().equals(o2.getClass())) {
                    return c1.compareTo(c2);
                } else {
                    return c1.getClass().getName()
                            .compareTo(c2.getClass().getName());
                }
            }
        };
    }
}

Related Tutorials