Java Enzanisa Rondedzero Mbiri

Iyo Rondedzero interface muJava inopa nzira dzekukwanisa kuenzanisa maList maviri uye nekuwana zvakajairika uye zvisipo zvinhu kubva pazvinyorwa.

Enzanisa zvinyorwa zviviri zvisina kurongwa zvekuenzana

Kana iwe uchida kutarisa kuti zvinyorwa zviviri zvakaenzana, i.e. zvine zvinhu zvakafanana uye uye zvinowoneka mune imwechete indekisi ipapo tinogona kushandisa:

import java.util.Arrays; import java.util.List; public class CompareTwoLists {
public static void main(String[] args) {
List listOne = Arrays.asList('a', 'b', 'c');
List listTwo = Arrays.asList('a', 'b', 'c');
List listThree = Arrays.asList('c', 'a', 'b');

boolean isEqual = listOne.equals(listTwo);
System.out.println(isEqual);

isEqual = listOne.equals(listThree);
System.out.println(isEqual);
} }

Kubuda:

true false

Sezvauri kuona iyo _ _ + _ | nzira inofananidza zvinhu nenzvimbo yadzo mune irwo runyorwa.

Inoenderana:

Enzanisa zvinyorwa zviviri zvakarongwa

Pane zvinyorwa zviviri zvine zvinhu zvakafanana?



Kuenzanisa zvinyorwa zviviri zvekuenzana chete maererano nezvinhu zvisinei nenzvimbo yadzo, tinofanirwa kushandisa equals() nzira kubva sort() kirasi.

Semuyenzaniso:

Collections()

Kubuda:

import java.util.Arrays; import java.util.Collections; import java.util.List; public class CompareTwoLists {
public static void main(String[] args) {
List listOne = Arrays.asList('b', 'c', 'a');
List listTwo = Arrays.asList('a', 'c', 'b');
List listThree = Arrays.asList('c', 'a', 'b');

Collections.sort(listOne);
Collections.sort(listTwo);
Collections.sort(listThree);

boolean isEqual = listOne.equals(listTwo);
System.out.println(isEqual);

isEqual = listOne.equals(listThree);
System.out.println(isEqual);
} }

Enzanisa zvinyorwa zviviri, tsvaga misiyano

I true true interface inopawo nzira dzekuwana mutsauko pakati pezvinyorwa zviviri.

I List nzira inoenzanisa zvinyorwa zviviri uye inobvisa zvese zvakajairika zvinhu. Chasara ndiko kuwedzera kana kushayikwa kwezvinhu.

Semuenzaniso kana tichienzanisa zvinyorwa zviviri, removeAll() uye listOne uye isu tinoda kutsvaga kuti ndezvipi zvinhu zvisipo kubva listTwo tinoshandisa:

listTwo

Kubuda:

import java.util.ArrayList; import java.util.Arrays; public class CompareTwoArrayLists {
public static void main(String[] args) {
ArrayList listOne = new ArrayList(Arrays.asList(1, 2, 3, 4, 5));

ArrayList listTwo = new ArrayList(Arrays.asList(1, 2, 4, 5, 6, 7));

listOne.removeAll(listTwo);

System.out.println('Missing items from listTwo ' + listOne);
} }

Saizvozvo, kana isu takashandisa:

Missing items from listTwo [3]

Taizowana:

listTwo.removeAll(listOne); System.out.println('Missing items from listOne ' + listTwo);

Enzanisa zvinyorwa zviviri, tsvaga zvakajairika zvinhu

I Missing items from listOne [6, 7] nzira chete inochengeta zvinhu zvakajairika mune ese ari maviri mazita. Semuyenzaniso:

retainAll()

Kubuda:

public class CompareTwoArrayLists {
public static void main(String[] args) {
ArrayList listOne = new ArrayList(Arrays.asList(1, 2, 3, 4, 5));

ArrayList listTwo = new ArrayList(Arrays.asList(1, 2, 4, 5, 6, 7));

listOne.retainAll(listTwo);

System.out.println('Common items in both lists ' + listOne);
} }