Java Bandhingake Loro Dhaptar

Antarmuka Dhaptar ing Java nyedhiyakake cara supaya bisa mbandhingake rong Dhaptar lan nemokake barang umum lan sing ilang saka dhaptar.



Bandhingake rong dhaptar sing durung diurutake kanggo kesetaraan

Yen sampeyan pengin mriksa manawa rong dhaptar padha, yaiku ngemot item sing padha lan katon ing indeks sing padha, mula bisa digunakake:

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);
} }

Output:


true false

Kaya sing sampeyan waca equals() cara mbandhingake barang lan dununge ing dhaptar.

Gegandhengan:




Bandhingake rong dhaptar sing diurutake

Apa rong dhaptar ngemot barang sing padha?



Kanggo mbandhingake rong dhaptar kesetaraan mung ing syarat-syarat item preduli saka dununge, kita kudu nggunakake sort() cara saka Collections() kelas.

Contone:

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);
} }

Output:


true true

Bandhingake rong dhaptar, temokake bedane

Sing List antarmuka uga nyedhiyakake cara kanggo nemokake beda antarane rong dhaptar.

Sing removeAll() cara mbandhingake rong dhaptar lan mbusak kabeh item umum. Sing isih ana yaiku barang tambahan utawa sing ilang.

Contone nalika mbandhingake rong dhaptar, listOne lan listTwo lan kita pengin ngerti barang sing ilang saka listTwo kita nggunakake:

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);
} }

Output:


Missing items from listTwo [3]

Kajaba, yen digunakake:

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

Kita bakal entuk:

Missing items from listOne [6, 7]

Bandhingake rong dhaptar, temokake barang umum

Sing retainAll() cara mung nyimpen item sing umum ing kaloro dhaptar. Contone:

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);
} }

Output:


Common items in both lists [1, 2, 4, 5]