91

let say I have this code

Map<String, String> list = new HashMap<String, String>();
list.put("number1", "one");
list.put("number2", "two");

how can I make some "alias" the type

Map<String, String>

to something that easier to be rewritten like

// may be something like this
theNewType = HashMap<String, String>;

theNewType list = new theNewType();
list.put("number1", "one");
list.put("number2", "two");

basically my question is, how to create "alias" to some "type", so i can make it easier to write and easier when need to change the whole program code.

Thanks, and sorry if this is silly question. I'm kinda new in Java.

shabunc
  • 23,119
  • 19
  • 77
  • 102
Lee
  • 3,259
  • 4
  • 21
  • 27

5 Answers5

80

There are no aliases in Java. You can extend the HashMap class with your class like this:

public class TheNewType extends HashMap<String, String> {
    // default constructor
    public TheNewType() {
        super();
    }
    // you need to implement the other constructors if you need
}

But keep in mind that this will be a class it won't be the same as you type HashMap<String, String>

KARASZI István
  • 30,900
  • 8
  • 101
  • 128
  • 3
    @Lee - KARASZI István's caveat about types is important - see http://www.ibm.com/developerworks/java/library/j-jtp02216/index.html – McDowell Apr 09 '11 at 11:36
  • 1
    There is an alternative trick/hack you can do with generics: http://stackoverflow.com/questions/5604390/how-do-i-create-some-variable-type-alias-in-java/27536347#27536347 – Andrey Dec 17 '14 at 22:54
  • 2
    But because this is a specialization of a more general type, you can't use `TheNewType` in place of `HashMap` as much as you can't use the latter where a `Map` is returned. – Niccolò Mar 29 '19 at 14:44
  • archive of link posted by @McDowell https://web.archive.org/web/20170206190605/http://www.ibm.com/developerworks/java/library/j-jtp02216/index.html – plswork04 Jun 27 '23 at 15:53
18

There is no typedef equivalent in Java, and there is no common idiom for aliasing types. I suppose you could do something like

class StringMap extends HashMap<String, String> {}

but this is not common and would not be obvious to a program maintainer.

Boann
  • 48,794
  • 16
  • 117
  • 146
Rom1
  • 3,167
  • 2
  • 22
  • 39
  • 2
    Bear in mind that you should implement the missing details of the Serializable interface for your subclass, should you do this. Else, you may encounter curiosities if you ever run into a situation where you need to (de)serialize your object. – nasukkin Aug 30 '16 at 22:43
12

Although Java doesn't support this, you can use a generics trick to simulate it.

class Test<I extends Integer> {
    <L extends Long> void x(I i, L l) {
        System.out.println(
            i.intValue() + ", " + 
            l.longValue()
        );
    }
}

Source: http://blog.jooq.org/2014/11/03/10-things-you-didnt-know-about-java/

Andrey
  • 830
  • 9
  • 12
  • 57
    I'm not sure why exactly you hate OP and want him to be bashed to deadh by coworkers – shabunc Jun 13 '17 at 13:50
  • IMO this is the same idea as in the other answers, in that you use the concept of derivation to get a new type name. Here it is `I` from `Integer` and `L` from `Long` by constraining type variables, where the other answers propose `NewType` from `HashMap` by constructing a new class. The drawback of the type variable approach is that you can only use it within the scope of the type variables, e.g. the `Test` class or the `x` method. – ThomasH Oct 18 '19 at 15:59
4

The closest one could think of is to make a wrapper class like so

class NewType extends HashMap<String, String> {
     public NewType() { }
}

I really wish Java had a sound type aliasing feature.

Ingo
  • 36,037
  • 5
  • 53
  • 100
1

Nothing like that exists in Java. You might be able to do something with IDE templates or autocompletion, and look forward to (limited) generics type inference in Java 7.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720