Java中两个集合的并集 [英] union of two set in java

查看:85
本文介绍了Java中两个集合的并集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我需要在java中基于集合理论将两个集合并集
例如A = {1,2,3,4,5}
B = {1,2,3,6,7}
UNION:C = {1,2,3,4,5,6,7}

我编码了一种方法,但是结果是错误的
请帮我!
我的代码:

public char[] union(char a[],char b[],int n,int m){
    int count=0;
    for(int i=0;i<a.length;i=i+2){
       for(int j=0;j<b.length;j=j+2){
       if(a[i]==b[j]){
           ++count;
       }}}
      c=new char [(n*2+m*2)-count*2];
 int j=0,h=0;
 for(int i=0;i<a.length;++i){
     c[i]=a[i];
     ++j;}
  
       for(;j<c.length;j=j+2){
           for(int i=0;i<a.length;i=i+2){
               for(int k=0;k<b.length;k=k+2){
               if(c[i]==b[k]){
                   continue;}
           else
              c[j]= b[k];  }  }
                   
   }
 return c;

解决方案

您可以尝试执行以下操作:

 公共  class 程序{

    私有 静态 布尔 contains( char  []数组, char 值){
         for (字符 item:数组){
            如果(项目==值)
                返回 true;
        }
        返回 false;
    }

    公共 静态  char  [] union( char  [] a, char  [] b){

        列表<字符>结果=  ArrayList< Character>();

         (字符值:a){
            如果(包含(b,值)){
                result.add(value);
            }
        }

        字符 [] resultArray =  字符 [ result.size()];
         for ( int  i =  0 ; i < result.size(); ++ i){
            resultArray [i] = result.get(i);
        }

        返回 resultArray;
    }


    公共 静态  void  main(字符串 [] args){

        字符 a [] =  字符 [ ] { 1  2  3  4  5 };
        字符 b [] =  字符 [ ] { 1  2  3  6  7 };

         for (字符 c:union(a,b)){
            System.out.print(( int )c);
        }
    }

} 




/Fredrik


为什么不利用Java的Available属性执行联合操作?

请按照以下步骤操作:

*导入 java.uitl.*

*使用以下方法声明2套.

 Set setName = new HashSet();
setName.add("value"); 



*使用以下方法查找联合

 Set unionSetName = new TreeSet(setName1);
unionSetName.addAll(setName2);



*使用迭代器在联合集中打印值

 Iterator it = unionSetName.iterator();
while(it.hasNext())
{
   System.out.println(in.next());
}



请查看下面的 Set 界面文档链接.
设置界面 [ addAll [ 解决方案

You could try something like this:

public class Program {

    private static boolean contains(char[] array, char value) {
        for(char item : array) {
            if (item == value)
                return true;
        }
        return false;
    }

    public static char[] union(char[] a, char[] b) {

        List<Character> result = new ArrayList<Character>();

        for(char value : a) {
            if (contains(b, value)) {
                result.add(value);
            }
        }

        char[] resultArray = new char[result.size()];
        for(int i = 0; i < result.size(); ++i) {
            resultArray[i] = result.get(i);
        }

        return resultArray;
    }


    public static void main(String[] args) {

        char a[] = new char[] { 1, 2, 3, 4, 5};
        char b[] = new char[] { 1, 2, 3, 6, 7};

        for(char c : union(a, b)) {
            System.out.print((int)c);
        }
    }

}




/Fredrik


Why don''t you make use of the Available property of java to perform the Union Operation ?

Follow the Below Steps :

* Import java.uitl.*

* Declare 2 Sets using the below methods.

 Set setName = new HashSet();
setName.add("value"); 



* Find the Union Using the below method

 Set unionSetName = new TreeSet(setName1);
unionSetName.addAll(setName2);



* Print the Values in the Union Set using Iterator

 Iterator it = unionSetName.iterator();
while(it.hasNext())
{
   System.out.println(in.next());
}



Take a Look at the below link for the Documentation of Set interface.
Set Interface[^]


BR//
Harsha


The best way to solve this problem is to use addAll[^] method of Set<T>: add elements of a and b to HashSet<char>, then call aSet.addAll(bSet); If you need the result to be sorted, use TreeSet instead of HashSet.

P.S. Your code is grossly wrong: you are skipping odd-numbered elements for some reason (you do i = i+2 instead of i++ everywhere). If you insist on doing the union in your own code, sort both arrays before you do it: after that, you can merge everything in a single loop.


这篇关于Java中两个集合的并集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆