Unveränderlichkeit in Java

Hallo Habr. In Erwartung des bevorstehenden Starts des OCAJP-Kurses (Oracle Java Programmer Certification Preparation) haben wir eine traditionelle Übersetzung des Materials für Sie vorbereitet.



Wir laden auch alle ein, an der offenen Demo-Lektion "Konstruktoren und Initialisierungsblöcke" teilzunehmen . In diesem kostenlosen Webinar werden wir:

- den Konstruktor für Teile zerlegen

- die Finalisten definieren (endgültige Variablen)

- die Dinge in Ordnung bringen (Initialisierungen)






Eine unveränderliche Klasse ist eine Klasse, die nach der Initialisierung ihren Status nicht ändern kann. Wenn der Code einen Verweis auf eine Instanz einer unveränderlichen Klasse enthält, führen Änderungen daran zur Erstellung einer neuen Instanz.





Damit eine Klasse unveränderlich ist, muss sie die folgenden Anforderungen erfüllen:





  • final, . .





  • .





  • , .





  • , .





  • - , .





, , , :





import java.util.Map;
public final class MutableClass {
  private String field;
  private Map<String, String> fieldMap;
public MutableClass(String field, Map<String, String> fieldMap) {
  this.field = field;
  this.fieldMap = fieldMap;
}
public String getField() {
  return field;
}
public Map<String, String> getFieldMap() {
  return fieldMap;
}
}
      
      



.





import java.util.HashMap;
import java.util.Map;
public class App {
public static void main(String[] args) {
  Map<String, String> map = new HashMap<>();
  map.put("key", "value");
    
  //   "" 
  MutableClass mutable = new MutableClass("this is not immutable", map);
  //      map ==  
  mutable.getFieldMap().put("unwanted key", "another value");
  mutable.getFieldMap().keySet().forEach(e ->  System.out.println(e));
}
}
//   
unwanted key
key
      
      



, , , .





import java.util.HashMap;
import java.util.Map;
public class AlmostMutableClass {
  private String field;
  private Map<String, String> fieldMap;
public AlmostMutableClass(String field, Map<String, String> fieldMap) {
  this.field = field;
  this.fieldMap = fieldMap;
}
public String getField() {
  return field;
}
public Map<String, String> getFieldMap() {
  Map<String, String> deepCopy = new HashMap<String, String>();
  for(String key : fieldMap.keySet()) {
    deepCopy.put(key, fieldMap.get(key));
  }
  return deepCopy;
}
}
      
      



getFieldMap



, , AlmostMutableClass



. , Map



, getFieldMap



, , map



. map



, . 





map



, , . , .





import java.util.HashMap;
import java.util.Map;
public class App {
public static void main(String[] args) {
  Map<String, String> map = new HashMap<>();
  map.put("good key", "value");
    
  //   "" 
  AlmostMutableClass almostMutable = new AlmostMutableClass("this is not immutable", map);
  
  //       
  //      map
  System.out.println("Result after modifying the map after we get it from the object");
  almostMutable.getFieldMap().put("bad key", "another value");
  almostMutable.getFieldMap().keySet().forEach(e -> System.out.println(e));
  
  System.out.println("Result of the object's map after modifying the initial map");
  map.put("bad key", "another value");
  almostMutable.getFieldMap().keySet().forEach(e -> System.out.println(e));
    
  }
}
//   
Result after modifying the map after we get it from the object
good key
Result of the object's map after modifying the initial map
good key
bad key
      
      



, , getFieldMap



. :





public AlmostMutableClass(String field, Map<String, String> fieldMap) {
  this.field = field;      
  Map<String, String> deepCopy = new HashMap<String, String>();
  for(String key : fieldMap.keySet()) {
    deepCopy.put(key, fieldMap.get(key));
  }
  this.fieldMap = deepCopy;
}
//   
Result after modifying the map after we get it from the object
good key
Result of the object's map after modifying the initial map
good key
      
      



, . , , .





, , , .





Java

String



, , , Java. — , .





, String



, , , . - Java (Integer, Boolean ..), String







:





  • .





  • , " ". String







  • , .





  • String -, -, String



    .





  • , , , .






« Oracle Java Programmer (OCAJP)».





« ».








All Articles