首页 > 编程语言 >Java读取txt文件内容并求交集并集差集对称差集合去重

Java读取txt文件内容并求交集并集差集对称差集合去重

时间:2023-07-18 20:12:50浏览次数:36  
标签:并求 Java String 并集 HashSet str2 str1 差集 new

Java读取两个txt内容并作集合运算

文件内容及格式

image

完整代码

package com.example.fortotest;


import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Objects;

public class Day3 {

    public static final String SPLIT_SYMBOL = ",";

    public static void main(String[] args) {
        try {
            // 将st1记为集合A str2文件记为集合B

            // 逗号连接的数字字符串
            String file1 = "E:\\str1.txt";
            File filePath1 = new File(file1);

            HashSet<Long> hashSet1 = new HashSet<>();
            BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(Files.newInputStream(filePath1.toPath())));
            String str1;
            while ((str1 = bufferedReader1.readLine()) != null) {
                String[] split = str1.split(SPLIT_SYMBOL);
                for (String s : split) {
                    hashSet1.add(Long.parseLong(s.trim()));
                }
            }


            // 以行划分的数字字符串
            String file2 = "E:\\str2.txt";
            File filePath2 = new File(file2);

            // 交集
            HashSet<Long> intersection = new HashSet<>();

            // 并集
            HashSet<Long> unionSet = new HashSet<>(hashSet1);

            // str2与str1的差集
            HashSet<Long> str2AndStr1DifferenceSet = new HashSet<>();


            String str2;
            BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(Files.newInputStream(filePath2.toPath())));
            while (!Objects.equals(str2 = bufferedReader2.readLine(), "") && str2 != null) {

                long l = Long.parseLong(str2.trim());
                if (hashSet1.contains(l)) {

                    // 交集
                    intersection.add(l);

                } else {

                    // 并集
                    unionSet.add(l);

                    // A对B的差集
                    str2AndStr1DifferenceSet.add(l);
                }
            }


            // 对称差集
            HashSet<Long> symmetricDifferenceSet = new HashSet<>(unionSet);
            symmetricDifferenceSet.removeAll(intersection);


            // str2对str1的差集
            HashSet<Long> str1AndStr2DifferenceSet = new HashSet<>(symmetricDifferenceSet);
            str1AndStr2DifferenceSet.removeAll(str2AndStr1DifferenceSet);


            System.out.printf("A∩B(交集) : %s%n", intersection);
            System.out.printf("A∪B(并集) : %s%n", unionSet);
            System.out.printf("A−B(str1与str2的差集) : %s%n", str1AndStr2DifferenceSet);
            System.out.printf("B-A(str2与str1的差集) : %s%n", str2AndStr1DifferenceSet);
            System.out.printf("A⊕B(对称差集) : %s%n", symmetricDifferenceSet);

            HashSet<Long> a = new HashSet<>(intersection);
            HashSet<Long> b = new HashSet<>(intersection);
            a.addAll(str1AndStr2DifferenceSet);
            b.addAll(str2AndStr1DifferenceSet);

            System.out.printf("集合A去重后 : %s%n", a);
            System.out.printf("集合B去重后 : %s%n", b);


        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
}

执行结果

image

标签:并求,Java,String,并集,HashSet,str2,str1,差集,new
From: https://www.cnblogs.com/yusishi/p/17544419.html

相关文章

  • Java异常机制
    Java异常机制什么是异常异常是指程序运行中出现的不期而至的各种状况,例如:文件找不到,网络连接失败,非法参数等异常的三种类型:检查型异常:最具代表的是用户错误或问题引起的异常运行时异常错误:错误不是异常,而是脱离程序员控制的问题异常体系结构Java把异常当作对象......
  • Java常用类
    Java常用类内部类概念:在一个类的内部再定义一个完整的类特点:1.编译后可以生成独立的字节码文件​ 2.内部类可以直接访问外部类的私有成员,而不破坏封装​ 3.可为外部类提供必要的功能组件//身体publicclassBody{privateStringname;//头部clas......
  • JAVA SE基础《七》 ---- JAVA案例
    目录一、案例一:买飞机票二、案例二:开发验证码三:案例三:评委打分四、案例四:数字加密五、案例五:数组拷贝六、案例六:抢红包七、案例七:找素数八、案例八:打印乘法表,打印三角形九、案例九:模拟双色球[拓展案例]1、业务分析、随机生成一组中奖号码2、让用户输入一组号码......
  • Java使用Stream函数对集合进行分组
    1List<Map<String,String>>list=newArrayList<>();2Map<String,String>map1=newHashMap<>();3map1.put("name","卢俊义");4map1.put("book","水浒传"......
  • java类型转换
    java类型转换-由于Java是强类型语言,所以在运算时,有时需要类型转换-低------------------------------------高-byte,short,char->int->long->float(小数的优先级大于整数)->double-运算中,不同类型的数据先转化为同一类型,然后进行运算-强制类型转换......
  • java后台启动jar包的一些命令
    启动方式一在jar包所在文件夹打开命令窗口,输入以下命令java-jarapp.jar特点:当前ssh窗口被锁定,可按CTRL+C打断程序运行,或直接关闭窗口,程序退出启动方式二java-jarapp.jar&&代表在后台运行。特定:当前ssh窗口不被锁定,但是当窗口关闭时,程序中止运行。启动方式三:no......
  • Java根据原始URL获取网络重定向后的URL
    方法1:/***获取重定向地址*@parampath原地址*@return*@throwsException*/privateStringgetRedirectUrl(Stringpath)throwsException{HttpURLConnectionconn=(HttpURLConnection)newURL(path)......
  • Java基本语法
    Java基本语法注释1单行注释2多行注释3文档注释//输出一个Helloworld单行注释/**多行注释**//***文档注释*@DescriptionHelloWorld*@AuthorDunCan*/标识符-Java所有的组成部分都需要名字。###标识符注意点1所有......
  • 设计模式-外观模式在Java中的使用示例
    场景外观模式外观模式是一种使用频率非常高的结构型设计模式,它通过引入一个外观角色来简化客户端与子系统之间的交互,为复杂的子系统调用提供一个统一的入口,降低子系统与客户端的耦合度,且客户端调用非常方便。示例自己泡茶和去茶馆喝茶的区别,如果是自己泡茶需要自行准备茶叶、......
  • java定时定时任务quartz
    Quartz是一个Java版开源定时调度器,功能强悍,使用方便。一、核心概念1.Job表示一个工作,要执行的具体内容,此接口只有一个方法voidexecute(JobExecutionContextcontext)2.JobDetailJobDetail表示一个具体的可以执行的调度程序,Job是这个可以执行程序所要执行的内容内容,JobDetai......