Skip to content

Commit 350c4ec

Browse files
author
Bhuwan Gautam
committed
added first time
1 parent 7d663bc commit 350c4ec

File tree

94 files changed

+3208
-0
lines changed

Some content is hidden

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

94 files changed

+3208
-0
lines changed

file.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.covariant;
5+
6+
/**
7+
* @author bhuwan
8+
*/
9+
public class AfricaZoo extends Zoo {
10+
11+
@Override
12+
Lion getWildAnimal() {
13+
return new Lion();
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.covariant;
5+
6+
/**
7+
* @author bhuwan
8+
*/
9+
public class AsianZoo extends Zoo {
10+
11+
@Override
12+
Tiger getWildAnimal() {
13+
return new Tiger();
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.covariant;
5+
6+
/**
7+
* @author bhuwan
8+
*/
9+
public class Covariant {
10+
11+
public static void main(String[] args) {
12+
AsianZoo asianZoo = new AsianZoo();
13+
System.out.println("Asian Zoo:" + asianZoo.getWildAnimal().whoAreYou());
14+
AfricaZoo africaZoo = new AfricaZoo();
15+
System.out.println("Africa Zoo:" + africaZoo.getWildAnimal().whoAreYou());
16+
}
17+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.covariant;
5+
6+
/**
7+
* @author bhuwan
8+
*
9+
*/
10+
public class Lion extends WildAnimal{
11+
12+
public String whoAreYou(){
13+
return "Lion";
14+
}
15+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.covariant;
5+
6+
/**
7+
* @author bhuwan
8+
*
9+
*/
10+
public class Tiger extends WildAnimal{
11+
12+
public String whoAreYou(){
13+
return "Tiger";
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.covariant;
5+
6+
/**
7+
* @author bhuwan
8+
*
9+
*/
10+
public class WildAnimal {
11+
12+
public String willYouBite(){
13+
return "Yes";
14+
}
15+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.covariant;
5+
6+
/**
7+
* @author bhuwan
8+
*
9+
*/
10+
public class Zoo {
11+
12+
WildAnimal getWildAnimal(){
13+
return new WildAnimal();
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.generics;
5+
6+
/**
7+
* @author bhuwan
8+
*/
9+
public class BoundedType {
10+
11+
/**
12+
* Bounded type example
13+
*
14+
* @author bhuwan
15+
* @param t1
16+
* @param t2
17+
* @return 0, 1, or -1
18+
*/
19+
public static <T extends Comparable<T>> int compare(T t1, T t2) {
20+
return t1.compareTo(t2);
21+
}
22+
23+
@SuppressWarnings("unused")
24+
public static void main(String[] args) {
25+
String name = "Bhuwan";
26+
String anotherName = "Gautam";
27+
Double amount = 0.0;
28+
BoundedType.compare(name, anotherName);
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.generics;
5+
6+
/**
7+
* @author bhuwan
8+
*/
9+
public class GenericsAndInheritance {
10+
public static void main(String[] args) {
11+
String name = "Bhuwan";
12+
Object nameObject = new Object();
13+
nameObject = name;
14+
15+
GenericsType<String> nameString = new GenericsType<String>();
16+
GenericsType<Object> object = new GenericsType<Object>();
17+
// this is not same with generic
18+
//object = nameString;
19+
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.generics;
5+
6+
/**
7+
* @author bhuwan
8+
*/
9+
public class GenericsInheritance {
10+
11+
@SuppressWarnings("unused")
12+
public static void main(String[] args) {
13+
String str = "GenericsInheritance";
14+
Object obj = new Object();
15+
// works because String is-a Object, inheritance in java
16+
obj = str;
17+
18+
MyClass<String> myClass1 = new MyClass<String>();
19+
MyClass<Object> myClass2 = new MyClass<Object>();
20+
// compilation error since MyClass<String> is not
21+
//myClass2 = myClass1;
22+
// MyClass<T> parent is Object
23+
obj = myClass1;
24+
}
25+
26+
public static class MyClass<T> {
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.generics;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/**
10+
* @author bhuwan
11+
*/
12+
public class GenericsLowerBounded {
13+
14+
public static void main(String[] args) {
15+
List<Number> ints = new ArrayList<>();
16+
ints.add(3);
17+
ints.add(5);
18+
ints.add(10);
19+
double sum = sumWithWildCard(ints);
20+
System.out.println("Sum of ints=" + sum);
21+
}
22+
23+
public static double sumWithWildCard(List<? super Number> list) {
24+
double sum = 0;
25+
for (Object n : list) {
26+
sum += (Integer)n;
27+
}
28+
return sum;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.generics;
5+
6+
import java.util.logging.Logger;
7+
8+
/**
9+
* @author bhuwan
10+
*/
11+
public class GenericsMethods {
12+
13+
private static final Logger log = Logger.getLogger(WithouUsingGenerics.class.getName());
14+
15+
// Generics in method
16+
public static <T> boolean isEqual(GenericsType<T> g1, GenericsType<T> g2) {
17+
return g1.get().equals(g2.get());
18+
}
19+
20+
public static void main(String args[]) {
21+
GenericsType<String> g1 = new GenericsType<>();
22+
g1.set("Bhuwan");
23+
24+
GenericsType<String> g2 = new GenericsType<>();
25+
g2.set("Bhuwan");
26+
27+
boolean isEqual = GenericsMethods.<String> isEqual(g1, g2);
28+
// above statement can be written simply as
29+
log.info("isEqualFirst:: " + isEqual);
30+
isEqual = GenericsMethods.isEqual(g1, g2);
31+
// This feature, known as type inference, allows you to invoke a generic
32+
// method as an ordinary method, without specifying a type between angle
33+
// brackets. Compiler will infer the type that is needed
34+
log.info("isEqualSecond:: " + isEqual);
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.generics;
5+
6+
/**
7+
* @author bhuwan
8+
*/
9+
public class GenericsType<T> {
10+
11+
private T t;
12+
13+
public T get() {
14+
return this.t;
15+
}
16+
17+
public void set(T t1) {
18+
this.t = t1;
19+
}
20+
21+
@SuppressWarnings({ "unchecked", "rawtypes" })
22+
public static void main(String args[]) {
23+
// java 7 style declaration
24+
// Generic type instantiation
25+
GenericsType<String> type = new GenericsType<>();
26+
type.set("Bhuwan Gautam");
27+
// this will be a compile time error
28+
// type.set(11);
29+
30+
// raw type instantiation
31+
GenericsType type1 = new GenericsType();
32+
type1.set("Prakash");
33+
type1.set(10);
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.generics;
5+
6+
/**
7+
* @author bhuwan
8+
*/
9+
public class GenericsTypeOld {
10+
11+
private Object t;
12+
13+
public Object get() {
14+
return t;
15+
}
16+
17+
public void set(Object t) {
18+
this.t = t;
19+
}
20+
21+
@SuppressWarnings("unused")
22+
public static void main(String args[]) {
23+
GenericsTypeOld type = new GenericsTypeOld();
24+
type.set("Bhuwan Gautam");
25+
// type casting, error prone and can cause ClassCastException
26+
String name = (String)type.get();
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.generics;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/**
10+
* @author bhuwan
11+
*/
12+
public class GenericsUpperBounded {
13+
14+
public static double sumWithoutWildCard(List<Number> list) {
15+
double sum = 0;
16+
for (Number n : list) {
17+
sum += n.doubleValue();
18+
}
19+
return sum;
20+
}
21+
22+
public static void main(String[] args) {
23+
List<Number> ints = new ArrayList<>();
24+
ints.add(3);
25+
ints.add(5);
26+
ints.add(10);
27+
double sum = sumWithWildCard(ints);
28+
System.out.println("Sum of ints=" + sum);
29+
}
30+
31+
public static double sumWithWildCard(List<? extends Number> list) {
32+
double sum = 0;
33+
for (Number n : list) {
34+
sum += n.doubleValue();
35+
}
36+
return sum;
37+
}
38+
39+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
*
3+
*/
4+
package com.lftechnology.generics;
5+
6+
import java.util.List;
7+
8+
/**
9+
* @author bhuwan
10+
*/
11+
public interface ISort {
12+
13+
// By using raw type
14+
@SuppressWarnings("rawtypes")
15+
public List sort(final List aList);
16+
17+
}

0 commit comments

Comments
 (0)