Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2238092

Browse files
author
rfadatare
committed
Java Generics Examples
0 parents commit 2238092

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+703
-0
lines changed

‎pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.javaguides.generics</groupId>
4+
<artifactId>java-generics-guide</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.javaguides.generics.classes;
2+
3+
public class GenType<T> {
4+
private T t;
5+
6+
public T get() {
7+
return this.t;
8+
}
9+
10+
public void set(T t1) {
11+
this.t = t1;
12+
}
13+
14+
public static void main(String args[]) {
15+
16+
// Generic type with Integer Type
17+
GenType<Integer> type = new GenType<>();
18+
type.set(10); // valid
19+
System.out.println(type.get());
20+
21+
// Generic type with String Type
22+
GenType<String> typeStr = new GenType<>();
23+
typeStr.set("String");
24+
System.out.println(typeStr.get());
25+
26+
// Generic type with Custom Student Type
27+
GenType<Student> genericType = new GenType<>();
28+
Student student = new Student();
29+
student.setName("Ramesh");
30+
genericType.set(student);
31+
System.out.println(genericType.get());
32+
33+
34+
@SuppressWarnings("rawtypes")
35+
GenType type1 = new GenType(); // raw type
36+
type1.set("Ramesh"); // valid
37+
type1.set(10); // valid and autoboxing support
38+
System.out.println("Raw Type :" + type1.get());
39+
}
40+
}
41+
42+
class Student{
43+
private String name;
44+
45+
public String getName() {
46+
return name;
47+
}
48+
49+
public void setName(String name) {
50+
this.name = name;
51+
}
52+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.javaguides.generics.classes;
2+
3+
public class GenericClass<T extends A> {
4+
private T t;
5+
6+
public GenericClass(T t){
7+
this.t = t;
8+
}
9+
10+
public void doRunTest() {
11+
this.t.displayClass();
12+
}
13+
14+
public static void main(String[] args) {
15+
16+
// Creating object of sub class C and
17+
// passing it to Bound as a type parameter.
18+
GenericClass<C> bec = new GenericClass<C>(new C());
19+
bec.doRunTest();
20+
21+
// Creating object of sub class B and
22+
// passing it to Bound as a type parameter.
23+
GenericClass<B> beb = new GenericClass<B>(new B());
24+
beb.doRunTest();
25+
26+
// similarly passing super class A
27+
GenericClass<A> bea = new GenericClass<A>(new A());
28+
bea.doRunTest();
29+
30+
}
31+
}
32+
33+
34+
class A {
35+
public void displayClass() {
36+
System.out.println("Inside super class A");
37+
}
38+
}
39+
40+
class B extends A {
41+
public void displayClass() {
42+
System.out.println("Inside sub class B");
43+
}
44+
}
45+
46+
class C extends A {
47+
public void displayClass() {
48+
System.out.println("Inside sub class C");
49+
}
50+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.javaguides.generics.classes;
2+
3+
/**
4+
* Generic class example. Create GenericFactory<T> to get any type of instance.
5+
* @author javaguides.net
6+
*
7+
*/
8+
public class GenericClassExample {
9+
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
10+
GenericFactory<ProductA> factory = new GenericFactory<ProductA>(ProductA.class);
11+
ProductA productA = factory.createInstance();
12+
System.out.println(productA.getProductName());
13+
14+
GenericFactory<ProductB> factoryB = new GenericFactory<ProductB>(ProductB.class);
15+
ProductB productB = factoryB.createInstance();
16+
System.out.println(productB.getProductName());
17+
18+
GenericFactory<ProductC> factoryC = new GenericFactory<ProductC>(ProductC.class);
19+
ProductC productC = factoryC.createInstance();
20+
System.out.println(productC.getProductName());
21+
}
22+
}
23+
24+
class ProductA {
25+
public String getProductName() {
26+
return "Product A";
27+
}
28+
}
29+
30+
class ProductB {
31+
public String getProductName() {
32+
return "Product B";
33+
}
34+
}
35+
36+
class ProductC {
37+
public String getProductName() {
38+
return "Product C";
39+
}
40+
}
41+
42+
class GenericFactory<T> {
43+
44+
Class theClass = null;
45+
46+
public GenericFactory(Class theClass) {
47+
this.theClass = theClass;
48+
}
49+
50+
public T createInstance() throws IllegalAccessException, InstantiationException {
51+
return (T) this.theClass.newInstance();
52+
}
53+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.javaguides.generics.classes;
2+
3+
/**
4+
* Generic interface example to find min and max of array.
5+
* @author javaguides.net
6+
*
7+
*/
8+
public class GenericInterfaceExample {
9+
public static void main(String args[]) {
10+
Integer intOfArray[] = { 3, 6, 2, 8, 6 };
11+
Character charOfArray[] = { 'A', 'r', 'V', 'w' };
12+
String strOfArray[] = {"abc", "xyz", "pqr"};
13+
14+
MinMaxImpl<Integer> intMinMax = new MinMaxImpl<Integer>(intOfArray);
15+
MinMaxImpl<Character> charMinMax = new MinMaxImpl<Character>(charOfArray);
16+
MinMaxImpl<String> strMinMax = new MinMaxImpl<String>(strOfArray);
17+
18+
System.out.println("Max value in intOfArray: " + intMinMax.max());
19+
System.out.println("Min value in intOfArray: " + intMinMax.min());
20+
21+
System.out.println("Max value in charOfArray: " + charMinMax.max());
22+
System.out.println("Min value in charOfArray: " + charMinMax.min());
23+
24+
System.out.println("Max value in strOfArray: " + strMinMax.max());
25+
System.out.println("Min value in strOfArray: " + strMinMax.min());
26+
}
27+
}
28+
29+
interface MinMax<T extends Comparable<T>> {
30+
T min();
31+
32+
T max();
33+
}
34+
35+
class MinMaxImpl<T extends Comparable<T>> implements MinMax<T> {
36+
T[] vals;
37+
38+
MinMaxImpl(T[] o) {
39+
vals = o;
40+
}
41+
42+
public T min() {
43+
T v = vals[0];
44+
45+
for (int i = 1; i < vals.length; i++) {
46+
if (vals[i].compareTo(v) < 0) {
47+
v = vals[i];
48+
}
49+
}
50+
51+
return v;
52+
}
53+
54+
public T max() {
55+
T v = vals[0];
56+
57+
for (int i = 1; i < vals.length; i++) {
58+
if (vals[i].compareTo(v) > 0) {
59+
v = vals[i];
60+
}
61+
}
62+
63+
return v;
64+
}
65+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.javaguides.generics.classes;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.function.Function;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* Generic methods example to convert array to list.
10+
* @author javaguides.net
11+
*
12+
*/
13+
public class GenericMethodsExamples {
14+
// definition of a generic method
15+
public static <T> List<T> fromArrayToList(T[] a) {
16+
return Arrays.stream(a).collect(Collectors.toList());
17+
}
18+
19+
// definition of a generic method
20+
public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
21+
return Arrays.stream(a).map(mapperFunction).collect(Collectors.toList());
22+
}
23+
24+
// example of a generic method that has Number as an upper bound for T
25+
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
26+
return Arrays.stream(a).collect(Collectors.toList());
27+
}
28+
29+
public static void main(String[] args) {
30+
// testing the generic method with Integer
31+
Integer[] intArray = { 1, 2, 3, 4, 5 };
32+
List<Integer> list = fromArrayToList(intArray);
33+
list.forEach(element -> System.out.println(element));
34+
35+
// testing the generic method with String
36+
String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" };
37+
List<String> strList = fromArrayToList(stringArray);
38+
strList.forEach(element -> System.out.println(element));
39+
40+
// testing the generic method with Integer and String type
41+
Integer[] intArr = { 1, 2, 3, 4, 5 };
42+
List<String> stringList = fromArrayToList(intArr, Object::toString);
43+
stringList.forEach(element -> System.out.println(element));
44+
45+
}
46+
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.javaguides.generics.classes;
2+
3+
public class GenericMultipleTypeParametersExample {
4+
public static void main(String[] args) {
5+
OrderedPair<String, Integer> p1 = new OrderedPair<>("Even", 8);
6+
System.out.println(p1.getKey());
7+
System.out.println(p1.getValue());
8+
9+
OrderedPair<String, String> p2 = new OrderedPair<>("hello", "world");
10+
System.out.println(p2.getKey());
11+
System.out.println(p2.getValue());
12+
13+
OrderedPair<String, Employee> p3 = new OrderedPair<>("key", new Employee("Ramesh"));
14+
System.out.println(p3.getKey());
15+
System.out.println(p3.getValue().getName());
16+
}
17+
}
18+
19+
class Employee{
20+
private String name;
21+
Employee(String name){
22+
this.name = name;
23+
}
24+
25+
public String getName(){
26+
return this.name;
27+
}
28+
}
29+
30+
interface Pair<K, V> {
31+
public K getKey();
32+
public V getValue();
33+
}
34+
35+
class OrderedPair<K, V> implements Pair<K, V> {
36+
37+
private K key;
38+
private V value;
39+
40+
public OrderedPair(K key, V value) {
41+
this.key = key;
42+
this.value = value;
43+
}
44+
45+
public K getKey() { return key; }
46+
public V getValue() { return value; }
47+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /