Java中两个大小不相同的数组列表如何比较大小和打印差异

如何解决Java中两个大小不相同的数组列表如何比较大小和打印差异

我在下面的程序中使用了两个比较两个数组列表的方法,但是我不想错过当两个数组列表的大小相同但包含不同值的情况。下面的代码也可以最小化。

ArrayList<String> firstList = new ArrayList<String> ( Arrays.asList("Cake","pizza","pasta") );
ArrayList<String> secondList = new ArrayList<String> ( Arrays.asList("Chocolate","fruits","pancake"));

if (!firstList.equals(secondList)) {
    if (firstList.size() > secondList.size()) {
        firstList.removeAll(secondList);
        SOP("Differance" + firstList+ "---------Total Number of Group mismatch----------"+ firstList.size());
    } else if (firstList.size() < secondList.size()) {
        secondList.removeAll(firstList);
        SOP("Differance" + secondList+ "---------Total Number of Group mismatch----------" + secondList.size());
    }
} else {
    SOP("Both are same");
}

解决方法

如果您要使用第三方实用程序,则可以直接使用

System.out.println(CollectionUtils.disjunction(firstList,secondList));

为最大限度地减少简单性,请使用下面的第三个列表

public static void main(String[] args) {
        ArrayList<String> firstList = new ArrayList<String> ( Arrays.asList("Cake","pizza","pasta","fruits") );
        ArrayList<String> secondList = new ArrayList<String> ( Arrays.asList("Chocolate","fruits","pancake"));         
        List<String> result = new ArrayList<String>();
        add(secondList,result);
        add(firstList,result);
        System.out.println(result);
    }

    public static void add(ArrayList<String> list,List<String> result) {
        for (String string : list) {
            if(result.contains(string)) {
                result.remove(string);
            }else {
                result.add(string);
            }
        }
    }

或者如果您可以修改您的列表(任何一个)都可以 您可以迭代一个列表,然后检查另一列表中的数据

for (String string : firstList) {
            if(secondList.contains(string)) {
                secondList.remove(string);
            }else {
                secondList.add(string);
            }
        }
        System.out.println(secondList);
,

这不是一种有效的解决方案,但是它可以满足您的需求,也可能满足您的期望:

/* Component Needs */

.pc-tab > input {
    /* opacity: 0; */
    visibility: hidden;
    position: absolute;
    
    }
    
.pc-tab .tab_content > div {
  /* display: none; */
    display: block;
    position: absolute;
    }

#tab1_content {
    z-index: 99;
    }

#tab2_content {
    z-index: 98;
    }
    
#tab3_content {
    z-index: 98;
    }
    
#tab4_content {
    z-index: 98;
    }

    
#tab1:target ~ .tab_container .tab1,#tab2:target ~ .tab_container .tab2,#tab3:target ~ .tab_container .tab3,#tab4:target ~ .tab_container .tab4{
  /* display: block; */
    z-index: 100; /* 101 */
    }  
    
#tab1:checked ~ .tab_container .tab1,#tab2:checked ~ .tab_container .tab2,#tab3:checked ~ .tab_container .tab3,#tab4:checked ~ .tab_container .tab4{
  /* display: block; */
    z-index: 101;
    }
    

/* tab visual function */
    
.tabs nav .tab1 label {
    background: transparent;
    }    

#tab1:target ~ .tabs nav .tab1 label,#tab2:target ~ .tabs nav .tab2 label,#tab3:target ~ .tabs nav .tab3 label,#tab4:target ~ .tabs nav .tab4 label {   
    background: transparent;
    }
    
#tab1:target ~ .tabs nav li:not(.tab1) label,#tab2:target ~ .tabs nav li:not(.tab2) label,#tab3:target ~ .tabs nav li:not(.tab3) label,#tab4:target ~ .tabs nav li:not(.tab4) label {   
    background: #eeeeee;
    }

#tab1:checked ~ .tabs nav li:not(.tab1) label,#tab2:checked ~ .tabs nav li:not(.tab2) label,#tab3:checked ~ .tabs nav li:not(.tab3) label,#tab4:checked ~ .tabs nav li:not(.tab4) label {   
    background: #eeeeee !important;
    }

    
    

#tab1:checked ~ .tabs nav .tab1 label,#tab2:checked ~ .tabs nav .tab2 label,#tab3:checked ~ .tabs nav .tab3 label,#tab4:checked ~ .tabs nav .tab4 label {
    background: transparent !important;
    color: #111;
    position: relative;
    }
    
#tab1:checked ~ .tabs nav .tab1 label:after,#tab2:checked ~ .tabs nav .tab2 label:after,#tab3:checked ~ .tabs nav .tab3 label:after,#tab4:checked ~ .tabs nav .tab4 label:after {
    background: transparent !important;
    }
       
label {
    cursor: pointer;
    }    
    


/* Visual Styles */

$activeColor: transparent; /* #ffffff */
$unactiveColor: #eeeeee;
$unactiveHoverColor: red; /* #dddddd */
$offsetHeight: 50px;


/* *,*:after,*:before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
} */

.pc-tab *,.pc-tab *:after,.pc-tab *:before {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}


.pc-tab { 
    width: 100%;
    /* max-width: 700px; */
    margin: 0 auto;
    }

.tabs ul {
    list-style: none;
    margin: 0;
    padding: 0;
    }

    
.tabs ul li label {
    /* font-family: "Raleway"; */
    font-size: 15px;
    line-height: 21px;
    
    float: left;
    padding: 15px 10px;
    border: 1px solid #ddd;
    border-bottom: 0;
    background: #eee; }};
    color: #444;
    }
    
    
    


    
#over #tab1 ~ .tabs nav .tab1 label:hover,#over #tab2 ~ .tabs nav .tab2 label:hover,#over #tab3 ~ .tabs nav .tab3 label:hover,#over #tab4 ~ .tabs nav .tab4 label:hover,#over .tabs ul li label:hover,label:hover {
    background: red !important;
    }


.tabs ul li label:active {
    background: transparent;
    }   

.tabs ul li label:not(:last-child) {
    border-right-width: 0;
    }
  
    
.tab_content {
    clear: both;
    }
    
.tab_content div {
      padding-bottom: 20px;
      padding-right: 20px;
      padding-left: 20px;
      padding-top: calc(20px + 50px);
      
      width: 100%;
      border: 1px solid #ddd;
      /* background: #fff; */
      line-height: 1.5em;
      letter-spacing: 0.3px;
      color: #444;
        
    box-sizing: border-box;
    }
    
.tab_content div h2 {
    margin: 0;
    font-family: "Raleway";
    letter-spacing: 1px;
    color: #34495e;
    }



label {
    width: calc(100% / 4);
    box-sizing: border-box;
    text-align: center;
    height: 50px;
    }

nav {
    }
    
.tab_content .tab1 {
  background-color: pink;
  }

.tab_content .tab2 {
  background-color: lightblue;
  }

.tab_content .tab3 {
  background-color: lightyellow;
  }

.tab_content .tab4 {
  background-color: lightgreen;
  }    
    
    

.tabs {
    height: 100%;
    display: table;
    width: 100%;
    margin-bottom: -50px;
    position: relative;
    z-index: 2;
    }
    
.tabs li {
    margin-bottom: 0;
    }

.tab_container {
    width: 100%;
    height: 0;
    padding-bottom: calc((100% / 16) * 9);
    position: relative;
    z-index: 1;
    }

.tab_content {
    position: absolute;
    height: 100%;
    width: 100%;
    }

.tab_content div {
    height: 100%;
    }
   
    
.title {
    padding: 0;
    }

输出:

<div style="width: 700px; margin: 0 auto;">
  <ul>
    <li><a href="#tab1">Link to First Tab</a></li>
    <li><a href="#tab2">Link to Second Tab</a></li>
    <li><a href="#tab3">Link to Third Tab</a></li>
    <li><a href="#tab4">Link to Fourth Tab</a></li>
  </uL> 
</div>

<div style="height:300px;"></div>


<!-- 
ORIGINAL BY RENA.TO 
https://codepen.io/renatorib/pen/rlpfj
-->

        <div class="pc-tab" id="over">
    
<input id="tab1" type="radio" name="pct" />
<input id="tab2" type="radio" name="pct" />
<input id="tab3" type="radio" name="pct" />
<input id="tab4" type="radio" name="pct" />  
  
<div class="tabs">
  <nav>
    <ul>
      <li class="tab1">
        <label for="tab1">First Tab</label>
      </li>
      <li class="tab2">
        <label for="tab2">Second Tab</label>
      </li>
      <li class="tab3">
        <label for="tab3">Third Tab</label>
      </li>
      <li class="tab4">
        <label for="tab4">Fourth Tab</label>
      </li>
    </ul>
  </nav>
</div>   

  
<div class="tab_container">
  <div class="tab_content">
    
    <div class="tab1 tab_block" id="tab1_content">
      <p class="title">First</p>
      <p>Lorem ipsum dolor sit amet,consectetur adipisicing elit. Repellendus itaque quidem minus nostrum,voluptatem accusamus aspernatur quia harum ratione,officia laudantium inventore autem doloribus atque labore numquam non. Hic,animi.</p>
    </div>
    
    <div class="tab2 tab_block" id="tab2_content">
      <p class="title">Second</p>
      <p>Lorem ipsum dolor sit amet,consectetur adipisicing elit. Laborum nesciunt ipsum dolore error repellendus officiis aliquid a,vitae reprehenderit,accusantium vero,ad. Obcaecati numquam sapiente cupiditate. Praesentium eaque,quae error!</p>
    </div>
    
    <div class="tab3 tab_block" id="tab3_content">
      <p class="title">Third</p>
      <p>Lorem ipsum dolor sit amet,consectetur adipisicing elit.</p>
    </div>
    
    <div class="tab4 tab_block" id="tab4_content">
      <p class="title">Fourth</p>
      <p> afdadfadfadfad Lorem ipsum dolor sit amet,consectetur adipisicing elit. Optio,nobis culpa rem,vitae earum aliquid.</p>
    </div>
    
  </div>
</div>        

  
        </div>


<div style="height:1000px;"></div>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-