[JAVA] 두 리스트 값 비교 방법~ArrayList List~(자바)__-How to compare two lists-++

- 두 개의 리스트에 대한 값 비교 방법


1. containsAll() 메서드를 사용하는 방법

2. Arrays.equals() 메서드를 사용하는 방법 

3. Arrays.equals() + Collections.sort() 메서드를 사용하는 방법

4. ArrayList를 Collection으로 변환 후 removeAll() 메서드를 사용하는 방법


1. containsAll() : 

두개의 리스트가 같은지 비교 / 리턴값은 boolean type

@RequestMapping(value = "/ex1")
public String ex1(){
ArrayList<String> cntyList = new ArrayList<String>();
cntyList.add("한국");
cntyList.add("미국");
cntyList.add("일본");
System.out.println("국가목록 : " + cntyList.toString() );

ArrayList<String> cntyList2 = new ArrayList<String>();
cntyList2.add("일본");
cntyList2.add("독일");
cntyList2.add("러시아");
System.out.println("국가목록2 : " + cntyList2.toString() );

//2개 리스트 값이 같은지 비교
System.out.println("국가목록과 국가목록2비교 : " + cntyList.containsAll(cntyList2));

return "index";
}

결과 : 

국가목록 : [한국, 미국, 일본]

국가목록2 : [일본, 독일, 러시아]

국가목록과 국가목록2비교 : false


2. Arrays.equals() : 

두개의 리스트(배열)가 같은지 비교 / 리턴값은 boolean type / 리스트를 배열(Array)로 형변환필요

@RequestMapping(value = "/ex2")
public String ex2(){
ArrayList<String> cntyList = new ArrayList<String>();
cntyList.add("한국");
cntyList.add("미국");
System.out.println("국가목록 : " + cntyList.toString() );

ArrayList<String> cntyList2 = new ArrayList<String>();
cntyList2.add("한국");
cntyList2.add("미국");

System.out.println("국가목록2 : " + cntyList2.toString() );

//2개 리스트 값이 같은지 비교
boolean result = Arrays.equals(cntyList.toArray(),cntyList2.toArray());
System.out.println("국가목록과 국가목록2비교 : " + result);

return "index";
}

결과 : 

국가목록 : [한국, 미국]

국가목록2 : [한국, 미국]

국가목록과 국가목록2비교 : true


3. Arrays.equals() + Collections.sort() : 

두 개의 리스트(배열) 중  배열의 값은 같으나 순서가 다른경우 false가 리턴된다. 이럴 때 Collections.sort() 메서드를 사용하여 두개의 배열을 오름차순으로 정렬 후 비교하면 true값을 리턴한다. 

@RequestMapping(value = "/ex3")
public String ex3(){
ArrayList<String> cntyList = new ArrayList<String>();
cntyList.add("미국");
cntyList.add("한국");
System.out.println("국가목록 : " + cntyList.toString() );

ArrayList<String> cntyList2 = new ArrayList<String>();

cntyList2.add("한국");
cntyList2.add("미국");
System.out.println("국가목록2 : " + cntyList2.toString() );

//오름차순으로 정렬
    Collections.sort(cntyList);
Collections.sort(cntyList2);

boolean result = Arrays.equals(cntyList.toArray(),cntyList2.toArray());
System.out.println("국가목록과 국가목록2비교 : " + result);

return "index";
}

결과 : 

국가목록 : [미국, 한국]

국가목록2 : [한국, 미국]

국가목록과 국가목록2비교 : true


4. ArrayList를 Collection으로 변환 후 removeAll() 메서드를 사용하는 방법 : 

Collection으로 변환 후 removeAll()를 실행 후 출력결과를 보면 중복되지 않은 값만 출력하게 된다.

국가리스트와 국가리스트2를 비교했을 때 국가리스트를 기준으로 했을 경우 국가리스트2와 중복되지않은 값은 없다.

반대로 국가리스트2 를 기준으로 했을 경우, 중복된 값은 제거후 남은 "중국"값만 출력되었다. 

@RequestMapping(value = "/ex4")
public String ex4(){
Collection<String> cntyList = new ArrayList(Arrays.asList("한국","미국","일본"));
Collection<String> cntyList2 = new ArrayList(Arrays.asList("한국","미국","일본","중국"));

//Collection으로 형변환 후 리스트에 담기
    List<String> targetList = new ArrayList<String>(cntyList);
List<String> targetList2 = new ArrayList<String>(cntyList2);

//각각 비교 대상 리스트 값 모두 제거
targetList.removeAll(cntyList2);
targetList2.removeAll(cntyList);

System.out.println("국가목록 :" + targetList.toString());
System.out.println("국가목록2 :" + targetList2.toString());

return "index";
}

결과 : 

국가목록 :[]

국가목록2 :[중국]


출처 : https://ddolcat.tistory.com/513

댓글

T O P