当前位置: 动力学知识库 > 问答 > 编程问答 >

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?

网友答案:

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.

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: