Nitin Agrawal
Contact -
  • Home
  • Interviews
    • Secret Receipe
    • InterviewFacts
    • Resume Thoughts
    • Daily Coding Problems
    • BigShyft
    • CompanyInterviews >
      • InvestmentBanks >
        • ECS
        • Bank Of America
        • WesternUnion
        • WellsFargo
      • ProductBasedCompanies >
        • CA Technologies
        • Model N India
        • Verizon Media
        • Oracle & GoJek
        • IVY Computec
        • Nvidia
        • ClearWaterAnalytics
        • ADP
        • ServiceNow
        • Pubmatic
        • Expedia
        • Amphora
        • CDK Global
        • CDK Global
        • Epic
        • Sincro-Pune
        • Whiz.AI
        • ChargePoint
      • ServiceBasedCompanies >
        • Altimetrik
        • ASG World Wide Pvt Ltd
        • Paraxel International & Pramati Technologies Pvt Ltd
        • MitraTech
        • Intelizest Coding Round
        • EPAM
    • Interviews Theory
  • Programming Languages
    • Java Script >
      • Tutorials
      • Code Snippets
    • Reactive Programming >
      • Code Snippets
    • R
    • DataStructures >
      • LeetCode Problems
      • AnagramsSet
    • Core Java >
      • Codility
      • Program Arguments OR VM arguments & Environment variables
      • Java Releases
      • Threading >
        • ThreadsOrder
        • ProducerConsumer
        • Finalizer
        • RaceCondition
        • Executors
        • Future Or CompletableFuture
      • Important Points
      • Immutability
      • Dictionary
      • URL Validator
    • Julia
    • Python >
      • Decorators
      • String Formatting
      • Generators_Threads
      • JustLikeThat
    • Go >
      • Tutorial
      • CodeSnippet
      • Go Routine_Channel
      • Suggestions
    • Methodologies & Design Patterns >
      • Design Principles
      • Design Patterns >
        • TemplatePattern
        • Adapter Design Pattern
        • Decorator
        • Proxy
        • Lazy Initialization
        • CombinatorPattern
        • RequestChaining
        • Singleton >
          • Singletons
  • Frameworks
    • Apache Velocity
    • Spring >
      • Spring Boot >
        • CustomProperties
        • ExceptionHandling
        • Issues
      • Quick View
    • Rest WebServices >
      • Interviews
      • Swagger
    • Cloudera BigData >
      • Ques_Ans
      • Hive
      • Apache Spark >
        • ApacheSpark Installation
        • SparkCode
        • Sample1
        • DataFrames
        • RDDs
        • SparkStreaming
        • SparkFiles
    • Integration >
      • Apache Camel
    • Testing Frameworks >
      • JUnit >
        • JUnit Runners
      • EasyMock
      • Mockito >
        • Page 2
      • TestNG
    • Blockchain >
      • Ethereum Smart Contract
      • Blockchain Java Example
    • Microservices >
      • Messaging Formats
      • Design Patterns
    • AWS >
      • Honeycode
    • Dockers >
      • GitBash
      • Issues
  • Databases
    • MySql
    • Oracle >
      • Interview1
      • SQL Queries
    • Elastic Search
  • Random issues
    • TOAD issue
    • Architect's suggestions
  • Your Views
Below is the code to compare the complex objects. Ok...it is not some magic stick which you can use to compare any of the objects of your custom class.
Yes, there are some conditions & limitations -
1) Your custom classes better to override equals()
2)  You have to use the jdk1.7
3) If the objects which you are comparing also referencing other complex classes then it will not go down the hierarchy to find the fields having differences.
 It will just tell about the fields in the current objects.
So why not, just check it once to know more about it :)

/**
  * Below is the method to compare the fields of any type of objects passed to it.
  * But the classes of passed objects must be comparable & must override the equals() of Object class
  * to make those objects comparable.
  * It is always better to override the equals() in your classes to keep future requirements in view.
  * jdk1.7 is used because Objects class is introduced in jdk1.7 only.
  * It returns a list of names of fields of the passed objects which have values different.
  * @param obj1
  * @param obj2
  * @return list of field names
  * @throws IllegalArgumentException
  * @throws IllegalAccessException
  */
 public static List<String> compare(Object obj1, Object obj2) throws IllegalArgumentException, IllegalAccessException{
  List<Field> differentFields = new LinkedList<>();
  List<String> differentField = new LinkedList<>();
     if(obj1 != obj2){
         Class clazz = obj1.getClass();
         Field[] fields = clazz.getDeclaredFields();
         for(Field f: fields){
             boolean accessible = f.isAccessible();
             if(! accessible){
                 f.setAccessible(true);
             }
             Object obj1f = f.get(obj1);
             Object obj2f = f.get(obj2);
             if(!Objects.equals(obj1f, obj2f)){
                 differentFields.add(f);
                 differentField.add(f.getName());
             }
             f.setAccessible(accessible);
         }
     }
     return differentField;
 }
}

Powered by Create your own unique website with customizable templates.