JAVA COLLECTIONS Java Generics Java Collections Generic method
![JAVA COLLECTIONS Java Generics Java Collections JAVA COLLECTIONS Java Generics Java Collections](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-1.jpg)
![Generic method static float add(float a, float b) { float answer = a + Generic method static float add(float a, float b) { float answer = a +](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-2.jpg)
![Generic Class class Pair<T, S> { public T first; public S second; public Pair( Generic Class class Pair<T, S> { public T first; public S second; public Pair(](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-3.jpg)
![Generics – Stack Example class Stack<E> { private final int size; private int top; Generics – Stack Example class Stack<E> { private final int size; private int top;](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-4.jpg)
![Java Collections • The Java collections framework (JCF) is a set of classes and Java Collections • The Java collections framework (JCF) is a set of classes and](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-5.jpg)
![Three main types of collections • Lists: always ordered, may contain duplicates and can Three main types of collections • Lists: always ordered, may contain duplicates and can](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-6.jpg)
![Generic List in Java List<String> list = new Array. List<String>; String string 1 = Generic List in Java List<String> list = new Array. List<String>; String string 1 =](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-7.jpg)
![Generic Set in Java Set<String> set = new Hash. Set<String>; String string 1 = Generic Set in Java Set<String> set = new Hash. Set<String>; String string 1 =](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-8.jpg)
![Generic Map in Java Map<Integer, String> map = new Hash. Map<Integer, String>; Integer key Generic Map in Java Map<Integer, String> map = new Hash. Map<Integer, String>; Integer key](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-9.jpg)
![Java Collections Hierarchy Java Collections Hierarchy](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-10.jpg)
![Lists List list. A = new Array. List(); list. A. add("element 0"); list. A. Lists List list. A = new Array. List(); list. A. add("element 0"); list. A.](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-11.jpg)
![Generic Sorting List list = new Array. List(); //add elements to the list Collections. Generic Sorting List list = new Array. List(); //add elements to the list Collections.](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-12.jpg)
- Slides: 12
![JAVA COLLECTIONS Java Generics Java Collections JAVA COLLECTIONS Java Generics Java Collections](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-1.jpg)
JAVA COLLECTIONS Java Generics Java Collections
![Generic method static float addfloat a float b float answer a Generic method static float add(float a, float b) { float answer = a +](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-2.jpg)
Generic method static float add(float a, float b) { float answer = a + b; return answer; } static int add(int i 1, float i 2) { int answer = a + b; return answer; } static long add(long a, long b) { long answer = a + b; return answer; } // Generic method static <T> add(T a, T b) { T answer = a + b; return answer; }
![Generic Class class PairT S public T first public S second public Pair Generic Class class Pair<T, S> { public T first; public S second; public Pair(](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-3.jpg)
Generic Class class Pair<T, S> { public T first; public S second; public Pair( T a, S b ) { first = a; second = b; } } Pair<String, Color> color. Name = new Pair<String, Color>("Red", Color. RED); Pair<Double, Double> coordinates = new Pair<Double, Double>(17. 3, 42. 8);
![Generics Stack Example class StackE private final int size private int top Generics – Stack Example class Stack<E> { private final int size; private int top;](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-4.jpg)
Generics – Stack Example class Stack<E> { private final int size; private int top; private E[] elements; public Stack() { this(10); } public Stack(int s) { size = s > 0 ? s : 10; top = -1; elements = (E[]) new Object[size]; // create array } public void push(E push. Value) { if (top == size - 1) // if stack is full throw new Full. Stack. Exception(String. format("Stack is full, cannot push %s", push. Value)); elements[++top] = push. Value; // place push. Value on Stack } public E pop() { if (top == -1) // if stack is empty throw new Empty. Stack. Exception("Stack is empty, cannot pop"); return elements[top--]; // remove and return top element of Stack } }
![Java Collections The Java collections framework JCF is a set of classes and Java Collections • The Java collections framework (JCF) is a set of classes and](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-5.jpg)
Java Collections • The Java collections framework (JCF) is a set of classes and interfaces that implement commonly reusable collection data structures. [1] • Although it is a framework, it works in a manner of a library. The JCF provides both interfaces that define various collections and classes that implement them.
![Three main types of collections Lists always ordered may contain duplicates and can Three main types of collections • Lists: always ordered, may contain duplicates and can](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-6.jpg)
Three main types of collections • Lists: always ordered, may contain duplicates and can be handled the same way as usual arrays • Sets: cannot contain duplicates and provide random access to their elements • Maps: connect unique keys with values, provide random access to its keys and may host duplicate values
![Generic List in Java ListString list new Array ListString String string 1 Generic List in Java List<String> list = new Array. List<String>; String string 1 =](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-7.jpg)
Generic List in Java List<String> list = new Array. List<String>; String string 1 = "a string"; list. add(string 1); String string 2 = list. get(0); List<String> list = new Array. List<String>; Iterator<String> iterator = list. iterator(); while(iterator. has. Next()) { String a. String = iterator. next(); }
![Generic Set in Java SetString set new Hash SetString String string 1 Generic Set in Java Set<String> set = new Hash. Set<String>; String string 1 =](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-8.jpg)
Generic Set in Java Set<String> set = new Hash. Set<String>; String string 1 = "a string"; set. add(string 1); Iterator<String> iterator = set. iterator(); while(iterator. has. Next()) { String a. String = iterator. next(); }
![Generic Map in Java MapInteger String map new Hash MapInteger String Integer key Generic Map in Java Map<Integer, String> map = new Hash. Map<Integer, String>; Integer key](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-9.jpg)
Generic Map in Java Map<Integer, String> map = new Hash. Map<Integer, String>; Integer key 1 = new Integer(123); String value 1 = "value 1"; map. put(key 1, value 1); String value 1_1 = map. get(key 1); Iterator<Integer> key. Iterator = map. key. Set(). iterator(); while(key. Iterator. has. Next()){ Integer a. Key = iterator. next(); String a. Value = map. get(a. Key); }
![Java Collections Hierarchy Java Collections Hierarchy](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-10.jpg)
Java Collections Hierarchy
![Lists List list A new Array List list A addelement 0 list A Lists List list. A = new Array. List(); list. A. add("element 0"); list. A.](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-11.jpg)
Lists List list. A = new Array. List(); list. A. add("element 0"); list. A. add("element 1"); list. A. add("element 2"); String element 0 = list. A. get(0); String element 1 = list. A. get(1); String element 3 = list. A. get(2); //access via Iterator iterator = list. A. iterator(); while(iterator. has. Next() { String element = (String) iterator. next(); } for(Object object : list. A) { String element = (String) object; }
![Generic Sorting List list new Array List add elements to the list Collections Generic Sorting List list = new Array. List(); //add elements to the list Collections.](https://slidetodoc.com/presentation_image_h2/69462f29475bff12b5c2f1c37d6e7c09/image-12.jpg)
Generic Sorting List list = new Array. List(); //add elements to the list Collections. sort(list); public interface Comparable<T> { int compare. To(T o); }
System.collections.generics
Java generic array
Generics java erklärung
Advantages of generics in java
Java generics array
System.collections.generic namespace
Hospitality generics
Kotlinx serialization generics
C generics
Minimax java
Collections overview in java
Java collections framework diagram
Collection hierarchy in java