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

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
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools