[Java] Generics classes and generics methods

Introduction

I've managed to use the generics that I'm familiar with in Lists and Maps, but I'll leave a few things to keep in mind as it can be vague when using generics for my own classes.

Verification environment

Source code for verification

Generics.java


package generics;

import java.util.ArrayList;
import java.util.List;

public class Generics {

	public static void main(String[] args) {
		Bean<String> beanForStr1 = new Bean<String>();
		beanForStr1.setValue("String");
		System.out.println(beanForStr1.getValue());

		Bean<Integer> beanForInt1 = new Bean<Integer>();
		beanForInt1.setValue(1000);
		System.out.println(beanForInt1.getValue());

		ExpandBean<Integer> beanForInt2 = new ExpandBean<Integer>();
		beanForInt2.setValue(1000);
		System.out.println(beanForInt2.getValue());

		System.out.println(getAnyBean(beanForStr1).get(0).getValue());
		System.out.println(getAnyBean(beanForInt1).get(0).getValue());
		System.out.println(getAnyBean(beanForInt2).get(0).getValue());
	}


	/**
	 *Generics method
	 */
	public static <T> List<T> getAnyBean(T args){

		List<T> list = new ArrayList<T>();
		list.add(args);

		return list;
	}

}


Bean.java


/**
 *Generics class
 */
public class Bean<T> {

	private T value;

	public T getValue() {
		return value;
	}

	public void setValue(T value) {
		this.value = value;
	}
}

ExpandBean.java


package generics;

/**
 *Generics class
 *You can specify (restrict) such as "some class that inherits the Number class"
 */
public class ExpandBean<T extends Number> {

	private T value;

	public T getValue() {
		return value;
	}

	public void setValue(T value) {
		this.value = value;
	}
}

Note

It is useful to remember to create the basic part and to organize common processes. The generics class comes sickly, but the point of writing the generics just before specifying the return value of the generics method does not come sickly.

See you again (^_^) Noshi

Recommended Posts

[Java] Generics classes and generics methods
Java generics (defines classes and methods)
Java abstract methods and classes
Java programming (classes and instances, main methods)
JAVA learning history abstract classes and methods
java (classes and instances)
Java methods and method overloads
java Generics T and? Difference
Ruby methods and classes (basic)
Java methods
How to call classes and methods
Studying Java 8 (StaticIF and Default methods)
Classes and instances Java for beginners
Functions and methods
Java and JavaScript
XXE and Java
Differences in writing Java, C # and Javascript classes
Check static and public behavior in Java methods
How to access Java Private methods and fields
About classes and instances
[Java] String comparison and && and ||
[Java] About anonymous classes
Frequently used Java generics
Ruby classes and instances
Java --Serialization and Deserialization
[Java] Arguments and parameters
timedatectl and Java TimeZone
[Java] Branch and repeat
List and happy classes
[Java] Variables and types
[Java] Overload and override
Java classes and instances to understand in the figure
Equivalence comparison of Java wrapper classes and primitive types
[Java10] Be careful of using var and generics together
Java programming (variables and data)
Java encryption and decryption PDF
Java and Iterator Part 1 External Iterator
rails path and url methods
Java if and switch statements
Check Java9 Interface private methods
Java class definition and instantiation
Apache Hadoop and Java 9 (Part 1)
[Java] About String and StringBuilder
[Java] HashCode and equals overrides
☾ Java / Iterative statement and iterative control statement
About classes and instances (evolution)
About pluck and ids methods
Consideration about classes and instances
[Java] Classes, constructors, static members
About Java class variables class methods
Define abstract methods in Java enum and write each behavior
Think about the differences between functions and methods (in Java)
Advantages and disadvantages of Java
java (conditional branching and repetition)
ClassCastException occurs when migrating from Java7 to Java8 ~ Generics and overload ~
About Java Packages and imports
About abstract classes in java
[Java] Upload images and base64
About Ruby classes and instances
Creating Ruby classes and instances
C # and Java Overrides Story