[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] Personal summary of classes and methods (basic)
(Note) Java classes / variables / methods
Java methods and method overloads
java Generics T and? Difference
[Ruby] Singular methods and singular classes
Ruby methods and classes (basic)
[Ruby] Singular methods and singular classes
Java methods
Java methods
[Java] Generics
About Java static and non-static methods
How to call classes and methods
Studying Java 8 (StaticIF and Default methods)
Classes and instances Java for beginners
Classes and instances
Java class methods
Java Generics Summary
[Java] Generics sample
Java Generics (Notes)
Functions and methods
Java and JavaScript
XXE and Java
[Java ~ Classes and External Libraries ~] Study Memo (6)
Effective Java 3rd Edition Chapter 4 Classes and Interfaces
Differences in writing Java, C # and Javascript classes
Kantai Collection Java # 1 Classes and Objects [For Beginners]
Check static and public behavior in Java methods
How to access Java Private methods and fields
[Ruby] Classes and instances
Getters and setters (Java)
[Java] Thread and Runnable
HashMap and HashSet classes
Java true and false
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
Ruby variables and methods
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
Study Java # 2 (\ mark and operator)
[For beginners] Explanation of classes, instances, and statics in Java
Java version 8 and later features
Coding methods and refactoring principles
How to separate words in names in classes, methods, and variables
[Java] Difference between == and equals