如何解决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 举报,一经查实,本站将立刻删除。