catalogue
List interface and implementation class
Set interface and implementation class
Map interface and implementation class
Java collection framework
A collection is sometimes called a container. In short, it is an object that can aggregate multiple elements with the same properties into a whole
Collections are used to store, retrieve, manipulate, and transfer aggregated data
Collections Framework is a unified architecture used to represent and manipulate collections. All collection frames contain the following:
- Interface: an abstract data type representing a collection.
- Implementation: it is the specific implementation of the collection interface. In essence, they are reusable data structures and classes.
- Algorithm: it is a method to perform useful calculation on the object that implements the collection interface, such as search and sorting.
Collection frame structure diagram
Collection interface
Traversal of Collection elements
Traversal of two sets
- For each structure traversal
2. Iterator iterator traversal
Iterator interface
Iterator objects created using the iterator() method of the collection are all sub type objects of the interface
During iterative traversal, the collection cannot be used to add or delete data
method:
- hasNext()
- next()
- remove()
Traversal mode
for loop writing
List<String>list=new ArrayList<String>(); for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) { String s = iterator.next(); System.out.print(s+" "); }
while loop writing
ArrayList<Integer>list=new ArrayList<Integer>(); Iterator<Integer> it = list.iterator(); while(it.hasNext()) { System.out.print(i+" "); }
foreach loop
List<String>list=new ArrayList<String>(); for (String s : list) { System.out.print(s+" "); }
List interface and implementation class
The List interface is an ordered collection that can contain duplicate elements
In addition to the operations inherited from the Collection, the List interface also provides the following methods to operate in sequence: ["aaa","bbb","ccc"]
Set interface and implementation class
- Set is an interface that cannot contain duplicate elements.
- The Set interface is a sub interface of the Collection
- Only methods inherited from the Collection are included
- The restriction on the add method is added, and duplicate elements are not allowed.
The common implementation classes of the Set interface include HashSet, TreeSet and LinkedHashSet
Write a program to obtain the string list in the command line parameters, and output the repeated words, non repeated words and the word list after eliminating the repetition.
Code example
import java.util.HashSet; import java.util.Set; public class SetTest { public static void main(String[] args) { String[] words={"a","b","c","d","e","f","g","d"}; Set<String> set=new HashSet<>(); for (String word : words) { if(!set.add(word)){ System.out.println("There are duplicate words"+word); } } System.out.println("Co existence"+set.size()+"Words that are not repeated are"+set); } }
Map interface and implementation class
- A Map is a collection of elements that contain key value pairs.
- Map cannot contain duplicate keys
- Each key can be mapped to a maximum of one value.
The Map interface defines a series of methods:
Code example
import java.util.HashMap; import java.util.Map; public class MapTest { public static void main(String[] args) { String[] words={"a","b","c","d","e","f","g","d","e","d"}; Map<String,Integer> map=new HashMap<>(); for (String word : words) { Integer times=map.get(word); map.put(word,times==null?1:times+1); } for (Map.Entry<String, Integer> entry : map.entrySet()) { System.out.println(entry); } } }
Collections utility class
1.Collections provides common algorithms for collection operations, which are provided in the form of static methods
2. The first parameter of these static methods is the set that needs to be operated on
3. Most of the algorithms provided by collections are aimed at Java util. List type collection can operate, and some can operate any type of collection
4.Collections collection operation tool class is located in Java Util package
5. The methods commonly used in collections are:
- Sort: void sort(List l)
- Random sorting: void shuffle(List l)
- Reverse the order in the set: void reverse(List l)
- Binary search: int binarySearch(List l,Key)
- Find extreme values: T max(List l),T min(List l)
Arrays tool class
Arrays provides various methods of array operation
Arrays provides a static method for converting arrays to lists
Common methods provided by Arrays are:
- Convert array to List: asList()
- Binary search: binarySearch()
- Copy array: copyOf()
- Array sorting: sort()
- Initialize array: fill()
Homework
Assignment 1:
Use Scanner to read a string from the console and count the number of occurrences of each character in the string. It is required to use the learned knowledge to complete the above requirements
The implementation idea is completed according to the characteristics of Set, List and Map Set.
Collections utility class