Properties Class: ================= ArrayList ar = new ArrayList<>(); student.property =============== name = "Karthik" Branch = "Electronics" Roll = "1021" Percentage = "72" -> The Properties Class not required any generic for its implementation. -> Properties class represents a persistent set of properties. -> The properties can be save to a stream (file) or loaded from a stream. -> Properties class belongs to "java.util" package. -> Properties class define the instance variable: Properties defaults ==> holds property list associated with property object. Features of Properties class: ============================= 1) The Properties class is the sub of Hashtable class. 2) Properties class can be used to maintain a list of values in which the key is a string and value is also a string only. 3) Can specify other properties list as its the default. 4) Does not require the external synchronization and multiple threads can share the properties of the object. 5) Also it can retrieve the properties of the system. Advantage: ========= In the event, that any data is changed from the properties object, we don't have to recompile the given java class. It is used to store data that is to be changed habitually. Ex: How to implement the properties class ========================================== create a property file: ======================= db.property =========== username = "Ashokit" password = "ashoki123" import java.util.*; import java.io.*; class AnyClass{ public static void main(String[] args) throws Exception{ FileReader reader = new FileReader("db.property"); Properties p = new Properties(); p.load(reader); System.out.println(p.getProperty("username")); System.out.println(p.getProperty("password")); } } =================================================================== How to use Properties class to get all the System properties: ============================================================= package p1; import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.Set; public class MainClass { public static void main(String[] args) throws Exception{ Properties p = new Properties(); Set set = p.entrySet(); Iterator it = set.iterator(); while(it.hasNext()) { Map.Entry entry = (Map.Entry)it.next(); System.out.println(entry.getKey()+"===>"+entry.getValue()); } } } ================================================ Q-1: What is Properties Class? ============================== 1) Properties class is a legacy class which extends from Hashtable class. 2) It can store key-value pairs of string type only. 3) It can also load the key-value pairs from a file by using load() method. Q-2: What are the differences between Enumeration and Iterator? ================================================================ 1) Enumeration ==> legacy interface Iterator ==> Collection Framework interface 2) Enumerator ==> has methods: hasMoreElements() nextElement() Iterator has methods: hasNext() next() remove() HashMap hmp = new HashMap<>(); Here: Product ==> Custom Class Q-3: Can we store a custom class objects as a key in HashMap object? ===================================================================== HashMap hmp = new HashMap<>(); Yes. But it is recommended to override hashCode() and equals() method. ================================================================================ Concurrent HashMap ================== -> it is a class is introduced in JDK 1.5 belongs to "java.util.concurrent" package. -> The ConcurrentHashMap class implements to ConcurrentMap interface and also to Serializable interface. Syntax: public ConcurrentHashMap implements ConcurrentMap, Serializable{ } Hierarchy of ConcurrentHashMap class: ===================================== Map Interface <================= ConcurrentMap Interface, Serializable Interface extends ConcurrentMap Interface <================== ConcurrentNavigableMap Interface extends ConcurrentNavigableMap, Serializable <================== ConcurrentHashMap class implements package p1; import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; public class MainClass { public static void main(String[] args) { ConcurrentHashMap map = new ConcurrentHashMap<>(); System.out.println(map); // adding of elements map.put("A", 101); map.put("B", 201); map.put("C", 301); System.out.println(map); // map.put("null", null); // adding more elements into concurrent hashmap ConcurrentHashMap m = new ConcurrentHashMap<>(); m.putAll(map); System.out.println(m); System.out.println("The Size = "+map.size()); // accessing of values from the map Integer a = map.get("B"); System.out.println(a); // map.put("A", 141); map.putIfAbsent("B", 151); System.out.println(map); Iterator> it = map.entrySet().iterator(); while(it.hasNext()) { ConcurrentHashMap.Entry e = it.next(); System.out.println(e.getKey()+"===>"+e.getValue()); } } }