Skip to main content

Posts

Java Program to count number of employees working in a company (Completed With Garbage Collection)

    // Java Program to count number of employees working in a company (Completed With Garbage Collection)     class Employee {         private String name ;         private int age ;         private int ID ;         private static int nextId = 1 ;                         public Employee ( String name , int age ) {             this . name = name;             this . age = age;             this . ID = nextId ++ ;         }                 public void show ()         {             System . out . println ( " \n ID=" + ID +   " \n Name=" + name + " \n Age=" + age);         }                 public void showNextId ()         {             System . out . println ( "Next employee id will be=" + nextId);         }                 protected void finalize ()         {             -- nextId;             // In this case,             // gc will call finalize()             // for 2 times for 2 objects.         }     }     c
Recent posts

Mastering Python Decorators: A Comprehensive Guide

Welcome to our comprehensive guide on mastering Python decorators! Whether you're a seasoned Python developer or just starting your coding journey, understanding decorators is a key step toward writing more elegant and efficient code. What are Decorators? Decorators are a powerful feature in Python that allows you to modify the behavior of functions or methods. They provide a clean and concise way to enhance the functionality of your code without cluttering it with repetitive patterns. Basics of Decorators Let's start with the basics. In Python, decorators are denoted by the @decorator syntax . Consider the following example:          def my_decorator ( func ):         def wrapper ():             print ( "Something is happening before the function is called." )             func()             print ( "Something is happening after the function is called." )         return wrapper     @my_decorator     def say_hello ():         print ( "Hello!" )

How to Create and Use Custom Filters in Django HTML Templates?

To add custom filters in a Django HTML template, you can follow these steps: Create a Custom Filter Function: You need to create a Python function that defines your custom filter logic. This function should accept one or more arguments and return the filtered output. Register the Filter Function: You'll need to register your custom filter function with Django's template system. This is typically done in a Django app's templatetags directory. Use the Custom Filter in Your HTML Template: After registering the custom filter, you can use it in your HTML templates by loading the filter library and applying the filter to a variable or expression. Here's a step-by-step guide: Step 1: Create a Custom Filter Function  Let's say you want to create a custom filter to capitalize the first letter of each word in a string. You can define your custom filter function like this:         # myapp/templatetags/custom_filters.py     from django import template     register = templ

How to find duplicate records in MongoDB?

To find duplicate entries in MongoDB based on the code and user_id fields, you can use the aggregate method along with the $group and $match pipeline stages. Here is an example of how to do this: Let's take the example collection - user_collection .     {             "_id" : ObjectId( "640054ef881f521a7b68fc96" ) ,             "code" : "google_uploader" ,             "user_id" : "33052" ,             "type" : "uploader" ,             "charge_type" : "Prepaid" ,             "available_credits" : 500 ,             "total_used_credits" : 0     }     {             "_id" : ObjectId( "640054ef881f521a7b68fc99" ) ,             "code" : "google_uploader" ,             "user_id" : "33052" ,             "type" : "uploader" ,             "charge_type" : "Prepaid&quo

How to copy one collection to another by adding a field in MongoDB?

Is there a way to copy one collection to another in MongoDB while adding a new field? Yes, it is possible to copy one collection to another in MongoDB by adding a field using the $addFields pipeline stage in the aggregation framework. Here is an example of how you can do it: Suppose we have two collections named source_collection and target_collection , and we want to copy data from source_collection to target_collection by adding a new field called new_field . Sample source_collection documents:     {         "_id" : 1 ,         "name" : "John" ,         "age" : 25     },     {         "_id" : 2 ,         "name" : "Jane" ,         "age" : 30     },     {         "_id" : 3 ,         "name" : "Bob" ,         "age" : 40     } We want to copy this collection to a new collection named target_collection by adding a new field new_field which contains a string value. T