Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Multiple Type Parameters

I asked google what is Mutiple Generics but still don't understand so I help. I think that it not about like Haskell Language where it print out tuples ex. (12,"hello") right?

I think it's something to deal with constructor? Can you give me a good code example and explain me step by step about Multiple Generics?

1 Answer

Similar to Generics, except that you can provide a key and a value type. Here's an example from http://www.java2s.com/Tutorial/Java/0200__Generics/MultipleTypeParameters.htm, except for the values in the Pair object:

class Pair<KeyType, ValueType> {

  private KeyType key;
  private ValueType value;

  // Constructor
  public Pair(KeyType key, ValueType value) {
    this.key = key;
    this.value = value;
  }

  // Get the key for this pair
  public KeyType getKey() {
    return key;
  }

  // Get the value for this pair
  public ValueType getValue() {
    return value;
  }

  // Set the value for this pair
  public void setValue(ValueType value) {
    this.value = value;
  }

}

public class MulipleTypeParameters {

  public static void main(String[] a) {

    //create Pair object with Integer key and String value
    Pair<Integer, String> p = new Pair<Integer, String>(101, "Treehouse MultipleTypeParameters");

    System.out.println(p.getKey());
    System.out.println(p.getValue());
    p.setValue("Now is the time for all good men to learn Java");
    System.out.println(p.getValue());
  }

}

But what is it useful for? Is it something to do with Constructor? What's the point with generics?..