Class set Framework in Java
The class set framework, then what is the class set framework, what kind of collections there are, and the infrastructure of the class set framework.
The class set framework is a collection of classes and interfaces that java.util
are used by users to store and manage objects, in which we mainly learn three main classes, namely, collections, lists, and mappings.
Collections, lists, mappings
set is a collection in which the objects in the collection are not in order, and are objects that have no duplicates. simple: unordered, no repetition.
lists are listed , the objects in the list are sorted by order, and there are duplicate objects.
Map is a map in which each element in the collection has a key object and a value object, and the key in the map cannot be duplicated, and the value can be duplicated.
Class Collection Framework Body
Composition and classification of set frame
Top Iterator and collection
import java.util.List;import java.util.ArrayList;public class Demo{ public static void main(String args[]){ // 创建列表的实现类对象 ArrayList<String> arrayList = new ArrayList<String>(); // 添加数据 arrayList.add("a"); arrayList.add("b"); arrayList.add("c");// 移除 arrayList.remove(1); // 获取数据 String s = arrayList.get(1); System.out.println(s); // 获取长度 int a = arrayList.size(); // 打印所有元素 for(int i=0; i<arrayList.size(); i++){ String s = arrayList.get(i); System.out.println(s); } }}
A class collection framework is a class of jdk
classes and interfaces that are provided to master collections, lists, mappings, collections that are unordered, the elements in the collection are not allowed to be duplicates, the list is ordered, the elements in the list are allowed to be duplicated, the mappings are stored in key-value pairs, the keys are non-repeatable, and the values can be duplicated.
Collection and Iterator interfaces
Collection
Yes List
and Set
the parent class
Collection
Method of the interface
Method |
Description |
int size() |
Returns the number of elements in the collection |
boolean add(Object obj) |
To add an object to the collection |
remove(Object obj) |
Remove an element from the collection |
void clear() |
Delete all objects in a collection |
boolean isEmpty() |
Determines whether the collection is empty |
Set and HashSet are used for the set implementation class
import java.util.Set;import java.util.HashSet;public class Demo{ public static void main(String args[]){ HashSet<String> hashSet = new HashSet<String>(); Set<String> set = hashSet; // 实现类向上转型为接口 // 同理 Set<String> set = new HashSet<String>(); // 添加元素 set.add("a"); set.add("b"); set.add("c"); set.add("d"); // 获取集合中的元素长度 int a = set.size(); }}
Iterator
Interface Iterator Object
Iterator - > Collection -> Set - > HashSet
hasNext() next()
it.hasNext()
Whether there are elements
it.next()
remove element
import java.util.Set;import java.util.HashSet;import java.util.Iterator;public class Demo{ public static void main(String args[]){ HashSet<String> hashSet = new HashSet<String>(); Set<String> set = hashSet; // 实现类向上转型为接口 // 同理 Set<String> set = new HashSet<String>(); // 添加元素 set.add("a"); set.add("b"); set.add("c"); set.add("d"); // 迭代器 iterator() 遍历整个set// 通过迭代器取出集合中的对象 Iterator<String> it = set.iterator<String>(); while(it.hasNext){ String s = it.next(); System.out.println(s); } }}
Set
and HashSet
for Set
the implementation class to use
Iterator, Collection, Set, HashSet
Iterator, Collection, List--ArrayList
hasNext()
Determine if there is a next element
next()
Remove the element and move the cursor to the next
Use of Map and HASHMAP (implementation class of map)
Map
For a map, each element in the map has a key object and a value object, in which the Map
key is not repeatable and the value can be duplicated. From jdk1.2
having this class.
java.utilInterface Map<K,V>K - the type of keys maintained by this mapV - the type of mapped values
Map
is an object that keys
values
establishes a mapping relationship between a key and a value, one that cannot be map
duplicated, and each can only be keys
key
uniquely mapped to a value.
Method |
Description |
put(K key, V value) |
map Add a key-value pair to the |
get(Object key) |
Get value |
import java.util.Map;import java.util.HahMap;public class Demo{ public static void main(String args[]){ HashMap<String,String> hasMap = new HashMap<String,String>(); Map<String,String> map = hasMap; // 向上转型 // 同理 Map<Stirng,String> map = new HashMap<String,String>(); // put() 用来存放键值对 map.put("1","a"); map.put("2","b"); map.put("3","c");// 获取长度 int i = map.size();// 获取键为2的值 String s = map.get("2"); }}
The Map
Middle key is not repeatable, and the value can be repeated. If you map
add the same key, the new value overwrites the old value.
For the rest of your life, with you.
Jian Suda tert-Niche
A handsome boy, good development habits, ability to think independently, initiative and good at communication
Jane Book Blog: https://www.jianshu.com/u/c785ece603d1
Conclusion
- I will continue to explain the other knowledge in depth, I am interested to continue to focus on
- A little gift to walk or like.
Section 41st: Class set framework in Java