Академический Документы
Профессиональный Документы
Культура Документы
System.out.println(hash_Set);
Output:
Set output without the duplicates[Geeks, Example, For, Set]
Sorted Set after passing into TreeSet[Example, For, Geeks, Set]
Note: As we can see the duplicate entry “Geeks” is ignored in the final output, Set
interface doesn’t allow duplicate entries.
Now we will see some of the basic operations on the Set i.e. Union, Intersection and
Difference.
Let’s take an example of two integer Sets:
[1, 3, 2, 4, 8, 9, 0]
[1, 3, 7, 5, 4, 0, 7, 5]
Union
In this, we could simply add one Set with other. Since the Set will itself not allow any
duplicate entries, we need not take care of the common values.
Expected Output:
Union : [0, 1, 2, 3, 4, 5, 7, 8, 9]
Intersection
We just need to retain the common values from both Sets.
Expected Output:
Intersection : [0, 1, 3, 4]
Difference
We just need to remove all the values of one Set from the other.
Expected Output:
Difference : [2, 8, 9]
// To find union
Set<Integer> union = new HashSet<Integer>(a);
union.addAll(b);
System.out.print("Union of the two Set");
System.out.println(union);
// To find intersection
Set<Integer> intersection = new HashSet<Integer>(a);
intersection.retainAll(b);
System.out.print("Intersection of the two Set");
System.out.println(intersection);
Operators in java
Operator in java is a symbol that is used to perform operations. For example: +, -, *, /
etc.
There are many types of operators in java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Arithmetic multiplicative * / %
additive + -
equality == !=
bitwise exclusive OR ^
bitwise inclusive OR |
logical OR ||
Ternary ternary ? :
Output:
10
12
12
10
Output:
22
21
Output:
-11
9
false
true
Java Arithmetic Operators
Java arithmatic operators are used to perform addition, subtraction, multiplication, and
division. They act as basic mathematical operations.
Output:
15
5
50
2
0
Output:
21
Output:
40
80
80
240
Output:
2
5
2
Output:
5
5
-5
1073741819
The bitwise & operator always checks both conditions whether first condition is true or false.
1. class OperatorExample{
2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. int c=20;
6. System.out.println(a<b&&a<c);//false && true = false
7. System.out.println(a<b&a<c);//false & true = false
8. }}
Output:
false
false
Output:
false
10
false
11
Java OR Operator Example: Logical || and Bitwise |
The logical || operator doesn't check second condition if first condition is true. It checks
second condition only if first one is false.
The bitwise | operator always checks both conditions whether first condition is true or false.
1. class OperatorExample{
2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. int c=20;
6. System.out.println(a>b||a<c);//true || true = true
7. System.out.println(a>b|a<c);//true | true = true
8. //|| vs |
9. System.out.println(a>b||a++<c);//true || true = true
10. System.out.println(a);//10 because second condition is not checked
11. System.out.println(a>b|a++<c);//true | true = true
12. System.out.println(a);//11 because second condition is checked
13. }}
Output:
true
true
true
10
true
11
Output:
2
Another Example:
1. class OperatorExample{
2. public static void main(String args[]){
3. int a=10;
4. int b=5;
5. int min=(a<b)?a:b;
6. System.out.println(min);
7. }}
Output:
Output:
14
16
Output:
13
9
18
9
Output:
1. class OperatorExample{
2. public static void main(String args[]){
3. short a=10;
4. short b=10;
5. a=(short)(a+b);//20 which is int now converted to short
6. System.out.println(a);
7. }}
Output:
20