Vijay Wilson
Vijay Wilson

Reputation: 516

Java generics holding same type of parameters more than once

I'm learning hadoop map-reduce algorithm, I'm new to java generics concepts, I can able to understand what generics can do(typecasting) for the objects created using a reference type. But could not understand why same parameter type is declared more than once inside <> braces.

Below is a reducer class definition which implements Reducer interface with type declaration as < Text, IntWritable, Text, IntWritable >. Why cant it be as < Text, IntWritable > alone ?

public static class T_Reduce extends MapReduceBase implements 
Reducer< Text, IntWritable, Text, IntWritable > 
{  

   public void reduce( Text key, Iterator <IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException 
     { 
        ......
        ......
     } 
} 

I have google it a lot and could not find a simple proper answer.

Upvotes: 0

Views: 81

Answers (4)

Lew Bloch
Lew Bloch

Reputation: 3433

Reducer<Text, IntWritable, Text, IntWritable> is a specialization of Reducer<K2, V2, K3, V3>. The interface does not require that K2 and K3 be the same type. So it cannot reuse the parameter for the key type. Therefore specializations​ cannot, either.

Upvotes: 1

markspace
markspace

Reputation: 11030

Have you checked the declaration for Reducer? My guess is it is declared as Reducer<T,U,V,W> so you have to parameterize all of the types, even if some of them are the same.

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201497

The JavaDoc for Reducer fully specifies the signature of the type like

org.apache.hadoop.mapreduce
     Class Reducer<KEYIN,VALUEIN,KEYOUT,VALUEOUT>

Which indicates that the first two parameters are the key and value types for the input to the reduce function - while the second pair are the output types. Here they are the same, but they are not required to be for every possible use case. For example, you might be producing a double from computation on int.

Upvotes: 3

J.N.
J.N.

Reputation: 536

Because the Reducer can take four different types. In your case, it doesn't, but the functionality is still there, and the compiler would have not idea what the last two types were with something like:

Reducer<Text, IntWritable, ?, ?>

Upvotes: 1

Related Questions