1, The methods of the Collection interface are:
- boolean add(E e)
This method adds an element to the collection. E represents generics (that is, the data type is determined when the object is created). The return value is a boolean value. When the addition is successful, it returns true, but this return value is generally not received.
Collection<String> col=new ArrayList<>(); boolean ad = col.add("tearful"); System.out.println("ad-->"+ad); System.out.println("The added elements are:"+col);
Operation results:
- boolean remove(Object o)
If present, removes the specified element from the collection. That is, using this method, you can delete one or more elements e at the same time, and the return value is true. If there is no such element in the collection, the deletion fails and returns false.
col.add("tearful"); col.add("Hastily"); col.add("Guan Yu"); col.add("Liu Bei"); boolean rem = col.remove("Hastily"); System.out.println("rem-->"+rem); System.out.println("The collection elements are:"+col); boolean mov = col.remove("Fei Zhang"); System.out.println("mov-->"+mov); System.out.println("The collection elements are:"+col);
Operation results:
- int size()
Returns the number of elements in the collection. If this collection contains elements larger than integer MAX_ Value, integer MAX_ VALUE
col.add("tearful"); col.add("Hastily"); col.add("Guan Yu"); col.add("Liu Bei"); int size = col.size(); System.out.println("col The length of the collection is:"+size);
Operation results:
- boolean contains(Object o)
Returns true if the collection contains the specified element o. That is, whether there are elements o with the same data and type in the collection. If yes, it returns true. Otherwise, it returns false.
col.add("tearful"); col.add("Hastily"); col.add("Guan Yu"); col.add("Liu Bei"); boolean cont = col.contains("Hastily"); System.out.println("cont-->"+cont); boolean tain = col.contains("Fei Zhang"); System.out.println("tian-->"+tain);
Operation results:
5. boolean isEmpty()
Whether there are elements in the collection. If yes, it returns false. Otherwise, it returns true.
Collection <String> list1=new ArrayList<>(); list1.add("tearful"); list1.add("Hastily"); boolean emp = list1.isEmpty(); System.out.println("emp-->"+emp); Collection<String> arr=new ArrayList<>(); boolean arremp = arr.isEmpty(); System.out.println("arremp-->"+arremp);
Operation results:
The above just lists some methods that I will show in a deeper code practice later, not all of them.
2, Method of Map interface
Map is a collection of Key Value pairs. A Key corresponds to a Value; In a map object, the same Key is not allowed, but the same Value can exist, that is, one Key corresponds to one Value, and one can have different keys. An Entry object (only Key Value pairs) stores Key Value pairs in the map. Map collection is to facilitate data access. When fetching data, you can obtain data through Key or directly obtain Value through Entry object. Introduce several main methods that are widely used:
- V put(K key, V value) adds the specified value and the specified key in this mapping to the collection.
be careful:
(1) If there is a key in the collection, the key will overwrite the old value with the new value
(2) The returned value is the same type as the value given when defining the Map object. If the key to be added exists in the collection, the old value will be returned, otherwise null will be returned.
Map<String,Integer> map=new HashMap<>(); Integer value1 = map.put("Spend less", 1); map.put("Deng Shao", 2); System.out.println(value1); System.out.println(map); Integer value2 = map.put("Spend less", 11); System.out.println(value2); System.out.println(map);
Operation results:
- boolean isEmpty() returns true if the Map collection is empty, otherwise false
Map<String,Integer> map=new HashMap<>(); Integer value1 = map.put("Spend less", 1); map.put("Deng Shao", 2); map.put("Zhou Shao", 3); Map<String,Integer> colMap=new HashMap<>(); System.out.println("map: "+map); System.out.println("colMap: "+colMap); boolean mapEmpty = map.isEmpty(); boolean colMapEmpty = colMap.isEmpty(); System.out.println("map Empty:"+mapEmpty); System.out.println("colMap Empty:"+colMapEmpty);
Operation results:
- int size() returns the number of key value pairs in this Map collection.
Map<String,Integer> map=new HashMap<>(); Integer value1 = map.put("Spend less", 1); map.put("Deng Shao", 2); map.put("Zhou Shao", 3); Map<String,Integer> colMap=new HashMap<>(); System.out.println("map: "+map); System.out.println("colMap: "+colMap); int mapSize = map.size(); System.out.println("map The size of the is:"+mapSize); int colMapSize = colMap.size(); System.out.println("colMap The size of the is:"+colMapSize);
Operation results:
- V remove(Object key) removes the key value pair from the Map collection if the key exists.
be careful:
(1) The remove method will delete the value value pair corresponding to the key. The value returned after successful deletion is the deleted value. Otherwise, null will be returned
(2) In a Map collection that allows null keys and values to be stored (such as hashMap and linkedMap), returning a null value does not mean that the key value pair does not exist in the original collection, and the value value may be null
(3) In the Map collection that allows null keys and values to be stored (such as hashMap and linkedMap), parameter passing null is also allowed, while in the Map collection that allows null keys and values to be stored (such as hashTable), an error will be reported.
Map<String,Integer> map=new HashMap<>(); map.put("Spend less", 1); map.put("Deng Shao", 2); map.put("Zhou Shao", 3); map.put("Leaf less", 4); System.out.println("map: "+map); Integer value1 = map.remove("Leaf less"); Integer value2 = map.remove("leaf"); System.out.println("value1-->"+value1); System.out.println("value2-->"+value2); System.out.println("map:"+map);
Operation results:
- Set keySet() returns the set of keys contained in this map.
V get(Object key) returns the value corresponding to the specified key. If the Map collection does not contain the mapping relationship of the key, it returns null.
Map<String,Integer> map=new HashMap<>(); map.put("Spend less", 1); map.put("Deng Shao", 2); map.put("Zhou Shao", 3); map.put("Leaf less", 4); System.out.println("map: "+map); Set<String> set=map.keySet(); for (String s : set) { System.out.println(s+": "+map.get(s)); } String m="Pig man"; System.out.println(m+": "+map.get(m));
Operation results:
- Set<Map. Entry < K, V > > entryset() returns the set of key value pairs contained in this map set.
Note: static interface map Entry < K, V > key value pair. Entry is an internal class that saves key value pairs. In the entry interface, there are:
(1) K getKey() get the key directly
(2) V getValue() gets the value directly,
(3) V setValue() replaces the value corresponding to this item with the specified value
Map<String,Integer> map=new HashMap<>(); map.put("Spend less", 1); map.put("Deng Shao", 2); map.put("Zhou Shao", 3); map.put("Leaf less", 4); System.out.println("map: "+map); Set<Map.Entry<String,Integer>> entrySet=map.entrySet(); for (Map.Entry<String, Integer> entry : entrySet) { System.out.println(entry.getKey()+": "+entry.getValue()); }
Operation results:
- Collection values() returns a collection of values contained in this map.
Map<String,Integer> map=new HashMap<>(); map.put("Spend less", 1); map.put("Deng Shao", 2); map.put("Zhou Shao", 3); map.put("Leaf less", 4); System.out.println("map: "+map); Collection<Integer> list=map.values(); System.out.println("list: "+list);
Operation results: