কম্পিউটার

একটি সেট অন্য সেটের উপসেট কিনা তা পরীক্ষা করার জন্য জাভা প্রোগ্রাম


এই নিবন্ধে, আমরা বুঝতে পারব কিভাবে একটি সেট অন্য সেটের উপসেট কিনা তা পরীক্ষা করতে হয়। একটি সেট হল একটি সংগ্রহ যাতে ডুপ্লিকেট উপাদান থাকতে পারে না। এটি গাণিতিক সেট বিমূর্ততাকে মডেল করে। সেট ইন্টারফেসে শুধুমাত্র সংগ্রহ থেকে উত্তরাধিকারসূত্রে প্রাপ্ত পদ্ধতি রয়েছে এবং বিধিনিষেধ যোগ করে যে নকল উপাদান নিষিদ্ধ।

নীচে একই -

এর একটি প্রদর্শন রয়েছে৷

ধরুন আমাদের ইনপুট হল

First set: [90, 75, 60, 45]
Second set : [90, 60]

কাঙ্খিত আউটপুট হবে

Is a sets sub-set of the other?
true

অ্যালগরিদম

Step 1 - START
Step 2 - Declare namely
Step 3 - Define the values.
Step 4 - Create two Sets, and add elements to it using the ‘add’ method.
Step 5 - Display the Sets on the console.
Step 6 - Create a Boolean variable and call the ‘containsAll’ method on one set with respect to the other.
Step 7 - This checks if one set is a subset of the other.
Step 8 - If yes, it returns True, else False.
Step 9 - Display the result on the console.
Step 10 - Stop

উদাহরণ 1

এখানে, আমরা 'প্রধান' ফাংশনের অধীনে সমস্ত ক্রিয়াকলাপ একসাথে আবদ্ধ করি।

import java.util.HashSet;
import java.util.Set;
public class Demo {
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      Set<Integer> input_set_1 = new HashSet<>();
      input_set_1.add(45);
      input_set_1.add(60);
      input_set_1.add(75);
      input_set_1.add(90);
      System.out.println("The first set is defined as: " + input_set_1);
      Set<Integer> input_set_2 = new HashSet<>();
      input_set_2.add(60);
      input_set_2.add(90);
      System.out.println("The second set is defined as: " + input_set_2);
      boolean result = input_set_1.containsAll(input_set_2);
      System.out.println("\nIs a sets sub-set of the other? \n" + result);
   }
}

আউটপুট

The required packages have been imported
The first set is defined as: [90, 75, 60, 45]
The second set is defined as: [90, 60]

Is a sets sub-set of the other?
true

উদাহরণ 2

এখানে, আমরা ক্রিয়াকলাপগুলিকে অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিং প্রদর্শনকারী ফাংশনে অন্তর্ভুক্ত করি।

import java.util.HashSet;
import java.util.Set;
public class Demo {
   static void is_subset(Set<Integer> input_set_1, Set<Integer> input_set_2){
      boolean result = input_set_1.containsAll(input_set_2);
      System.out.println("\nIs a sets sub-set of the other? \n" + result);
   }
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      Set<Integer> input_set_1 = new HashSet<>();
      input_set_1.add(45);
      input_set_1.add(60);
      input_set_1.add(75);
      input_set_1.add(90);
      System.out.println("The first set is defined as: " + input_set_1);
      Set<Integer> input_set_2 = new HashSet<>();
      input_set_2.add(60);
      input_set_2.add(90);
      System.out.println("The second set is defined as: " + input_set_2);
      is_subset(input_set_1, input_set_1);
   }
}

আউটপুট

The required packages have been imported
The first set is defined as: [90, 75, 60, 45]
The second set is defined as: [90, 60]

Is a sets sub-set of the other?
true

  1. একটি বৃত্তের পরিধি খুঁজে পেতে জাভা প্রোগ্রাম

  2. জাভা প্রোগ্রাম একটি ট্রাপিজিয়াম এর এলাকা খুঁজে বের করতে

  3. জাভাতে একটি প্রোগ্রাম দ্বারা ব্যবহৃত মেমরি কিভাবে পরীক্ষা করবেন?

  4. জাভা প্রোগ্রাম JSlider এ সীমা নির্ধারণ করতে