1. κ°λ π»
λν΄νΈ λ©μλλ₯Ό μ μΈνλ©΄, κ·Έ μΈν°νμ΄μ€λ₯Ό ꡬνν ν λν΄νΈ λ©μλλ₯Ό μ¬μ μνμ§ μμ λͺ¨λ ν΄λμ€μμ λν΄νΈ ꡬνμ΄ μ°μ΄κ² λλ€.
κΈ°μ‘΄ μΈν°νμ΄μ€μ λ©μλλ₯Ό μΆκ°νλ κΈΈμ΄ μ΄λ Έμ§λ§ λͺ¨λ κΈ°μ‘΄ ꡬν체λ€κ³Ό 맀λλ½κ² μ°λλ리λΌλ 보μ₯μ μλ€. μλ° 7κΉμ§μ μΈμμμλ λͺ¨λ ν΄λμ€κ° "νμ¬μ μΈν°νμ΄μ€μ μλ‘μ΄ λ©μλκ° μΆκ°λ μΌμ μμν μλ€"λΌκ³ κ°μ νκ³ μ₯μ±λμΌλ λ§μ΄λ€.
λν΄νΈ λ©μλλ ꡬν ν΄λμ€μ λν΄ μ무κ²λ λͺ¨λ₯Έ μ± ν©μ μμ΄ λ¬΄μμ± 'μ½μ 'λ λΏμ΄λ€. μ΄λ² κΈμμλ μ΄λ° κ²½μ° λ°μν μ μλ λ¬Έμ μν©λ€μ μ΄ν΄λ³΄κ³ κ·Έμ λν ν΄κ²°μ± μ μμ보μ.
2. [λ¬Έμ μν© 1] μκ°ν μ μλ λͺ¨λ μν©μμ λΆλ³μμ ν΄μΉμ§ μλ λν΄νΈ λ©μλλ₯Ό μμ±νκΈ°λ μ΄λ ΅λ€. π€
μλ° 8μ Collction μΈν°νμ΄μ€μ μΆκ°λ removeIf λ©μλλ₯Ό μλ‘ μκ°ν΄ 보μ.
public interface Collection<E> extends Iterable<E> {
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}
}
μ΄ λ©μλλ μ£Όμ΄μ§ boolean ν¨μ(Predicate)κ° trueλ₯Ό λ°ννλ λͺ¨λ μμλ₯Ό μ κ±°νλ€. λν΄νΈ ꡬνμ λ°λ³΅μλ₯Ό μ΄μ©ν΄ μννλ©΄μ κ° μμλ₯Ό μΈμλ‘ λ£μ΄ predicateλ₯Ό νΈμΆνκ³ , predicateκ° trueλ₯Ό λ°ννλ©΄ λ°λ³΅μμ remove λ©μλλ₯Ό νΈμΆν΄ κ·Έ μμλ₯Ό μ κ±°νλ€.
μ΄ μ½λλ³΄λ€ λ λ²μ©μ μΌλ‘ ꡬννκΈ°λ μ΄λ ΅κ² μ§λ§, κ·Έλ λ€κ³ ν΄μ νμ‘΄νλ λͺ¨λ Collection ꡬν체μ μ μ΄μ°λ¬μ§λ κ²μ μλλ€.
org.apache.commons.collections4.collection.SynchronizedCollecionμ μκ°ν΄ 보μ. SynchronizedCollection ν΄λμ€κ° removeIfμ λν΄νΈ ꡬνμ λ¬Όλ €λ°κ² λλ€λ©΄, λͺ¨λ λ©μλ νΈμΆμ μμμ λκΈ°ννμ§ λͺ»νλ€λ λ¬Έμ κ° μκΈ°κ² λλ€. removeIfμ ꡬνμ λκΈ°νμ κ΄ν΄ μ무κ²λ λͺ¨λ₯΄λ μνμ΄κΈ° λλ¬Έμ΄λ€.
κ·Έλμ SynchronizedCollection μΈμ€ν΄μ€λ₯Ό μ¬λ¬ μ€λ λκ° κ³΅μ νλ νκ²½μμ ν μ€λ λκ° removeIfλ₯Ό νΈμΆνλ©΄ ConcurrentModificationExceptionμ΄ λ°μνκ±°λ λ€λ₯Έ μκΈ°μΉ λͺ»ν κ²°κ³Όλ‘ μ΄μ΄μ§ μ μλ€.
3. [λ¬Έμ μν© 2] λν΄νΈ λ©μλλ μ»΄νμΌμ μ±κ³΅νλλΌλ κΈ°μ‘΄ ꡬν체μ λ°νμ μ€λ₯λ₯Ό μΌμΌν¬ μ μλ€. πΎ
SuperClass, SubClassλ₯Ό μ΄μ©ν μμλ₯Ό μ΄ν΄λ³΄μ.
public class SuperClass {
public void hello() {
System.out.println("hello class");
}
}
----------
public class SubClass extends SuperClass {
}
----------
public static void main(String[] args) {
SubClass subClass = new SubClass();
subClass.hello();
}
μμ μ½λλ₯Ό λμμμΌ λ³΄λ©΄ μ½κ² μμν μ μλ―μ΄ "hello class"κ° μΆλ ₯λλ€. κ·Έλ¬λ©΄ SuperClass, SubClass, MarkerInterfaceλ₯Ό μ΄μ©ν μλ μ½λλ μ΄λ¨κΉ?
public class SuperClass {
// μ κ·Ό μ νμκ° privateλΌλ κ²μ νμΈνμ.
private void hello() {
System.out.println("hello class");
}
}
----------
public interface MarkerInterface {
default void hello() {
System.out.println("hello interface");
}
}
----------
public class SubClass extends SuperClass implements MarkerInterface{
}
----------
public static void main(String[] args) {
SubClass subClass = new SubClass();
subClass.hello();
}
SuperClassμ hello() λ©μλκ° privateμ΄λ―λ‘ SubClassμμλ μ κ·Όν μ μλ€. νμ§λ§ MarkerInterfaceμ λν΄νΈ λ©μλλ‘ hello()κ° μμΌλ―λ‘ μ΄κ²μ μ΄μ©νλ©΄ λ κ²μ΄λ€.
νμ§λ§, μ€μ λμμ μμΌλ³΄λ©΄ IllegalAccessErrorκ° λ°μνλ€.
μμμ μκΈ°ν μκ°μ νλ¦λλ‘λΌλ©΄ λΉμ°ν MarkerInterfaceμ hello()λ₯Ό μ¬μ©ν κ±° κ°μ§λ§, μ€μ λ‘λ SuperClassμ hello()λ₯Ό μ¬μ©νλ €κ³ νκΈ° λλ¬Έμ IllegalAccessErrorκ° λ°μν κ²μ΄λ€. (λ©μλλ₯Ό μ°ΎμΌλ €κ³ ν λ ν΄λμ€κ° μΈν°νμ΄μ€λ₯Ό μ΄κΈ°κ² λμ΄μκΈ° λλ¬Έμ΄λ€.)
μ¦, μ΄λ κ² κΈ°μ‘΄μ μ§μ¬μ§ μλ° μ½λκ° μν₯μ λ°μ μ μλ€.
4. ν΄κ²°μ± π½
μμ κ°μ λ¬Έμ μν©λ€μ ν΄κ²°νκΈ° μν΄μλ λ€μμ κ³ λ €ν΄μΌ νλ€.
- κΈ°μ‘΄ μΈν°νμ΄μ€μ λν΄νΈ λ©μλλ‘ μ λ©μλλ₯Ό μΆκ°νλ μΌμ κΌ νμν κ²½μ°κ° μλλ©΄ νΌν΄μΌ νλ€.
- μΆκ°νλ €λ λν΄νΈ λ©μλκ° κΈ°μ‘΄ ꡬν체λ€κ³Ό μΆ©λνμ§ μμμ§ μ¬μ¬μκ³ ν΄μΌ ν¨λ λΉμ°νλ€.
- λν΄νΈ λ©μλλ μΈν°νμ΄μ€λ‘λΆν° λ©μλλ₯Ό μ κ±°νκ±°λ κΈ°μ‘΄ λ©μλμ μκ·Έλμ²λ₯Ό μμ νλ μ©λκ° μλμ λͺ μ¬ν΄μΌ νλ€.
- μ΄λ° ννλ‘ μΈν°νμ΄μ€λ₯Ό λ³κ²½νλ©΄ λ°λμ κΈ°μ‘΄ ν΄λΌμ΄μΈνΈλ₯Ό λ§κ°λ¨λ¦¬κ² λλ€.
- μλ‘μ΄ μΈν°νμ΄μ€λΌλ©΄ λ¦΄λ¦¬μ€ μ μ λ°λμ ν μ€νΈλ₯Ό κ±°μ³μΌ νλ€.
- μλ‘ λ€λ₯Έ λ°©μμΌλ‘ μ΅μν μΈ κ°μ§λ ꡬνν΄ λ³΄μ.
- μΈν°νμ΄μ€λ₯Ό 릴리μ€ν νλΌλ κ²°ν¨μ μμ νλ κ² κ°λ₯ν κ²½μ°λ μκ² μ§λ§, μ λ κ·Έ κ°λ₯μ±μ κΈ°λμλ μ λλ€.
ν΄λΉ κΈμ λ°±κΈ°μ λμ 'μ΄νν°λΈ μλ° μλ²½ 곡λ΅'μ μ°Έκ³ νμμ΅λλ€.