Java Reintroduction-Java Collection

background

Java Collection memo writing. Also, a generics-like writing memo.

References

Verification environment

Basic form of Collection

  1. List --Managed by Index. So-called general array --Elements can be duplicated
  2. Set --Elements are in no particular order --Elements cannot be duplicated
  3. Queue / Deque
  1. Map --Managed by key / value pairs --Keys cannot be duplicated

In the Java Tutorial, it is explained in this figure because it is a Core Collection Interface. image

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));
		}

? Wildcard (unbounded wildcard type) example

	// 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);
	}

Class / Method example using Generic Type

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

Java Reintroduction-Java Collection
☾ Java / Collection
Java9 collection
[Java] Collection framework
Expired collection of java
Java parallelization code sample collection
[Java] Comparator of Collection class
Java test code method collection
What is a Java collection?
Java
Java
Deep copy collection in Java
JNA (Java Native Access) pattern collection
Java Performance Chapter 5 Garbage Collection Basics
Java learning (0)
Studying Java ―― 3
Java protected
[Java] Annotation
[Java] Module
Java array
Significance of interface learned from Java Collection
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java (interface)
Java memorandum
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
Sample code collection for Azure Java development
java beginner 3
Java memo
java (encapsulation)
Java inheritance
Java basics
Decompile Java