Using generics in java besides creating safe type collections?

I mainly used generic files to create safe types. What are the other uses of generics?

+4
source share
8 answers

To allow the use of custom classes with your code.

Suppose you have released an SDK that allows you to use some special functionality. Generics will allow developers to use your features in many places with almost any class.

Assign generics to reduce code repetition.

+2
source

I used it to write a very thin layer of DAO:

/* superclass for all entites, a requirement is that they have the same kind of primary key */ public abstract class Entity { } /* superclass for all DAOs. contains all CRUD operations and anything else can be generalized */ public abstract class DAO<T extends Entity> { //assuming full control over the database, all entities have Integer pks. public T find(Integer pk) {...} public void save(T entity) {...} public void remove(T entity) {...} etc... } /* Complete example of an ideal DAO, assuming that there are no special operations specific for the Entity. Note the absence of any implementation at all. */ public class SpecificDAO extends DAO<SpecificEntity> {} 
+5
source

Another interesting use is to represent types as variables in Java reflection frames .

In particular, note that java.lang.Class has a type parameter indicating which class represents the Class object.

See Wikipedia for general information.

+2
source

There are other uses, such as general classes and general methods.

+1
source

Generics uses:

  • Compilation type security.
  • Avoid casting (hence avoid a ClassCastException ).
  • Readable code.

See the Java Generics tutorial for more details.

0
source

Enumerations are another example from the JDK. Basically, when you have two classes working together, but don’t know exactly which classes, generics become interesting.

0
source

Generic is used to create a generic class, so when you create a class, you can make it a parameterized class. Later, when its object is created, you can specify which data type or classtype type will display the class parameters. In addition, it can make the cast safe class, so only elements of one data type or class can be inserted. This method is mainly useful in data structure classes.

0
source

Generics allows parameters to be parameters Generics allows you to define types (classes and interfaces) as parameters when defining classes, interfaces, and methods. This is similar to the familiar formal parameters used in method declarations. The difference is that the inputs for formal parameters are values, and the inputs for type parameters are types. Example below: Using a formalized parameterized method and generics: -

 class Room { private Object object; public void add(Object object) { this.object = object; } public Object get() { return object; } } public class Main { public static void main(String[] args) { Room room = new Room(); room.add(60); //room.add("60"); //this will cause a run-time error Integer i = (Integer)room.get(); System.out.println(i); } } 

General version version: -

 class Room<T> { private T t; public void add(T t) { this.t = t; } public T get() { return t; } } public class Main { public static void main(String[] args) { Room<Integer> room = new Room<Integer>(); room.add(60); Integer i = room.get(); System.out.println(i); } } 

In the brilliant version of the class, if someone adds room.add("60") , a compile-time error will be shown.

source 1.Info 2. Example from

0
source

All Articles