Drop Down

Sunday, June 15, 2025

Java 8 Notes Pics










Java 8 , ways to print List

 package com.test;


import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;


public class PrintingList {


public static void main(String[] args) {

List<String> list = new ArrayList<String>();

list.add("System");

list.add("org");

System.out.println("--------using toString() to print list----------");

System.out.println(list);

System.out.println("--------using iterator to print list----------");

Iterator<String> i = list.iterator();

while(i.hasNext())

{

System.out.println(i.next());

}

System.out.println("--------using For loop to print list----------");

for(int j = 0 ;j<list.size();j++)

{

System.out.println(list.get(j));

}

System.out.println("--------using Enhanced For loop to print list----------");

for(String item : list)

{

System.out.println(item);

}

System.out.println("--------using foreach() with lamda to print list----------");

list.forEach(x->System.out.println(x));   

 // Here we are using forEach() or List. it takes Consumer functional Interface as paramer. Consumer Funtional Interface has accept() abstract method. So we are basically implementing accept() in forEach() using lamda expression.

}


}

-----------------------output---------------

--------using toString() to print list----------

[System, org]

--------using iterator to print list----------

System

org

--------using For loop to print list----------

System

org

--------using Enhanced For loop to print list----------

System

org

--------using foreach() with lamda to print list----------

System

org

Friday, August 12, 2022

Interview Codings

 Reverse String

-------------------------

package strings;


public class Reverse_String {


public static void main(String[] args) 

{

  String original_string1 = "aidnI";

  String original_string2 = "aidnI si taerg";

  

  char[] ch = original_string1.toCharArray();

  for(int i = ch.length-1;i>=0;i--)

  {

  System.out.print(ch[i]);

  }

}


}


------------------------

Sunday, January 12, 2020

Get All Methods of Objects Class

package warmUP;

import java.lang.reflect.Method;

public class Show_Methods {

public static void main(String[] args) throws ClassNotFoundException
{
int count = 0;
Class c = Class.forName("java.lang.Object");
Method[] m = c.getDeclaredMethods();
for(Method m1 : m)
{
count++;
System.out.println(m1.getName());
}
System.out.println("Total No. of Methods: "+count);
}

}
----------------------------------------------------------------
OutPut:
---------
finalize
wait
wait
wait
equals
toString
hashCode
getClass
clone
notify
notifyAll
registerNatives
Total No. of Methods: 12


Different ways to create objects in Java

ways to create objects in Java


1. Using new keyword
This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.
MyObject object = new MyObject();

2. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

 3. Using clone()The clone() can be used to create a copy of an existing object.
MyObject anotherObject = new MyObject(); 
MyObject object = anotherObject.clone();

4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream ); 
MyObject object = (MyObject) inStream.readObject();
------------------------------------------------------------------------------------------------------------------------------------.

Monday, January 6, 2020

TryLock game

package multithreading;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

class MyThread4 extends Thread
{
static ReentrantLock l = new ReentrantLock();
MyThread4(String name)
{
super(name);
}
@Override
public void run()
{
do
{
try {
if(l.tryLock(5000,TimeUnit.MICROSECONDS))
{
System.out.println(Thread.currentThread().getName()+" ..Got Lock and running safe code");
Thread.sleep(30000);
l.unlock();
System.out.println(Thread.currentThread().getName()+" ..Released the lock");
break;
}
else
{
System.out.println(Thread.currentThread().getName()+" ..Unable to get Lock and executing alternative code");
Thread.sleep(5000);
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
while(true);
}
}


public class ReentrantLock_Demo4
{

public static void main(String[] args)
{
MyThread4  mtt1 = new MyThread4("F1 Thread");
MyThread4  mtt2 = new MyThread4("F2 Thread");
mtt1.start();
mtt2.start();
}

}
----------------------------------
output
----------
F2 Thread ..Got Lock and running safe code
F1 Thread ..Unable to get Lock and executing alternative code
F1 Thread ..Unable to get Lock and executing alternative code
F1 Thread ..Unable to get Lock and executing alternative code
F1 Thread ..Unable to get Lock and executing alternative code
F1 Thread ..Unable to get Lock and executing alternative code
F1 Thread ..Unable to get Lock and executing alternative code
F2 Thread ..Released the lock
F1 Thread ..Got Lock and running safe code
F1 Thread ..Released the lock

Java 8 Notes Pics