The usage codes of List, Set collection and Map in Java are as follows:
package tingjizifu;import java.util.*;public class TongJi {/** Use Scanner to read a string from the console, count the number of times each character appears in the string, and require the above requirements to be completed using the learned knowledge* The implementation idea is completed based on the characteristics of Set, List, and Map collections. */public static void main(String[] args) {// Input string Scanner input = new Scanner(System.in);String shuRu = input.next();// Load string characters into List set List<String> list = new ArrayList<String>(); for (int i = 0; i < shuRu.length(); i++) {list.add(String.valueOf(shuRu.charAt(i)));}// Load string characters into Set set Set<String> set = new HashSet<String>(); for (int i = 0; i < shuRu.length(); i++) {set.add(String.valueOf(shuRu.charAt(i)));}// Compare the characters in the Set with the characters in the List, and add 1 to count the same way. // Then use the characters in the Set set as the key (key) and the counted number as the value (value), and finally print out the data in the Map Map<String, Integer> map = new HashMap<String, Integer>(); for (String str : set) {int sum = 0;// System.out.println(str); for (int i = 0; i < list.size(); i++) {if (list.get(i).equals(str)) {sum++;}}map.put(str, sum);}Set<String> ss = map.keySet(); for (String str : ss) {System.out.println(str + "appearances are" + map.get(str) + "times");}input.close();}}Let's see the difference between set map list
All collection interfaces
set --The values in it are not allowed to be repeated, and the unordered data structure is
list -- The values in which are allowed to be repeated because they are ordered data structures
map-Paired data structures, the keys must be unique (the keys cannot be the same, otherwise the value will be replaced)
List Save objects in the order in which objects enter, without sorting or editing operations.
Set accepts only once for each object and uses its own internal sorting method (usually, you only care whether an element belongs to Set, not its order - otherwise you should use List).
Map also saves a copy of each element, but this is based on "key" and Map also has built-in sorting, so it doesn't care about the order in which elements are added. If the order in which elements are added is important to you, you should use LinkedHashSet or LinkedHashMap.
Collection is an object collection, and Collection has two subinterfaces List and Set
List can obtain the value by subscripting (1,2..), and the value can be repeated
Set can only use cursors to get values, and the values cannot be repeated
ArrayList, Vector, LinkedList is the implementation class of List
ArrayList is thread-insecure, Vector is thread-safe, and both classes are implemented by arrays.
LinkedList is thread-insecure, and the underlying layer is implemented by linked lists.
Map is a key-value pair collection
HashTable and HashMap are Map implementation classes
HashTable is thread-safe and cannot store null values
HashMap is not thread-safe and can store null values
The above is the knowledge about the use of List, Set collection and Map in Java introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!