java - How to write method that returns hash set of common string -


how create new hash set combines common string values (case sensitive) 2 other sets?

the main method contains:

    public static void main(string[] args) {     set<string> set1 = new hashset<string>();     set<string> set2 = new hashset<string>();     set1.add("blue");     set1.add("red");     set1.add("yellow");     set2.add("blue");     set2.add("red");     set2.add("orange"); } 

the method header is:

 public static set<string> buildlist (set<string>set1, set<string>set2){  set<string> set3 = new hasset<string>();  } 

if understood question correctly need retain common values both hashset if yes use set1.retainall(set2)

code:

public static void main(string[] args) {         set<string> set1 = new hashset<string>();         set<string> set2 = new hashset<string>();         set1.add("blue");         set1.add("red");         set1.add("yellow");         set2.add("blue");         set2.add("red");         set2.add("orange");          set1.retainall(set2);         system.out.println(set1);     } 

output:

[red, blue] 

you can modify buildlist method mentioned below returns common list of string result.

 public static set<string> buildlist (set<string>set1, set<string>set2){    set1.retainall(set2);    return set1;  } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -