1. Java Collection Framework Overview
Java SE includes a Java Collection Framework (JCF) composed of a set of classes and interfaces. Its main function is to organize stored data in a certain structure and access this data in a specific way. Its goal is to provide a general framework for processing object collections and reduce the amount of encoding that programmers use when processing different object collections.
Some differences in collection classes, in addition to whether they support duplicate element operations, include whether the elements are in order and whether null elements are allowed to be added. According to these three differences in the Java collection framework, the storage methods of objects are divided into three types, namely:
In order to support the sorting and traversal access operations of objects, several interfaces are provided in the Java collection framework:
2.Collection interface and Iterator interface
Some basic methods shared by Collection objects are defined in the Collection interface.
| method | describe |
| int size() | Returns the number of elements contained in the current collection |
| isEmpyt() | Determine whether the set contains elements |
| boolean contains(Object o) | Determine whether a specified element is contained in the collection |
| add(Object o) | Add an element to the collection |
| remove(Object o) | Delete an element from the collection |
| Iterator iterator() | Returns a traverser to access various elements in the collection |
The Iterator interface is an interface used to traverse a collection.
| method | describe |
| hasNext() | If there are more elements in the collection, the method returns true |
| next() | Return the next element in the collection |
| remove() | Delete the last element returned by Iterator |
1.List interface
The List interface is inherited from the Collection interface, and it has the following characteristics:
The most commonly used implementation classes for List interface are the ArrayList class and the LinkedList class.
1).ArrayList
Program example:
package lei; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { List list = new ArrayList<>(); list.add(1); list.add("zhangsan"); list.add(false); list.add('a'); list.add(0, "lisi"); list.add(1); list.remove(1); list.remove(2); list.set(0, "wangwu"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }The equals() method defined by the Object class will return true only when the object passed to the method and the object calling the method is the same object. Two objects with the same state can be regarded as the same object by overwriting the equals() method.
2).LinkedList
| method | describe |
| void addFirst | Add an object at the beginning of the linked list |
| void addLast | Add an object at the end of the linked list |
| getFirst() | Return the first element in the linked list |
| getLast() | Return the last element in the linked list |
| removeFirst() | Delete the first element in the linked list |
| removeLast() | Delete the last element in the linked list |
Program example:
package lei; import java.util.LinkedList; import java.util.List; public class Test2 { public static void main(String[] args) { LinkedList l=new LinkedList<>(); l.add("zhangsan"); l.add("lisi"); l.addFirst(1); l.addLast(4); System.out.println(l.getFirst()); System.out.println(l.getLast()); l.removeFirst(); l.removeLast(); for (int i = 0; i < l.size(); i++) { System.out.println(l.get(i)); } } }Selection of LinkedList vs. ArrayList
If the list needs to be quickly accessed but does not insert and delete elements frequently, it will be better to select ArrayList; if the list needs to be frequently inserted and deleted, then you should select LinkedList.
2.set interface
The set interface inherits from the Collection interface and also inherits all methods of the Collection interface. The set interface has the following characteristics:
The most commonly used to implement the Set interface are the HashSet class and the TreeSet class.
1).Hashset
The Hashset class is a Set interface implementation based on hash algorithm. It has the following characteristics:
If the class we wrote redefines the equals method, then this class must also redefine the hashCode() method, and ensure that when the result of the two objects is compared with the equals method is true, the return values of the hashCode() method of the two objects are equal.
Program example:
package lei; import java.util.HashSet; import java.util.Set; public class Test4 { public static void main(String[] args) { Set<String> set=new HashSet<String>(); set.add("zhangsan"); set.add("lisi"); for(String s:set){ System.out.println(s); } } }2).TreeSet
The TreeSet class not only implements the class Set interface, but also implements the SortedSet interface, so as to ensure that the objects in the collection are sorted in a certain order. When an object is added to the TreeSet collection, it is inserted into an ordered sequence of objects, but this sort is not sorted in the order in which objects are added, but is sorted according to a certain algorithm.
TreeSet sorts elements in the natural order of elements, or sorts them according to the Comparator provided when the Set is created. TreeSet supports two sorting methods: natural sorting and custom sorting.
3.Map interface
The Map (map) interface is another important interface in the Java collection framework that is different from the Collection interface. It corresponds to a collection of corresponding relationships from keys to values. There are two groups of objects in the Map object container, one group of objects is used to save the key in the Map, and the other group is used to save the Value. Key and Value can upgrade data of any reference type. Key cannot be repeated, but Value can be repeated.
1).HashMap
HashMap is an implementation of the Map interface based on hash algorithm. HashMap saves its keys in the hash table for maintenance, and the keys are unique. However, HashMap does not guarantee that the keys are arranged in a specific order, especially that the order is permanently unchanged.
The HashMap class implements the Map interface, thus having all the methods of the Map interface.
package day1228; import java.util.*; public class HashMapDemo { public static void main(String[] args) { // Create a new HashMap Map<String, String> map = new HashMap<String, String>(); map.put("a1", "xiao"); map.put("b2", "xiaol"); map.put("a4", "xiaosd"); map.put("b1", "12a"); map.put("a3", "1"); // Use iterator to traverse the key and value System.out.println("Before the Map value is: "); Set<String> keys = map.keySet(); for (Iterator<String> i = keys.iterator(); i.hasNext();) { String key = i.next(); String value = map.get(key); System.out.println(key + "=" + value); } // Delete the value with the key "a4" System.out.println("/nDelete the element with the key value a4"); map.remove("a4"); // //Use iterator to traverse the keys and values System.out.println("/n Map value after: "); keys = map.keySet(); for (Iterator<String> i = keys.iterator(); i.hasNext();) { String key = i.next(); String value = map.get(key); System.out.println(key + "=" + value); } } }2).TreeMap
The TreeMap class is a Map interface implementation based on the red and black tree algorithm. The storage method of keys in TreeMap is similar to TreeSet. It stores keys in a tree, and the order of keys is arranged in natural order or custom order.
Program example:
package day1228; import java.util.*; public class TreeMapDemo { public static void main(String[] args) { //Create a new TreeMap Map<Integer, String> map = new TreeMap<Integer, String>(); map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); map.put(4, "four"); map.put(5, "five"); //Use iterator to display the keys and values System.out.println("The previous map value is: "); Set<Integer> keys=map.keySet(); for(Object key:keys){ String value=map.get(key); System.out.println(key+"="+value); } //Delete the value of key 3 System.out.println("/nDelete the element with key value 3"); map.remove(3); //Use iterator to display the key and value System.out.println("/nThe value after Map is: "); for(Object key:keys){ String value=map.get(key); System.out.println(key+"="+value); } } }The above is all the content of this article. I hope that the content of this article will be of some help to everyone’s study or work. I also hope to support Wulin.com more!