Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:16:07+00:00 2026-06-12T14:16:07+00:00

Can anyone help me? example A {1,2,3} B {1,4,5} Code snippet: a.intersect(b).print() // Result

  • 0

Can anyone help me?
example

  • A {1,2,3}
  • B {1,4,5}

Code snippet:

a.intersect(b).print()
// Result 1 . twin between two object

a.merge(b).print()
// Result 1,2,3,4,5

It is valid if I write code below?
If not, which part I have to fix?

public static void main(String[] args) {
    // TODO code application logic here
   Set<Integer> a = new TreeSet<Integer>();
   a.add(1);
   a.add(2);
   a.add(6);
   a.remove(2);
   a.add(1); //gak berpengaruh karena sudah ada yang 1 sebelumnya
   //mengapa begituu ? karena et adalah collection yang tidak dapat memiliki elemen kembar.
   System.out.println("A = " + a);

   Set<Integer> b = new TreeSet<Integer>();
   b.add(2);
   b.add(6);
   b.add(1);
   System.out.println("B = " + b); //hasilnya 1,2,6.
   //loh?? kok bisa ? krn sy pake TreeSet, jadi udah terurut.

   b.retainAll(a); //ini rumus intersect XD
   for (Integer i: b)
   {
       System.out.print(i);
   }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:16:08+00:00Added an answer on June 12, 2026 at 2:16 pm

    Like this:

    import java.util.*;
    Set<Integer> a = new HashSet<Integer>();
    a.add( 1);
    a.add( 2);
    a.add( 3);
    

    Or adding from an Array/ or multiple literals; wrap to a list, first.

    Integer[] array = new Integer[]{ 1, 4, 5};
    Set<Integer> b = new HashSet<Integer>();
    b.addAll( Arrays.asList( b));         // from an array variable
    b.addAll( Arrays.asList( 8, 9, 10));  // from literals
    

    To get the intersection:

    // copies all from A;  then removes those not in B.
    Set<Integer> r = new HashSet( a);
    r.retainAll( b);
    // and print;   r.toString() implied.
    System.out.println("A intersect B="+r);
    

    Hope this answer helps. Vote for it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can anyone help me out with the syntax and example for the target server
Can anyone of you help me with an example of how to override symfony's
Can anyone please provide me with an example that can help me to understand
I got an issue which is, in my code,anyone can help will be great.
Can anyone share a code snippet or direct me to some discussions as to
Can anyone help me for a Java code which copy or move one folder
Can anyone help - this is driving me mad. I am calling a mysql
Can Anyone help me why x2 prints zero. I guess because of floating point
Can anyone help me how to split /explode verse format to 3 parts? The
Can anyone help me to get good WordPress interview questions and answers. Any link

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.