Yuhao
Yuhao

Reputation: 1610

Java class definition: what does "<>" mean?

Here is the class definition in java code:

public class WordCount {

 public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        StringTokenizer tokenizer = new StringTokenizer(line);
        while (tokenizer.hasMoreTokens()) {
            word.set(tokenizer.nextToken());
            context.write(word, one);
        }
    }
 } 

So what does this mean?

public static class Map extends Mapper<LongWritable, Text, Text, IntWritable>

Why we need a "<>" here?

Upvotes: 0

Views: 720

Answers (2)

This is called "generics". It allows you to add type parameters.

In this particular case, it means that Map is a Mapper for tuples of (LongWritable,Text,Text,IntWritable).

A simpler example: suppose you have a Set. It could be a Set of integers, a set of strings, of MyClass instances.... this is where you use generics.
By declaring that a variable is of type Set<Integer>, you specify that it is a set of Integers. If you'd just declare it as a Set, you would have to check that it only contained Integers yourself. By adding the type parameter <Integer>, the compiler can now do the type-checking.

Generics are defined here in the Java Language Specification.

Upvotes: 2

Juvanis
Juvanis

Reputation: 25950

Take a look at the declaration of your Mapper class. It is possibly something like that class Mapper<E, T, T, K> which declares 3 different generic types.

Upvotes: 0

Related Questions