laitimes

Several methods of "Java" summation, intersection, difference, intersection, and Cartesian set merge intersecting difference complement Cartesian product

I believe that friends in the process of program development, more or less, will use set-related operations, such as: to find the union of sets, intersections, differences and so on. Today we will introduce to you several methods commonly used in Java to find unions, intersections, differences, complements, and Cartesian sets.

The several set operations introduced in the article mainly rely on the relevant methods in jdk, as well as the related methods in Apache's commons-collections4 library and Google's guava library. Apache and Google's library dependencies are as follows:

Several methods of "Java" summation, intersection, difference, intersection, and Cartesian set merge intersecting difference complement Cartesian product

A union is the sum of all the elements in two sets, such as: set 1 is:[1, 2], set 2 is:[3, 4], and the result of summation is:[1, 2, 3, 4]. A common way to find unions is as follows:

Several methods of "Java" summation, intersection, difference, intersection, and Cartesian set merge intersecting difference complement Cartesian product

Intersection is to find the elements of two sets, such as: set 1 is:[1, 2], set 2 is:[1, 3], the result after intersection is:[1], the common method of intersection is as follows:

Several methods of "Java" summation, intersection, difference, intersection, and Cartesian set merge intersecting difference complement Cartesian product

A difference set is the result of excluding elements from another set from one set (note: the result of set 1 difference set 2 and set 2 difference set 1 may be different), such as: set 1 is:[1, 2], set 2 is:[1, 3], set 1 difference set 2 results is:[2], set 2 difference set 1 results is: [3]. A common way to find a difference set is as follows:

Several methods of "Java" summation, intersection, difference, intersection, and Cartesian set merge intersecting difference complement Cartesian product

Intersection complement is to find two sets of elements that are different from each other, which can be understood as a complement of the union and intersection between two sets. For example, set 1 is:[1, 2], set 2 is:[1, 3], and the result of the complement is:[2, 3]. Common methods for intersecting complements are as follows:

Several methods of "Java" summation, intersection, difference, intersection, and Cartesian set merge intersecting difference complement Cartesian product

The Cartesian product is a set that combines each element between the sets, such as: set 1 is:[1, 2], set 2 is:[1, 3], and the result of the Cartesian set is:[[1, 1], [1, 3], [2, 1], [2, 3]. A common way to find a Cartesian set is as follows:

Several methods of "Java" summation, intersection, difference, intersection, and Cartesian set merge intersecting difference complement Cartesian product

The various methods listed above can meet the requirements of the calculation, and the specific difference is that some methods are to directly update the calculation results into the input object, and the return value is a Boolean value indicating whether the input object has changed; some methods do not modify the input object, but return the calculation result as a new object. Choosing which method to use is mainly based on the context of the code and some of your own coding styles.

Finally, it should be mentioned that the objects stored in the collection of examples in this article are all integers, but in the specific code, the collection may be stored in other custom class objects, so you need to pay attention to the need to override the hashcode() and equals() methods, because many set operations rely on the return value of these two methods, if not overwritten correctly, it may lead to errors in the calculation result.

------ the end ------

Read on