Can I add new methods to the String class in Java?

后端 未结 15 2330
再見小時候
再見小時候 2020-11-27 06:36

I\'d like to add a method AddDefaultNamespace() to the String class in Java so that I can type \"myString\".AddDefaultNamespace() instead of

15条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 07:05

    People searching with keywords "add method to built in class" might end up here. If you're looking to add method to a non final class such as HashMap, you can do something like this.

    public class ObjectMap extends HashMap {
    
        public Map map;
    
        public ObjectMap(Map map){
            this.map = map;
        }
    
        public int getInt(String K) {
            return Integer.valueOf(map.get(K).toString());
        }
    
        public String getString(String K) {
            return String.valueOf(map.get(K));
        }
    
        public boolean getBoolean(String K) {
            return Boolean.valueOf(map.get(K).toString());
        }
    
        @SuppressWarnings("unchecked")
        public List getListOfStrings(String K) {
            return (List) map.get(K);
        }
    
        @SuppressWarnings("unchecked")
        public List getListOfIntegers(String K) {
            return (List) map.get(K);
        }
    
        @SuppressWarnings("unchecked")
        public List> getListOfMapString(String K) {
            return (List>) map.get(K);
        }
    
        @SuppressWarnings("unchecked")
        public List> getListOfMapObject(String K) {
            return (List>) map.get(K);
        }
    
        @SuppressWarnings("unchecked")
        public Map getMapOfObjects(String K) {
            return (Map) map.get(K);
        }
    
        @SuppressWarnings("unchecked")
        public Map getMapOfStrings(String K) {
            return (Map) map.get(K);
        }
    }
    

    Now define a new Instance of this class as:

    ObjectMap objectMap = new ObjectMap(new HashMap();
    

    Now you can access all the method of the built-in Map class, and also the newly implemented methods.

    objectMap.getInt("KEY");
    

    EDIT:

    In the above code, for accessing the built-in methods of map class, you'd have to use

    objectMap.map.get("KEY");
    

    Here's an even better solution:

    public class ObjectMap extends HashMap {
    
        public ObjectMap() {
    
        }
    
        public ObjectMap(Map map){
            this.putAll(map);
        }
    
        public int getInt(String K) {
            return Integer.valueOf(this.get(K).toString());
        }
    
        public String getString(String K) {
            return String.valueOf(this.get(K));
        }
    
        public boolean getBoolean(String K) {
            return Boolean.valueOf(this.get(K).toString());
        }
    
        @SuppressWarnings("unchecked")
        public List getListOfStrings(String K) {
            return (List) this.get(K);
        }
    
        @SuppressWarnings("unchecked")
        public List getListOfIntegers(String K) {
            return (List) this.get(K);
        }
    
        @SuppressWarnings("unchecked")
        public List> getListOfMapString(String K) {
            return (List>) this.get(K);
        }
    
        @SuppressWarnings("unchecked")
        public List> getListOfMapObject(String K) {
            return (List>) this.get(K);
        }
    
        @SuppressWarnings("unchecked")
        public Map getMapOfObjects(String K) {
            return (Map) this.get(K);
        }
    
        @SuppressWarnings("unchecked")
        public Map getMapOfStrings(String K) {
            return (Map) this.get(K);
        }
    
        @SuppressWarnings("unchecked")
        public boolean getBooleanForInt(String K) {
            return Integer.valueOf(this.get(K).toString()) == 1 ? true : false;
        }
    }
    

    Now you don't have to call

    objectMap.map.get("KEY");
    

    simply call

    objectMap.get("KEY");
    

提交回复
热议问题