Set Intersection in Java10 Sept 2024 | 3 min read In Java, Sets. intersection () method of Guava returns a non-modifiable view of the intersection of provided two sets. All the elements or values which are present in both sets will be returned. The order of iteration of both returned and first set will be similar. Syntax:Return Value: The above-mentioned method will return a non-modifiable view of the two sets, respectively. Let us understand about Set Intersection in detail with the help of few Java example programs. Example 1:Filename: Example1.java Output: First Set: [1, 2, 3] Second Set: [2, 3, 4] Intersection of Set 1 and Set 2: [2, 3] Explanation: In the above-mentioned Java example program, two sets of Integer data types are declared. The first set contains a few integer values in it. Similarly, the second set stores a few integer values in it. Using the retainAll() method, we are storing the intersection of both first and second set in the first set respectively. The result will be the values which are common in both first and second set. The result is being printed. Example 2:Filename: Example1.java Output: First Set: [a, b, c] Second Set: [b, e, z] Intersection of Set 1 and Set 2: [b] Explanation: In the above-mentioned Java example program, two sets of Character data types are declared. The first set contains few Character values in it. Similarly, the second set stores few Character values in it. Using the retainAll() method, we are storing the intersection of both first and second set in the first set respectively. The result obtained will be the values or elements common in both first and second set. The result obtained is being printed. |
A Java default keyword is an access modifier. If we do not assign any access modifier to variables, methods, constructors, and classes, by default, it is considered as default access modifier. The default keyword is a versatile and powerful tool that plays a crucial role...
10 min read
In Java8, the DoubleBinaryOperator interface came into existence. It returns a double value as the final result of an operation on two double values that it represents. It can be used as a method reference or as a lambda expression because it is a functional...
3 min read
The Knight's tour Problem in Java The Knight's Tour issue is a famous case of a backtracking algorithm. It entails moving a knight over a chessboard so that it visits each square precisely once. Given a (n times n) board and a starting location, the aim...
6 min read
Prime factorization is a fundamental concept in number theory that involves breaking down a composite number into its smallest prime factors. This process is invaluable in various fields of mathematics and computer science, including cryptography and number theory. In this section, we will explore how to...
4 min read
How to Increment & Decrement Date using Java? Changing dates, whether by incrementing them or decrementing them, is a typical operation in Java. It entails changing a date by adding or removing a specific number of days, weeks, months, or years. Thankfully, Java comes with libraries that...
4 min read
Java is a platform-independent programming language. It means that we can run Java on the platforms that have a Java interpreter. It is the reason that makes the Java platform-independent. The Java interpreter converts the Java bytecode (.class file) into the code understand by the operating...
3 min read
? Operator overloading is a programming concept that allows programmer to redefine the behavior of the standard mathematical operator (like, +, -, *, /). This concept is allowed in C++ and Python but not support in Java. Java does not support user-defined operator overloading. The only aspect...
6 min read
? Calculating the week number from the current date is a common requirement in various Java applications. Week number calculations are useful in scheduling, time tracking, payroll management, and many other scenarios. Java provides several ways to calculate the week number from the current date, using built-in...
4 min read
| File Transfer Using SFTP in Java JSCH In digital word transferring file client to server and vice-versa is a typical process because the size of file may be large or may be un authorize access. So, it become necessary to protect the file and data...
2 min read
Most of the time we need to compare two date and date-time objects. Date comparison is required when we want to get the data of some specific date and time from the database or to filter the returned data based on date and time. In order to...
3 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India