Home  >  Article  >  Java  >  The implementation and difference between Map and Set in Java collection framework

The implementation and difference between Map and Set in Java collection framework

王林
王林Original
2024-04-12 22:12:021116browse

The difference between Map and Set: key value and uniqueness: Map stores key-value pairs, and Set stores unique elements. Order: Among Map and Set, HashMap and HashSet are unordered sets, LinkedHashMap and LinkedHashSet are ordered sets, and TreeSet is sorted in order. Mutability: Map, LinkedHashMap and TreeSet are mutable collections, HashSet and LinkedHashSet are immutable collections. Purpose: Map is used for key-value pair data, and Set is used for unique element data.

The implementation and difference between Map and Set in Java collection framework

Implementation and differences of Map and Set in Java collection framework

Introduction

Java The collection framework provides various data structures, the two most common of which are Map and Set. This article will delve into the differences between the implementation, features, and uses of Map and Set.

Map implementation

Map is a data structure that stores key-value pairs. Various implementations such as HashMap, LinkedHashMap and TreeMap are provided.

// 创建 HashMap
Map<String, Integer> ages = new HashMap<>();
ages.put("John", 25);  // 添加键值对
ages.get("John");  // 获取与 John 关联的值

Set Implementation

Set is a data structure that stores unique elements. It has implementations such as HashSet, LinkedHashSet and TreeSet.

// 创建 HashSet
Set<String> names = new HashSet<>();
names.add("Alice");  // 添加元素
names.contains("Alice");  // 检查元素是否存在

Feature differences

  • Key value and uniqueness: Map stores key-value pairs, where the key must be unique. Set stores unique elements.
  • Order: HashMap and HashSet are unordered collections, and the order of elements is not guaranteed. LinkedHashMap and LinkedHashSet are ordered collections that maintain the insertion order of elements. TreeSet sorts elements in natural order.
  • mutability: HashMap, LinkedHashMap and TreeSet are mutable collections. HashSet and LinkedHashSet are immutable collections and do not support addition or deletion operations.

Differences in usage

  • Map: Key-value pairs used to store related data, such as name mapping to age.
  • Set: Used to store unique elements, such as a set of student names or a list of completed tasks.

Practical case

// 使用 Map 存储学生姓名和分数
Map<String, Integer> scores = new HashMap<>();
scores.put("Bob", 90);
scores.put("Alice", 85);

// 使用 Set 存储一组国家
Set<String> countries = new HashSet<>();
countries.add("USA");
countries.add("India");
countries.add("China");

Conclusion

Map and Set are powerful data structures in the Java collection framework , used to process different types of data. It is crucial to understand their characteristics and uses in order to use them effectively in real projects.

The above is the detailed content of The implementation and difference between Map and Set in Java collection framework. For more information, please follow other related articles on 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