반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
import java.util.*;
public class ListRemoveAllTest {
public static void main(String[] args) {
// list<String>.removeAll 예제
List orgList = new ArrayList();
orgList.add("AA");
orgList.add("BB");
orgList.add("CC");
orgList.add("AA");
List list = new ArrayList();
list.add("CC");
list.add("CC");
list.add("AA");
System.out.println("=======BEFORE============");
for(String item : orgList) {
System.out.println(item);
}
orgList.removeAll(list);
System.out.println("=======AFTER============");
for(String item : orgList) {
System.out.println(item);
}
// list<VO>.removeAll 예제
UserVo vo;
List voOrgList = new ArrayList();
vo = new UserVo("Mike",35,"서울");
voOrgList.add(vo);
vo = new UserVo("Tom",35,"김해");
voOrgList.add(vo);
vo = new UserVo("Tom",99,"김해");
voOrgList.add(vo);
vo = new UserVo("Bit",35,"서울");
voOrgList.add(vo);
List voList = new ArrayList();
vo = new UserVo("Mike",35,"서울");
voList.add(vo);
vo = new UserVo("Tom",35,"김해");
voList.add(vo);
System.out.println("=======BEFORE============");
for(UserVo item : voOrgList) {
System.out.println(item.toString());
}
//removeAll -> batchRemove -> contains -> equlas
voOrgList.removeAll(voList);
System.out.println("=======AFTER============");
for(UserVo item : voOrgList) {
System.out.println(item.toString());
}
}
}
class UserVo {
private String name;
private int age;
private String address;
UserVo(String name,int age, String address){
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "UserVo [name=" + name + " , Age = "+age+", address=" + address + "]";
}
@Override
public boolean equals(Object anObject) {
if (!(anObject instanceof UserVo)) {
return false;
}
UserVo otherMember = (UserVo)anObject;
return (otherMember.getName().equals(getName())&&otherMember.getAge()==getAge()&&otherMember.getAddress().equals(getAddress()));
}
|
cs |
과제 진행중 A List 중 B List 중복되는것을 제거해야하는데 for문을 돌리면서 AList.contains(BList중) 으로하였다가
removeAll 을 알게되어 남기게 됐습니다. 남기는김에 List<VO>관련하여 euqals를 이용하여 removeAll 하는 방법도 추가
반응형
'Java' 카테고리의 다른 글
Enum equals , == (0) | 2024.04.11 |
---|---|
Mac Java 버전 관리 (0) | 2023.03.13 |
Windows OpenJdk 다운로드 및 설치 (0) | 2022.10.21 |
Java 상위로 예외 넘기기 throws (0) | 2022.10.14 |
Java HMAC 암호화 (2) | 2021.12.15 |
댓글