PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 7, 2022

[FIXED] How to creat a set with elements from either set 1 or set 2 or both?

 November 07, 2022     java, set     No comments   

Issue

Hey I wanted to ask how I can add elements in a new set that either are in my set a or set b or both sets. This is what I have until now. Would be nice of you could help me.

public static Set<String> vereinigung(Set<String> a, Set<String> b) {
        Set<String> setForAB = new HashSet<String>();

        //add elements from a
        setForAB.add(String.valueOf(a));

        //add elements from a
        setForAB.add(String.valueOf(b));

        //elements from both sets
        setForAB.addAll(a);
        setForAB.addAll(b);

        return setForAB;
    }

Solution

You are on right track, removed unncessary code from your solution. This should suffice.

public static Set<String> vereinigung(Set<String> a, Set<String> b) {
        Set<String> setForAB = new HashSet<String>();
        //add elements from both sets
        setForAB.addAll(a);
        setForAB.addAll(b);
        return setForAB;
    }


Answered By - Jitendra
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing