Advanced Usage: Custom Objects, Serialization, and Stream API Integration
Advanced Usage: Custom Objects, Serialization, and Stream API Integration
आज के blog में हम Java programming के एक बहुत important और exam-specific topic पर बात करने वाले हैं — Custom Objects, Serialization और Stream API Integration। ये तीनों concept Object-Oriented Programming में बहुत काम आते हैं, खासकर जब हमें data को efficiently process या store करना होता है।
Custom Objects क्या होते हैं?
Java में जब हम किसी class को अपने हिसाब से define करते हैं और उसके अंदर fields (variables) और methods (functions) लिखते हैं, तो ऐसे objects को Custom Objects कहा जाता है। ये हमारे program की जरूरत के हिसाब से design किए जाते हैं ताकि data को logically represent किया जा सके।
Example:
class Student {
int id;
String name;
double marks;
Student(int id, String name, double marks){
this.id = id;
this.name = name;
this.marks = marks;
}
void display(){
System.out.println(id + " " + name + " " + marks);
}
}
public class Main {
public static void main(String args[]){
Student s1 = new Student(101, "Ravi", 88.5);
s1.display();
}
}
ऊपर के example में Student एक custom object है जो तीन attributes को hold करता है — id, name और marks।
Custom Objects की जरूरत क्यों पड़ती है?
- Data को logically group करने के लिए।
- Code को reusable और modular बनाने के लिए।
- Complex real-world entities को represent करने के लिए।
Custom Objects को Collections में store करना
Custom objects को हम Java Collection Framework (जैसे ArrayList, HashMap, LinkedList) में भी store कर सकते हैं।
ArrayList<Student> list = new ArrayList<>();
list.add(new Student(101, "Aman", 85));
list.add(new Student(102, "Riya", 90));
list.add(new Student(103, "Karan", 78));
for(Student s : list){
System.out.println(s.name + " : " + s.marks);
}
Serialization क्या है?
Serialization एक ऐसी process है जिसमें हम किसी object को byte stream में convert करते हैं ताकि उसे file, database, या network के जरिए transfer किया जा सके। और जब उस byte stream को वापस object में convert किया जाता है, उसे Deserialization कहते हैं।
Serialization क्यों जरूरी है?
- Object को permanent form में save करने के लिए।
- Network communication में object transfer करने के लिए।
- Data persistence maintain करने के लिए।
Serializable Interface
किसी class को serializable बनाने के लिए उसे java.io.Serializable interface implement करना जरूरी होता है। यह एक marker interface है — यानी इसमें कोई method नहीं होता, बस यह Java को बताता है कि यह class serializable है।
Example of Serialization:
import java.io.*;
class Student implements Serializable {
int id;
String name;
transient double marks;
Student(int id, String name, double marks){
this.id = id;
this.name = name;
this.marks = marks;
}
}
public class SerializeDemo {
public static void main(String[] args) {
try {
Student s1 = new Student(101, "Ravi", 89.7);
FileOutputStream fout = new FileOutputStream("student.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(s1);
out.close();
fout.close();
System.out.println("Object successfully serialized!");
} catch (Exception e) {
System.out.println(e);
}
}
}
Deserialization Example:
import java.io.*;
class DeserializeDemo {
public static void main(String[] args) {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("student.txt"));
Student s = (Student) in.readObject();
System.out.println(s.id + " " + s.name + " " + s.marks);
in.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
ऊपर के code में ध्यान दो कि marks variable को transient keyword के साथ declare किया गया है। इसका मतलब है कि यह field serialization में include नहीं होगी।
Serialization के फायदे
- Data को persistent रखने में मदद करता है।
- Network communication आसान बनाता है।
- Object state को save और restore किया जा सकता है।
Serialization के नुकसान
- Performance थोड़ा slow हो सकता है।
- File size बढ़ जाता है।
- Version mismatch से error आ सकती है (serialVersionUID issue)।
Stream API Integration
Java 8 में introduce हुआ Stream API data processing को बहुत आसान और readable बनाता है। यह mainly Functional Programming concept पर based है।
Stream API क्या करता है?
Stream API collections (जैसे List, Set, Map) में stored data पर operations perform करने देता है — जैसे filtering, mapping, sorting, reducing आदि — वो भी बहुत ही concise तरीके से।
Stream API Example:
import java.util.*;
import java.util.stream.*;
class StreamExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10);
List<Integer> squareList = numbers.stream()
.map(x -> x * x)
.collect(Collectors.toList());
System.out.println(squareList);
}
}
ऊपर के code में map() function हर element को square कर रहा है और उसे एक नए list में collect कर रहा है।
Stream API के Important Operations
| Operation | Use |
|---|---|
| filter() | Data को condition के अनुसार filter करता है। |
| map() | Data को transform करता है (जैसे square, uppercase आदि)। |
| sorted() | Elements को sort करता है। |
| collect() | Stream को result में convert करता है (जैसे List, Set)। |
| forEach() | हर element पर operation perform करता है। |
Custom Objects के साथ Stream API
Stream API को Custom Objects के साथ integrate करके हम बहुत powerful data operations कर सकते हैं।
import java.util.*;
import java.util.stream.*;
class Student {
int id;
String name;
double marks;
Student(int id, String name, double marks){
this.id = id;
this.name = name;
this.marks = marks;
}
}
public class StudentStream {
public static void main(String[] args) {
List<Student> students = Arrays.asList(
new Student(101, "Aman", 85),
new Student(102, "Riya", 92),
new Student(103, "Karan", 76),
new Student(104, "Neha", 89)
);
// Filter students with marks above 80
List<Student> topperList = students.stream()
.filter(s -> s.marks > 80)
.collect(Collectors.toList());
topperList.forEach(s -> System.out.println(s.name + " : " + s.marks));
}
}
Stream API Integration Benefits
- Readable और concise code।
- Parallel processing support।
- Less boilerplate code और high performance।
Stream API और Serialization का Combination
हम Serialization और Stream API को एक साथ use करके data persistence + data processing दोनों achieve कर सकते हैं। Example के लिए:
import java.io.*;
import java.util.*;
import java.util.stream.*;
class Student implements Serializable {
int id;
String name;
double marks;
Student(int id, String name, double marks){
this.id = id;
this.name = name;
this.marks = marks;
}
}
public class CombinedExample {
public static void main(String[] args) {
List<Student> students = Arrays.asList(
new Student(1, "Aman", 82),
new Student(2, "Riya", 95),
new Student(3, "Karan", 70)
);
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("students.ser"));
out.writeObject(students);
out.close();
System.out.println("Students serialized!");
} catch (Exception e) { e.printStackTrace(); }
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("students.ser"));
List<Student> list = (List<Student>) in.readObject();
in.close();
// Stream API for data processing
list.stream()
.filter(s -> s.marks >= 80)
.forEach(s -> System.out.println(s.name + " - " + s.marks));
} catch (Exception e) { e.printStackTrace(); }
}
}
ऊपर के example में object list को पहले serialize किया गया है और फिर deserialization के बाद Stream API के जरिए process किया गया है।
Exam Tips:
- Serializable interface का use हमेशा याद रखें।
- transient keyword सिर्फ sensitive या temporary fields के लिए use करें।
- Stream API में हमेशा filter() और map() जैसे methods practice करें।
- Custom Objects के साथ Stream operations बार-बार revise करें।