Home  >  Article  >  Java  >  A piece of code to understand the use of List, Set and Map in Java

A piece of code to understand the use of List, Set and Map in Java

高洛峰
高洛峰Original
2017-01-22 15:18:021499browse

The usage codes of List, Set collection and Map in Java are as follows:

package tingjizifu;
import java.util.*;
public class TongJi {
/*
* 使用Scanner从控制台读取一个字符串,统计字符串中每个字符出现的次数,要求使用学习过的知识完成以上要求
* 实现思路根据Set、List、Map集合的特性完成。
*/
public static void main(String[] args) {
// 输入字符串
Scanner input = new Scanner(System.in);
String shuRu = input.next();
// 把字符串字符装入List集合
List<String> list = new ArrayList<String>();
for (int i = 0; i < shuRu.length(); i++) {
list.add(String.valueOf(shuRu.charAt(i)));
}
// 把字符串字符装入Set集合
Set<String> set = new HashSet<String>();
for (int i = 0; i < shuRu.length(); i++) {
set.add(String.valueOf(shuRu.charAt(i)));
}
// 用Set中的字符与List中的字符进行比较,相同就加1进行计数,
// 然后把Set集合中的字符作为键(key),统计的数作为值(value),最后打印出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 + "出现次数为" + map.get(str) + "次");
}
input.close();
}
}

Let’s look at the difference between set map list

They are all collection interfaces

set --The values ​​are not allowed to be repeated, an unordered data structure
list --The values ​​are allowed to be repeated, because it is an ordered data structure
map --A paired data structure, the key value must have Uniqueness (the keys cannot be the same, otherwise the value will be replaced)

List saves the objects in the order they are entered, without sorting or editing operations.

Set accepts each object only once and uses its own internal sorting method (usually, you only care about whether an element belongs to the Set, not its order - otherwise you should use List ).

Map also saves a copy of each element, but this is based on "key". Map also has built-in sorting, so it does not care about the order in which elements are added. If the order of adding elements is important to you, you should use LinkedHashSet or LinkedHashMap.

Collection is a collection of objects. Collection has two sub-interfaces List and Set

List can be passed Subscript (1,2..) to get the value, the value can be repeated

, while Set can only get the value through the cursor, and the value cannot be repeated

ArrayList, Vector, and LinkedList are the implementation classes of List

ArrayList is thread-unsafe, and Vector is thread-safe. The underlying layers of these two classes are implemented by arrays

LinkedList is thread-unsafe, and the bottom layer is implemented by a linked list

Map is a collection of key-value pairs

HashTable and HashMap are the implementation classes of Map

HashTable is thread-safe and cannot store null values

HashMap is not thread-safe and can store null values

The above is what the editor introduces to you about Java I hope this knowledge about the use of List, Set and Map will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more articles about the use of List, Set and Map in Java, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn