Java Collection memo writing. Also, a generics-like writing memo.
In the Java Tutorial, it is explained in this figure because it is a Core Collection Interface.
Generics The one who writes like this ... I can't explain it ...
Collection<String> col1 = new ArrayList<>();
See here for generics terms and more. Points of Java Generics
Type Parameter naming convention
Type Parameter | Description |
---|---|
E | Element |
K | Key |
N | Number |
T | Type |
V | Value |
S,U,V etc. | 2nd, 3rd, 4th types |
ArrayList
ArrayList sample code.
It works, but it gets angry from Eclipse in the following places ...
「List myList = new ArrayList();
」
// Old usage of List - Raw type List
List myList = new ArrayList();
String s1 = "Apple";
String s2 = "Orange";
String s3 = "Banana";
myList.add(s1);
myList.add(s2);
myList.add(s3);
// Size, Print List
System.out.println("List size : " + myList.size());
for (Object obj : myList) {
System.out.println(obj);
}
// Sort List
Collections.sort(myList);
for (Object obj : myList) {
System.out.println(obj);
}
// Using Iterator
Iterator itr = myList.iterator();
while (itr.hasNext()) {
System.out.println((String)itr.next());
}
Click here for a modern way (Java 7 and above) using Generics (parameterized type).
The correct answer is "List <String> myList = new ArrayList <> ();
".
The point is to thoroughly type-safe at the coding level.
// New usage of List - Generics
List<String> myList = new ArrayList<>();
String s1 = "Apple";
String s2 = "Orange";
String s3 = "Banana";
myList.add(s1);
myList.add(s2);
myList.add(s3);
// Using Iterator
Iterator<String> itr = myList.iterator();
while (itr.hasNext()) {
System.out.println((String)itr.next());
}
HashSet HashSet sample code.
// Use HashSet (Old Style)
Set hs = new HashSet();
hs.add("Hello");
hs.add("Dolly");
// Using Iterator
Iterator itr = hs.iterator();
while (itr.hasNext()) {
System.out.println((String)itr.next());
}
// New Style
Set<String> hs2 = new HashSet<>();
// Using Iterator
Iterator<String> itr2 = hs2.iterator();
while (itr2.hasNext()) {
System.out.println(itr2.next());
}
HashMap HashMap sample code.
Map<Integer, String> map1 = new HashMap<>();
map1.put(1, "Cloud");
map1.put(2, "Big Data");
map1.put(3, "Machine Learning");
// key objects
Set<Integer> keys = map1.keySet();
for (Object key : keys) {
System.out.println(key + ": " + map1.get(key));
}
// argument with ? wildcard
public static void printList(List<?> list) {
for (Object obj : list ) {
System.out.println(obj);
}
}
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
list1.add("Apple");
list1.add("Grape");
printList(list1);
List<Integer> list2 = new ArrayList<>();
list2.add(100);
list2.add(300);
printList(list2);
}
public class GenericTypeClassSample<T> {
T height;
T width;
public GenericTypeClassSample(T width, T height) {
super();
this.width = width;
this.height = height;
}
public T getWidth() {
return width;
}
public void setWidth(T width) {
this.width = width;
}
public T getHeight() {
return height;
}
public void setHeight(T height) {
this.height = height;
}
public static void main(String[] args) {
GenericTypeClassSample<Integer> rect1 = new GenericTypeClassSample<>(10, 5);
System.out.println("Area : " + rect1.getHeight() * rect1.getWidth());
GenericTypeClassSample<Double> rect2 = new GenericTypeClassSample<>(4.3, 5.7);
System.out.println("Area : " + rect2.getHeight() * rect2.getWidth());
}
}
Recommended Posts