用java实现在 对两个文件数据取交集、并集、差集

用java实现在 对两个文件数据取交集、并集、差集

  • 以上代码首先读取了两个文件中的数据,然后使用 Java 集合类 HashSet 来存储集合中的元素。接着,我们使用 retainAlladdAllremoveAll 等方法来对两个集合进行操作,求它们的交集、并集和差集。最后,我们将结果输出到控制台。
  • 需要注意的是,在具体实现时,可能会遇到一些异常情况,比如文件不存在、IO异常等等,需要适当地进行处理。此外,如果要处理大规模的数据,可能需要考虑使用更高效的算法和数据结构来提高程序的性能。

代码:

import java.io.*;
import java.util.*;

public class SetOperations {

    public static void main(String[] args) throws IOException {
        // 读取文件1和文件2中的数据
        Set<String> set1 = readDataFromFile("file1.txt");
        Set<String> set2 = readDataFromFile("file2.txt");

        // 对两个集合进行操作,求它们的交集、并集和差集
        Set<String> intersection = new HashSet<>(set1);
        intersection.retainAll(set2);

        Set<String> union = new HashSet<>(set1);
        union.addAll(set2);

        Set<String> difference1 = new HashSet<>(set1);
        difference1.removeAll(set2);

        Set<String> difference2 = new HashSet<>(set2);
        difference2.removeAll(set1);

        // 输出结果
        System.out.println("Intersection of set1 and set2:");
        System.out.println(intersection);

        System.out.println("Union of set1 and set2:");
        System.out.println(union);

        System.out.println("Difference of set1 and set2:");
        System.out.println(difference1);

        System.out.println("Difference of set2 and set1:");
        System.out.println(difference2);
    }

    /**
     * 从文件中读取数据到一个集合中
     */
    public static Set<String> readDataFromFile(String filename) throws IOException {
        Set<String> set = new HashSet<>();
        File file = new File(filename);
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = reader.readLine()) != null) {
            set.add(line.trim());
        }
        reader.close();
        return set;
    }
}

package li.dong;

import java.io.*;
import java.sql.SQLOutput;
import java.util.*;

/**
 * 用java实现在 对两个文件数据取交集、并集、差集
 */
public class SetOperations {
    public static void main(String[] args) throws IOException {
        // 读取文件1和文件2中的数据
        Set<String> set1=readDataFromFile("D:\\CS\\Code\\IdeaCode\\Database_Test001\\src\\li\\dong\\file1.txt");
        Set<String> set2=readDataFromFile("D:\\CS\\Code\\IdeaCode\\Database_Test001\\src\\li/dong/file2.txt");

        // 对两个集合进行操作,求它们的交集、并集和差集
        //交集
        Set<String> intersection=new HashSet<>(set1);
        intersection.retainAll(set2);

        //并集
        Set<String> union=new HashSet<>(set2);
        union.addAll(set2);

        //差集
        Set<String> difference1=new HashSet<>(set1);
        difference1.removeAll(set2);

        Set<String> difference2=new HashSet<>(set2);
        difference2.removeAll(set1);

        //输出结果
        System.out.print("文件1和文件2的交集为:");
        System.out.println(intersection);

        System.out.print("文件1和文件2的并集为:");
        System.out.println(union);

        System.out.print("文件1和文件2的差集为:");
        System.out.println(difference1);

        System.out.print("文件2和文件1的差集为:");
        System.out.println(difference2);
    }


    /**
     * @order:从文件中读取数据到一个集合中
     * @param fileName
     * @return
     * @throws IOException
     */
    public static Set<String> readDataFromFile(String fileName)throws IOException{
        Set<String> set=new HashSet<>();
        File file=new File(fileName);
        BufferedReader bufferedReader=new BufferedReader(new FileReader((file)));
        String line="";
        while ((line=bufferedReader.readLine())!=null){
            set.add(line.trim());
        }
        bufferedReader.close();
        return  set;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

captain_dong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值