GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — JavaLearningPath/Generics/src/GenericMethods.java at master · AlexGithubbb/JavaLearningPath

Expand file tree

/

GenericMethods.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
58 lines (45 loc) · 1.59 KB
/

GenericMethods.java

Copy path

File metadata and controls

58 lines (45 loc) · 1.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class GenericMethods {
public static void main(String[] args) {
Pair<String, Integer> p1 = new Pair<>("Hello", 22);
Pair<String, Integer> p2 = new Pair<>("Hello2", 11);
// method 1 to call compare()
System.out.println(GenericMethods.compare(p1, p2)); // false
// method 2 to call compare()
// boolean same = GenericMethods.<String, Integer>compare(p1, p2);
/*
* his feature, known as type inference, allows you to invoke a generic method as an ordinary method,
* without specifying a type between angle brackets.
* This topic is further discussed in the following section, Type Inference.*/
boolean same = GenericMethods.compare(p1, p2); // simplified syntax
System.out.println(same);
p1.setKey("Hello2");
p2.setValue(22);
System.out.println(compare(p1, p2)); // true
}
// create a generic method
public static<K, V> boolean compare(Pair<K, V>p1, Pair<K, V>p2){
return (p1.getKey().equals(p2.getKey()) &&
p1.getValue().equals(p2.getValue())
);
}
public static class Pair<K, V>{
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public void setKey(K key) {
this.key = key;
}
public void setValue(V value) {
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.