Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java generics type

Java Generic Type Naming convention helps us understanding code easily and having a naming convention is one of the best practices of Java programming language. So generics also comes with its own naming conventions. Usually, type parameter names are single, uppercase letters to make it easily distinguishable from java variables. The most commonly used type parameter names are:

E – Element (used extensively by the Java Collections Framework, for example ArrayList, Set etc.)
K – Key (Used in Map)
N – Number
T – Type
V – Value (Used in Map)
S,U,V etc. – 2nd, 3rd, 4th types
Comment

java generics

// generic methods

public <T> List<T> fromArrayToList(T[] a) {   
	    return Arrays.stream(a).collect(Collectors.toList());
	}

public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
	    return Arrays.stream(a)
	      .map(mapperFunction)
	      .collect(Collectors.toList());
	}

// bounded generics

public <T extends Number> List<T> fromArrayToList(T[] a) {
	    ...
	}

//multiple bounds

<T extends Number & Comparable>

// upper bound wildcards

public static void paintAllBuildings(List<? extends Building> buildings) {
	    ...
	}
    
// lower bound wildcard

<? super T>
Comment

java generics

public class Tuple <T> {
  // the T is a placeholder for any datatype
  public T leftValue;
  public T rightValue;
  
  public Tuple(T leftValue, T rightValue){
    // again, T is being used as a placeholder for any type
    this.leftValue = leftValue;
    this.rightValue = rightValue;
}

public class Program{
  public static void main (String args){
    // And upon using Tuples we can fill in the T from the Tuple class with actual datatypes
    Tuple <int> intTuple = new Tuple <int>(5, 500)
    Tuple <String> stringTuple = new Tuple <String> ("Hello", "World")

    // we can even put Tuples inside of Tuples!
    Tuple<Tuple<int>> metaIntTuple = new Tuple <Tuple <int>> (intTuple, new Tuple <int> (456, 0));
  }
}
Comment

generics in java

public <T> List<T> fromArrayToList(T[] a) {   
    return Arrays.stream(a).collect(Collectors.toList());
}
Comment

PREVIOUS NEXT
Code Example
Java :: localdate to string java 
Java :: how to create a java library in intellij 
Java :: Create JDBC connection using properties file 
Java :: maven skip make-assembly 
Java :: how to call super onbackpressed in fragment 
Java :: overload and override in java 
Java :: variable is multiple of 3 
Java :: java Math.sqrt(double) 
Java :: object creation in java 
Java :: how to spilt the string with comma in jaav 
Java :: how to return the first character in an array from a method java 
Java :: springbootservletinitializer maven dependency 
Java :: make a textarea not editable javafx 
Java :: thread in java 
Java :: Print Text Using Java 
Java :: java array merge 
Java :: android select video samsung stackoverflow 
Java :: how to get the memory location of an object in java 
Java :: how to make jenkins pipeline choose specific java version 
Java :: python discord embed generator 
Java :: javax.persistence.noresultexception: no entity found for query 
Java :: array copy java 
Java :: horizontalAlignment center jlabel 
Java :: how to upload image from android app to server 
Java :: for each loop in java 
Java :: java string replace 
Java :: replace everything before a character in java 
Java :: call by value and call by reference in java 
Java :: java final variable 
Java :: lambda expression java 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =