[Java] Set structure of collection class (about HashSet and TreeSet)

Programming study diary

October 15, 2020 I used TreeSet in Java, but I didn't understand the difference from HashSet, so I record it.

What is a collection class?

A collection is an object that collects elements. Collection classes are roughly divided into three types: List, Map, and Set. Each is divided into classes with different properties.

There are two types of List structure, ArrayList and LinkedList. The List structure is a structure in which elements are ordered and managed, and since the elements are arranged in the order of index numbers, elements can be acquired, inserted, updated, changed, etc. by specifying the numbers.

There are two types of Map structures, HashMap and TreeMap. The Map structure is a data structure that manages a set of keys and values as one element, and since the elements are managed as keys, you can specify the key and not update or delete the value. The HashSet and TreeSet handled this time have a Set structure as the name implies, and are summarized below.

What is a collection class with a Set structure?

There are two types of Set structures, HashSet and TreeSet, which are data structures that manage elements without ordering them. Since there is no ordering like List and key management like Map, iterator or extended for statement is used to get the elements. Elements cannot be duplicated </ b> (If the same key is set, it will be overwritten.) The differences between HashMap and TreeSet can be summarized as follows.

--HashMap does not matter the order of acquisition --TreeSet can get elements in sorted order --HashMap can't handle null --TreeSet can handle null

HashMap does not guarantee the order of element acquisition, but TreeSet is automatically sorted and managed, so elements can be acquired in the sorted order. Also, HashMap can use null for elements, but TreeSet can use null.

Summary

Not only the Set structure but also the features of the collection class are easily summarized in a table.

ArrayList LinkedList HashMap TreeMap HashSet TreeSet
interface List List Map Map Set set
Duplicate elements × × × ×
null element × × ×
Automatic sorting × × ×

Although not mentioned above, ArrayList is characterized by fast acquisition and slow insertion and deletion, and LinkedList is characterized by fast element insertion and deletion but slow acquisition.

References

TreeSet class Comparison of collection classes

Recommended Posts

[Java] Set structure of collection class (about HashSet and TreeSet)
[Java] Inheritance and structure of HttpServlet class
[Java] Comparator of Collection class
About fastqc of Biocontainers and Java
Mechanism and characteristics of Collection implementation class often used in Java
[Java] Contents of Collection interface and List interface
About Java class
About next () and nextLine () of the Scanner class
Expired collection of java
About class division (Java)
About Java StringBuilder class
[Java] About Singleton Class
About Java String class
Java programming (class structure)
About java abstract class
[Java] About Objects.equals () and Review of String comparisons (== and equals)
Use of Abstract Class and Interface properly in Java
[Java] Handling of character strings (String class and StringBuilder class)
About an instance of java
About Java class loader types
Java class definition and instantiation
[Java] About String and StringBuilder
Recommendation of set operation by Java (and understanding of equals and hashCode)
Summary of Java Math class
About Java class variables class methods
Advantages and disadvantages of Java
About Java Packages and imports
[Java] I thought about the merits and uses of "interface"
About miscellaneous impressions of "Testing Java Microservices" and Consumer Driven Contract
Outputs a list of CDK descriptor, class name, and 3D structure descriptor.
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
Handle business logic for a set of Entity in Java class
Collection of programming selection tasks to make and remember (Java basics)
Read the first 4 bytes of the Java class file and output CAFEBABE
Various methods of Java String class
About Lambda, Stream, LocalDate of Java8
[Java beginner] About abstraction and interface
StringBuffer and StringBuilder Class in Java
[Java] Judgment of identity and equivalence
About removeAll and retainAll of ArrayList
About Java primitive types and reference types
This and that about Base64 (Java)
Basic structure of Java source code
A collection of phrases that impresses the "different feeling" of Java and JavaScript
About the classification and concept of Immutable / Mutable / Const / Variable of Java and Kotlin.
Java beginner escape boot camp Part 1 Java class structure and how to write
Display Japanese calendar and days of the week using java8 standard class
Significance of interface learned from Java Collection
After 3 months of Java and Spring training
About the operation of next () and nextLine ()
[Java] Differences between instance variables and class variables
[About JDBC that connects Java and SQL]
[Java / Swift] Comparison of Java Interface and Swift Protocol
[Java] Collection and StringBuilder operation method comparison
[Java beginner] About initialization of multidimensional array
[Basic knowledge of Java] About type conversion
Proper use of interface and abstract class
Java programming (static clauses and "class variables")
java Calendar class (time set, comparison, format)
About the mechanism of the Web and HTTP
Summary of Java Math.random and import (Calendar)