FAQs

This section covers FAQs of Core JAVA.

Programs

All programs on Core JAVA for practice are listed here.

Assignments

Assignment programs to get prepared for an interview.

Basic

packagecom.core;
public class HelloWorld 
{
	public static void main(String args[])
	{
		System.out.println("Hello World");
	}
}

packagecom.core;
importjava.util.Scanner;
public class ScannerTest 
{	
	public static void main(String[] args) 
	{
		Scanner scan = newScanner(System.in);
		System.out.println("Enter First Integer");
		int i = scan.nextInt();
		System.out.println("Enter Second Integer");
		int j = scan.nextInt();

		System.out.println("i : " + i + "  J : " + j);
		System.out.println("Addition of two numbers :" + (i + j));
		System.out.println("Substraction of two numbers :" + (i - j));
		System.out.println("Mutliplication of two numbers :" + (i * j));
		try {
			System.out.println("Division of two numbers :" + (i / j));
		} catch (ArithmeticExceptione) {
			System.out.println(e.getMessage());
		}
	}
}

packagecom.core;
public class Numbers 
{
	public static void main(String[] args) 
	{
		int i = 50;
		int j = 50;
		int max = 0;

		if (i>j )
			max = i;
		elseif (j>i )
			max = j;
		else
			max = i;
		System.out.println(" The larger number is : " + max);
	}
}

packagecom.core;
public class ReverseNumber 
{
	public static void main(String[] args) 
	{
		int num = 12345;
		boole anres = true;
		int remainder = 0;
		int newNum = 0;

		while (res&&num != 0) 
		{
			remainder = num % 10;
			num = num / 10;
			System.out.println("Quotient is  :" + num);
			System.out.println("Remainder is :" + remainder);
			newNum = newNum * 10 + remainder;
		}
		System.out.println("Number in reverse :" + newNum);
	}
}

packagecom.core;
public class FloatAddition 
{
	public static void main(String[] args)
	{
		float f1=(float) 10.22;
		double f2=23.15;
		
		f1=(float)(f1+f2);
		System.out.println("Additon of float values :"+f1);	
	}
}

packagecom.core;
// A Series of numbers in which each number is the sum of the two preceding numbers
public class Fibonacci 
{
	public static void main(String[] args) 
	{
		int i = 0;
		int j = 1;
		int coun = 10;
		System.out.print(j + " ");
		for (intk = 1; k< coun; k++) 
		{
			int sum = i + j;
			System.out.print(sum + " ");
			i = j;
			j = sum;
		}
	}
}

packagecom.core;
import java.util.Scanner;
public class Vote 
{
	public static void main(String[] args) 
	{
		System.out.println("Enter your age to know if you are eligible to vote : ");
		Scanner s = newScanner(System.in);
		intage = s.nextInt();

		if (age>= 18)
			System.out.println("You are eligible to vote");
		else
			System.out.println("You are not eligible to vote");
	}
}

packagecom.core;
import java.util.Scanner;
public class StrudentGrade 
{
	public static void main(String[] args) 
	{
		System.out.println("Enter the markes you have scored : ");
		Scanner s = newScanner(System.in);
		intmarks = s.nextInt();
		if (marks> 80)
			System.out.println("Grade A");
		elseif (marks> 70)
			System.out.println("Grade B");
		elseif (marks> 60)
			System.out.println("Grade C");
		else
			System.out.println("Grade D");
	}
}

packagecom.core;
//An even number is an integer which is evenly divisible by 2. This means if number is divided by 2, it yields no remainder
public class NumberEvenOrOdd 
{
	public static void main(String[] args) 
	{
		int num = 21;
		if (num % 2 == 0)
			System.out.println("Number is even Number");
		else
			System.out.println("Number is Odd Number");
	}
}

package com.core;
//Java Program to Compute the Sum of Digits in a given Integer.
//Java Program for multiplying the Digits of a number
public class SumAndProduct 
{
	public static void main(String[] args) 
	{
		int sum = 0;
		int prod = 1;
		int num = 1456;
		int n;
		do 
		{
			n = num % 10;
			sum = sum + n;
			prod = prod * n;
			num = num / 10;
		}while (num > 0);
		System.out.println("Sum of the digits of a given number is :" + sum);
		System.out.println("Multiplication of the digits of the given number is :" + prod);
	}
}

package com.core;
// Program to swap two numbers using temporary variable and with out using temporary variable
public class Swap 
{
	public static void main(String[] args) 
	{
		int i = 10;
		int j = 20;
		int temp;
		temp = i;
		i = j;
		j = temp;
		System.out.println("i : " + i + " J : " + j);
		int a = 30;
		int b = 40;
		a = a + b;// 70
		b = a - b;// 30
		a = a - b;// 40
		System.out.println("a :" + a + "  b:" + b);
	}
}

package com.core;
//An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself.
//For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
public class Armstorng 
{
	public static void main(String[] args) 
	{
		int n;
		int temp;
		int a;
		int sum = 0;
		for (int num = 1; num < 999; num++) 
		{
			n = num;
			while (n > 0) 
			{
				a = n % 10;
				n = n / 10;
				sum = sum + (a * a * a);
			}
			if (num == sum)
			System.out.println(num + "  is an armstrong number");
			sum = 0;
		}
	}
}

package com.core;
// Program to check whether the given number is Prime or not
public class PrimeNumber 
{
	public static void main(String[] args)
	{
		int num = 4;
		boolean flag = true;
		for (int i = 2; i <= num / 2; i++) 
		{
			if (num % i == 0)
			flag = false;
		}
		if (flag && num != 1)
			System.out.println(num + " is a prime number");
		else
			System.out.println(num + " is NOT a prime number");
	}
}

package com.core;
// A String/Number is a palindrome if it remains unchanged when it reversed
public class Palindrome 
{
	public static void main(String[] args) 
	{
		int num = 123212;
		String str = "DAD";
		palindrome Number(num);
		palindrome String(str);

	}
	private static void palindromeNumber(int num) 
	{
		int n = num;
		int revNumber = 0;
		while (num != 0) 
		{
			revNumber = (revNumber * 10) + num % 10;
			num = num / 10;
		}
		if (revNumber == n)
			System.out.println("Given Number " + n + " is a palindrome");
		else
			System.out.println("Given Number " + n + " is NOT a palindrome");
	}
	private static void palindromeString(String str) 
	{
		String str1 = str;
		String revStr = "";
		int len = str.length();
		char ch;
		char[] charStr = str.toCharArray();

		for (int i = 0; i < len / 2; i++) 
		{
			ch = charStr[i];
			charStr[i] = charStr[len - 1 - i];
			charStr[len - 1 - i] = ch;
		}
		revStr = String.valueOf(charStr);

		if (str1.equals(revStr))
			System.out.println("Given String " + str + " is a palindrome");
		else
			System.out.println("Given String " + str + " is NOT a palindrome");
	}

}

package com.core;
public class ForLoop
{
	public static void main(String[] args) 
	{
		for (int i = 1; i <= 10; i++) 
		{
			System.out.println(i);
		}
	}

}

package com.core;
public class WhileLoop 
{
	public static void main(String[] args) 
	{
		int i = 10;
		while (i <= 20) 
		{
			System.out.println(i);
			i++;
		}
	}
}

package com.core;
//The condition is evaluated after executing the loop body.
//So loop will be executed at least once even if the condition is false.
public class DoWhileLoop 
{
	public static void main(String[] args) 
	{
		int i = 20;
		do 
		{
			System.out.println(i);
		} while (i <= 30);
	}
}

package com.core;
public class ForEachLoop 
{
	public static void main(String[] args) 
	{
		int a[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
		for (int i : a) 
		{
			System.out.println(i);
		}
	}
}

package com.core;
//Factorial number is multiplication of all positive integer from one to that number.
public class Factorial 
{
	int fact = 1;
	public static void main(String[] args) 
	{
		int num = 5;
		int fact = 1;
		for (int i = 1; i <= num; i++)
		fact = fact * i;
		System.out.println("Factorial of a number is :" + fact);
		System.out.println("Factorial using recursion : " + factRecursion(num));
	}
	private static int factRecursion(int num) 
	{
		if (num == 1)
			return 1;
		else
			return num * factRecursion(num - 1);
	}
}

import java.util.*;
class SumOfNum
{
	public static void main(String args[])
	{
		int sum = 0;
		Scanner s=new Scanner(System.in);
		System.out.println("Enter the Sum of values");
		int n=s.nextInt(); 
		for(int i = 1;i <= n;i++)
		{
			sum = sum + i;
		}
		System.out.println("The Sum Of "+n+" Numbers are:" + sum);
	}
}

OOPS Concepts

class OOPSConcept{
	public static int add(int a, int b) {
		return a + b;
	}
	public static double add(double d1, double d2) {

		return d1 + d2;
	}
	public static int add(int a, int b, int c) {

		return a + b + c;
	}	
}

class CollectionDemo extends OOPSConcept{
	public static void main(String[] args) {
		System.out.println(add(10, 20));
		System.out.println(add(20.0, 30.00));
		System.out.println(add(10, 20,40));
	}
}

package com.core;
public class OverLoading {
	public static void main(String[] args) {
		System.out.println(add(10, 20));
		System.out.println(add(20.0, 30.00));
		System.out.println(add(10, 20,40));
	}
	
	public static int add(int a, int b) {
		return a + b;
	}
	public static double add(double d1, double d2) {

		return d1 + d2;
	}
	public static int add(int a, int b, int c) {

		return a + b + c;
	}
}

class OOPSConcept{
	public static int sub(int a, int b) {
		return a - b;
	}
	public static double sub(double d1, double d2) {

		return d1 -  d2;
	}
	public static int sub(int a, int b, int c) {
		return a - b - c;
	}	
}

class Two extends OOPSConcept{
	public static void main(String[] args) {
		System.out.println(sub(200, 100));
		System.out.println(sub(400.00, 300.00));
		System.out.println(sub(400, 300,200));
	}
}

abstract class Base
{
    abstract void fun();
}	

class Derived extends Base 
{
    void fun() 
    {
           System.out.println("Derived fun() called"); 
    }
}

class Main 
{
    public static void main(String args[]) 
    { 
        // Uncommenting the following line will cause compiler error as the 
        // line tries to create an instance of abstract class.
        // Base b = new Base();
        // We can have references of Base type.
        Base b = new Derived();
        b.fun(); 
    }
}

interface interfaceDemo
{
   public void method();
   public void method1();
}

class Bright implements interfaceDemo
{
  public void method()
  {
      System.out.println("implementation of method");
  }
  public void method1()
  {
      System.out.println("implementation of method1");
  }
  public static void main(String arg[])
  {
      interfaceDemo interfaceDemo = new Bright();
      interfaceDemo. method();
  }
}

class ConstructorDemo{  
	ConstructorDemo()
	{
		System.out.println("ConstructorDemo is created");
	}  
	public static void main(String args[]){ 	 
	ConstructorDemo b=new ConstructorDemo();  
	}  
} 

public class StaticDemo {
  public static void main(String[] args) {
    One a1 = new One();
    Two b1 = new Two();
  }
}

class One {
  static{
    System.out.println("Static block of Class One");
  }
  {
    System.out.println("Non-Static block of a instance of Class One");
  }
  public One(){
    System.out.println("Constructing object of type One");
  }
}

class Two {
  static{
    System.out.println("Static block of Class Two");
  }
  {
    System.out.println("Non-Static block of a instance of Class Two");
  }
  public Two(){
    System.out.println("Constructing object of type One");
  }
}

class Student{  
	int rollno;  
	String name;  
	float fee;  
	Student(int rollno,String name,float fee){  
		this.rollno=rollno;  
		this.name=name;  
		this.fee=fee;  
	}  
	void display(){
	System.out.println(rollno+" "+name+" "+fee);
	}  
}  

public class ThisDemo{  
public static void main(String args[]){  
	Student s1=new Student(234,"Ramu",4500f);  
	Student s2=new Student(123,"Sitha",6500f);  
	s1.display();  
	s2.display();  
}}

class Laptop{  
String color="white";  
}  
class Dell extends Laptop{  
String color="black";  
void printColor(){  
System.out.println(color);//prints color of Dell class  
System.out.println(super.color);//prints color of Laptop class  
}  
}  

class SuperDemo{  
public static void main(String args[]){  
Dell d=new Dell();  
d.printColor();  
}} 

class Book{  
 final String type="Horror";//final variable  
 void run(){  
  System.out.println(type);
 }  
 public static void main(String args[]){  
 Book obj=new Book();  
 obj.run();  
 }  
}//end of class 

Arrays

class ArrayDemo
{
	public static void main(String ar[]){
	 int[] array = {10, 20, 30, 40, 50, 10};
	System.out.println("Given array elements..");
	for(int num : array){
		System.out.println(num);	
	}
}
}

import java.util.*;
class ArrayDemo1{
	public static void main(String ar[]){
		int sum=0;
		System.out.println("Enter the values 5 values");
		Scanner s=new Scanner(System.in);
		int arr[]=new int[5];
		for(int i=0;i<5;i++){
			arr[i]=s.nextInt();
		}
		for(int num: arr){
			sum=sum+num;
		}
	System.out.println("Sum of array elements is:"+sum);
	}
}

public class ArrayMaxMin {
 
        public static void main(String[] args) {
               
                //numbers array
                int numbers[] = new int[]{5,55,9,44,12,4,8,90,219,319};
               
                //assign first element of an array to largest and smallest
                int smallest = numbers[0];
                int largetst = numbers[0];
               
                for(int i=1; i< numbers.length; i++) { 
			if(numbers[i] > largetst)
                                largetst = numbers[i];
                        else if (numbers[i] < smallest)
                                smallest = numbers[i];    
                }
                System.out.println("Largest Number is : " + largetst);
                System.out.println("Smallest Number is : " + smallest);
        }
}

class ArrayLoop {
   public static void main(String[] args) {
      int[] ary = { 11,222,333,444};
      forDis(ary);
      foreachDis(ary);
   }
   public static void forDis(int[] a) {  
      System.out.println("Display an array using for loop");
      for (int i = 0; i < a.length; i++) {
         System.out.print(a[i] + " ");
      }
      System.out.println();
   }
   public static void foreachDis(int[] data) {
      System.out.println("Display an array using for each loop");
      for (int a  : data) {
         System.out.print(a+ " ");
      }
   }
}

// differences between array and arraylist
import java.util.ArrayList;
import java.util.Arrays;
 
class Arraylist
{
    public static void main(String args[])
    {
        /* ........... Normal Array............. */
        int[] arr = new int[3];
        arr[0] = 11;
        arr[1] = 22;
        System.out.println(arr[1]);
 
        /*............ArrayList..............*/
        
        ArrayList arrL = new ArrayList(2);
 
        // Add elements to ArrayList
        arrL.add(11);
        arrL.add(22);
 
        // Access elements of ArrayList
        System.out.println(arrL.get(1));
    }
}

// Removing duplicate elements from the sorted array
public class Strings {
	public static int[] duplicateElements(int[] input) {
		int j = 0;
		int i = 1;
		while (i < input.length) {
			if (input[j] == input[i])
				i++;
			else {
				input[++j] = input[i++];
			}
		}
		int[] output = new int[j + 1];
		for (int k = 0; k < output.length; k++)
			output[k] = input[k];
		return output;
	}
	public static void main(String[] args) {
		int[] input = { 10, 10, 20, 20, 30, 30, 30 };
		int[] output;
		output = duplicateElements(input);
		for (int i = 0; i < output.length; i++) {
			System.out.println(output[i]);
		}
	}
}

public class OutDemo {
  public static void main(String[] args) {
    String[] Hello = new String[3];
    Hello[0] = "Good Morning";
    Hello[1] = "Good Afternoon";
    Hello[2] = "Good Evening.";

    for (int i = 0; i < Hello.length; i++)
      System.out.println(Hello[i]);
  }
}

public class extendsDemo {
   public static void main(String[] args) {
      String[] beforeInit= new String[] { "A", "B", "C" };
      //Define array length
      String[] afterInit = new String[7];
    //extend new array
      afterInit[3] = "D";
      afterInit[4] = "E";
      afterInit[5] = "F";
      afterInit[6] = "G";
    //  check value and length 
	System.arraycopy(beforeInit,0, afterInit ,0, beforeInit.length);
      for (String str : afterInit){
         System.out.println(str);
      }
   }
}

public class Strings {
	public static void main(String[] args){
		String str="Bright IT Career";
		String[] strArray=str.split("\\s", 6);// Try with 0,2 as well 
		for(String s:strArray){
			System.out.println("Sub String is : "+s);
		}
		
		System.out.println("Lenght of the array of strings :"+strArray.length);
	}
	
}

class multiArrayDemo{  
public static void main(String args[]){  
  
//declaring and initializing MulMultidimensional array  
int arr[][]={{11,22,33},{22,44,55},{44,44,55}};  
  
//printing Multidimensional array  
for(int i=0;i<3;i++){  
 for(int j=0;j<3;j++){  
   System.out.print(arr[i][j]+" ");  
 }  
 System.out.println();  
}  
  
}}  

Strings

// Program to find out the length of the String
public class strLength {

	public static void main(String args[]) {
		String str = new String("Welcome to BrightITCareer.com");
		System.out.print("Length of the String is :  " + str.length());

	}
}

import java.util.Scanner;

// Program to find if the given string is a palindrome
public class Palindrome {
	public static void main(String args[]) {
		String original;
		String reverse = "";

		// Reads input from a console
		Scanner in = new Scanner(System.in);

		System.out.println("Enter a string to check if it is a Palindrome");
		original = in.nextLine();

		int length = original.length();

		for (int i = length - 1; i >= 0; i--)
			reverse = reverse + original.charAt(i);

		// Compares the original string with the string reversed
		if (original.equals(reverse))
			System.out.println("Entered string is a palindrome.");
		else
			System.out.println("Entered string is not a palindrome.");

	}
}	

//Program to trim the whitespace at both sides of the String

class StringTrim{

   public static void main(String args[]) {
      String Str = new String("   Welcome to              BrightITCareer.com   ");
      System.out.println(Str.trim() );
   }
}

// Program to understand startsWith() and endsWith() functions

package com.core;

public class StringStartsEndsWith {

	public static void main(String args[]) {

		String str = new String("Bright IT Career.com");
		System.out.println("Does string start with Bright: " + str.startsWith("Bright"));
		System.out.println("Does string end with Bright: " + str.endsWith(".com"));

	}
}

// Program to replace a string
package com.core;

public class StringReplace {

	public static void main(String args[]) {
		String Str = new String("The Time is Short, Learn only The Right Things Here to get a JOB");

		// Replaces a character
		System.out.println(Str.replace('a', 'Z'));

		// Replace a string
		System.out.println(Str.replace("Time is", " Time is Precious and "));

		// Replace the first occurrence of the Regular expression with the given
		// String
		System.out.println(Str.replaceFirst("The", " Time is Precious and "));
	}
}

public class compareStrDemo{

   public static void main(String args[]) {
      String str1 = "Bright";
      String str2 = new String("BrightITCareer.com");
      String str3 = new String("bright");
      
      int result = str1.compareTo( str2 );
      System.out.println(result);
      
      result = str2.compareTo( str3 );
      System.out.println(result);
   }
}

// Program to get a sub string of a string 

class SubString {
	public static void main(String args[]) {
		String str = "BrightITCareer";
		System.out.println(str.substring(0, 5));// returns Brigh
		System.out.println(str.substring(0));// returns BrightITCareer
	}
}

// Program to get a sub string of a string 

class ToUpperOrLower {
	public static void main(String args[]) {
		String str = "BrightITCareer";
		// Converts the string to UPPERCASE
		String upper = str.toUpperCase();
		System.out.println(upper);
		// Converts the string to lower case
		String lower = str.toLowerCase();
		System.out.println(lower);
	}
}

public class concatDemo{

   public static void main(String args[]) {
      String s = "Welcome to ";
      s = s.concat(" BrightITCareer.com");
      System.out.println(s);
   }
}

public class Strings {
	public static void main(String[] args){
		String str="Bright IT Career";
		String[] strArray=str.split("\\s", 6);// Try with 0,2 as well 
		for(String s:strArray){
			System.out.println("Sub String is : "+s);
		}
		
		System.out.println("Lenght of the array of strings :"+strArray.length);
	}
	
}

public class CharToStringDemo {

	public static void main(String args[]) {
		// Using toString() method
		char ch = 'a';
		String str = Character.toString(ch);
		System.out.println("String is: " + str);

		// Using valueOf() method
		String str2 = String.valueOf(ch);
		System.out.println("String is: " + str2);
	}
}

// Program to convert an integer to String 
// Throws NumberFormatException in case if all the characters in the string are not digits


public class IntegerToString {

	public static void main(String[] args) {

		int i = 67890;

		// using String.valueOf
		String str = String.valueOf(i);
		System.out.println(str);

		// using Integer.toString(int)

		str = Integer.toString(i);
		System.out.println(i);

	}

}

// Program to convert a string into integer 
// Throws NumberFormatException in case if all the characters in the string are not digits


public class StringToInteger{
	
	public static void main(String[] args){
		
		String str="12345";
		
		// using parseInt method
		int num=Integer.parseInt(str);
		System.out.println(num);
		
		// using Integer.valueOf
		
		int n=Integer.valueOf(str);
		System.out.println(n);
	}	
}

public class countDemo
{ 
	public static void main(String args[])
	{ 
		String s = "welcome to BrightITCareer.com";
 
		int count = 1;
 
		for (int i = 0; i < s.length() - 1; i++)
		{
			if ((s.charAt(i) == ' ') && (s.charAt(i + 1) != ' '))
			{
				count++;
 
			}
		}
 		System.out.println("Number of words in a string = " + count);
	}
}

Exception Handling

import java.io.*;
import java.util.*;
class tryDemo
{
 	public static void main(String args[])
 	{
 	   Scanner s=new Scanner(System.in);
	   System.out.println("Enter the a value:");
	   int a=s.nextInt();
           System.out.println("Enter the b value:");
	   int b=s.nextInt();
 	try
        {
        	int c=a/b;
        }catch(Exception e){
		System.out.println(e);
	}
    }
     
}

import java.util.*;
class ageTryDemo{  
   public static void main(String args[]){  
	Scanner s=new Scanner(System.in);
	System.out.println("Enter the age...");  
	int age=s.nextInt();
 	if(age<18)  
     	 System.out.println("not valid");  
     	else  
     	 System.out.println("welcome to vote");  
  }  
} 

class finallyDemo{  
  public static void main(String args[]){  
  try{  
   int value=50/5;  
   	System.out.println(value);  
  }  
  catch(Exception e){
	System.out.println(e);
	}  
  finally{
	System.out.println("finally block is always executed");
	}  
  }  
} 

class ExceptionDemo{  
  void Bright(){  
    int data=50/0;  
  }  
  void Career(){  
    Bright();  
  }  
  void It(){  
   try{  
    Career();  
   }catch(Exception e){
	System.out.println("exception handled");
	}  
  }  
  public static void main(String args[]){  
   ExceptionDemo obj=new ExceptionDemo();  
   obj.It();  
   System.out.println("normal flow...");  
  }  
} 

public class CheckedExcDemo{
   public static void main (String args[])throws Exception { 
      int s = 10, value = 0;
      try { 
         value = s/0;
         System.out.println("The result is: "+value);
      } catch(ArithmeticException exp) { 
         System.out.println ("Arithmetic exception occoured: "+exp);
         try { 
            throw new NumberFormatException();
         } catch(NumberFormatException exp1) {
            System.out.println ("Chained exception thrown manually : "+exp1);
         }
      }
   }
}

Collections

// Collections.reverse()
import java.util.*;
 
class collectionReverse
{
    public static void main(String[] args)
    {
        // Let us create a list of strings
        ArrayList  list = new ArrayList();
        list.add("Bright");
        list.add("IT");
        list.add("Career");
        list.add("com");
 
        System.out.println("Original List : " + list);
 
        // Here we are using reverse() method
        // to reverse the element order of mylist
        Collections.reverse(list);
 
        System.out.println("Modified List: " + list);
    }
}

// Collections.reverse()
import java.util.*;
 
class collectionReverse
{
    public static void main(String[] args)
    {
        // Let us create a list of strings
        ArrayList  list = new ArrayList();
        list.add("Bright");
        list.add("IT");
        list.add("Career");
        list.add("com");
 
        System.out.println("Original List : " + list);
 
        // Here we are using reverse() method
        // to reverse the element order of mylist
        Collections.reverse(list);
 
        System.out.println("Modified List: " + list);
    }
}

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
public class collectionIterateDemo {
 
	public static void main(String[] argv) {
 
		// create list
		List List = new ArrayList();
 
		// add 4 different values to list
		List.add("JavaSE");
		List.add("JavaEE");
		List.add("Hibernate");
		List.add("Spring");
 
		// iterate via "for loop"
		System.out.println(" .For Loop Example. ");
		for (int i = 0; i < List.size(); i++) {
			System.out.println(List.get(i));
		}
 
		// iterate via "New way to loop"
		System.out.println(" Advance For Loop Example..");
		for (String temp : List) {
			System.out.println(temp);
		}
 
		// iterate via "iterator loop"
		System.out.println(" Iterator Example...");
		Iterator Iterator = List.iterator();
		while (Iterator.hasNext()) {
			System.out.println(Iterator.next());
		}
	}
}

import java.util.*;

public class collectionSizeDemo {
   public static void main(String args[]) {
   // create hash set
   HashSet  set = new HashSet ();
                  
   // populate hash set
   set.add("Bright"); 
   set.add("IT");
   set.add("Career");  
      
   System.out.println("Size of the set: "+set.size());     
   }    
}

import java.util.LinkedHashSet;
 
public class collectionHashDemo {
 
  public static void main(String[] args) {
 
    // Create a LinkedHashSet and populate it with elements
    LinkedHashSet HashSet = new LinkedHashSet();
    HashSet.add("Brighty");
    HashSet.add("IT");
    HashSet.add("Career");

    // boolean contains(Object value) method returns true if the LinkedHashSet contains the value, otherwise false.
    boolean exists = HashSet.contains("Career");
    System.out.println("Career exists in LinkedHashSet ? : " + exists);
 
  }
}

import java.util.TreeSet;
 
public class collectionTreeSetDemo {
 
  public static void main(String[] args) {
 
    //create object of TreeSet
    TreeSet tSet = new TreeSet();
   
    //add elements to TreeSet object
    tSet.add(new Integer("1"));
    tSet.add(new Integer("2"));
    tSet.add(new Integer("3"));
   
    System.out.println("TreeSet before removal : " + tSet);
   
    boolean bRemove = tSet.remove(new Integer("3"));
    System.out.println("Was 3 removed from TreeSet ? " + bRemove);
   
    System.out.println("TreeSet after removal : " + tSet);
  }
}

import java.util.*;
class sortedArrayDemo  {

	public static void main(String args[]){
	   ArrayList arraylist = new ArrayList();
	   arraylist.add("AA");
	   arraylist.add("ZZ");
	   arraylist.add("CC");
	   arraylist.add("FF");

	   /*Unsorted List: ArrayList content before sorting*/
	   System.out.println("Before Sorting:");
	   for(String str: arraylist){
			System.out.println(str);
		}

	   /* Sorting in decreasing order*/
	   Collections.sort(arraylist, Collections.reverseOrder());

	   /* Sorted List in reverse order*/
	   System.out.println("ArrayList in descending order:");
	   for(String str: arraylist){
			System.out.println(str);
		}
	}
}

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class collectionArrayListDemo {
    public static void main(String a[]){
        List li = new ArrayList();
        ListIterator litr = null;
        li.add(2);
        li.add(8);
        li.add(9);
        li.add(7);
        li.add(6);
        litr=li.listIterator();
        System.out.println("Elements in forward directiton");
        while(litr.hasNext()){
            System.out.println(litr.next());
        }
     }
}

import java.util.HashMap;
import java.util.Map;

public class keyhashDemo {

    public static void main(String[] args) {

        Map map = new HashMap<>();
        map.put("Spring", 1);
        map.put("Hibernate", 2);
        map.put("JavaEE", 3);
        map.put("watermelon", null);

        System.out.println("1. Is key 'Spring' exists?");
        if (map.containsKey("Spring")) {
            //key exists
            System.out.println("yes! - " + map.get("Spring"));
        } else {
            //key does not exists
            System.out.println("no!");
        }

        System.out.println("\n2. Is key 'JavaEE' exists?");
        if (map.containsKey("JavaEE")) {
            System.out.println("yes! - " + map.get("JavaEE"));
        } else {
            System.out.println("no!");
        }


    }

}

import java.util.LinkedList;
 /** * Java Program to search an element inside LinkedList. 
  * LinkedList doesn't provide random search and 
  * time complexity of searching is O(n) 
  *
  * @BrightITCareer 
  *
  */ 
   public class SearchLinkedDemo { 
	public static void main(String args[]) { 
	LinkedList list = new LinkedList<>(); 
	
	list.add(100); 
	list.add(105); 
	list.add(104); 
	list.add(103); 
	list.add(102); 
	list.add(101); 

	System.out.println("First index of 103 is : " + list.lastIndexOf(103)); 
	System.out.println("Last index of 103 is : " + list.lastIndexOf(103)); 
	System.out.println("First index of 102 is : " + list.indexOf(102)); 
	System.out.println("Last index of 102 is : " + list.lastIndexOf(102)); 		 
	} 
} 

import java.util.Collections;
import java.util.Vector;
public class SwapDemo {
	public static void main(String[] args) {
		Vector v = new Vector();
		
		v.add("Career");
		v.add("IT");
		v.add("Bright");
		v.add(".com");
		System.out.println(v);
		Collections.swap(v, 0, 2);
		System.out.println("After swapping");
		System.out.println(v);
	}
}

import java.util.LinkedList;

public class UpdateDemo {
   public static void main(String[] a) {
      LinkedList list = new LinkedList();
      list.add("Britht");
      list.add("IT");
      list.add("Career");
      list.add(".com");

      System.out.println(list);
      list.set(1, "and");
      System.out.println(list);
   }
}

import java.util.Collections;
import java.util.Vector;

public class maxVectorDemo {
   public static void main(String[] args) {
      Vector v = new Vector();
      v.add(new Double("14.4324"));
      v.add(new Double("13.3532"));
      v.add(new Double("35.342"));
      v.add(new Double("8.349"));
      v.add(new Double("2.3"));
      Object obj = Collections.max(v);
      Object obj1 = Collections.min(v);
      System.out.println("The max element is:"+obj);
      System.out.println("The max element is:"+obj1);
   }
}

import java.util.*;

public class GetLinkedDemo {

   public static void main(String[] args) {

   // create a LinkedList
   LinkedList list = new LinkedList();

   // add some elements
   list.add("Bright");
   list.add(2);
   list.add("Career");
   list.add("10");

   // print the list
   System.out.println("LinkedList:" + list);

   // print element at index 2
   System.out.println("Element at index 3 :" + list.get(2));
   }
}

/* logic: characters are inserted as keys and their count as values. 
 * If map contains the char already then increase the value by 1
 */

package com.core;
import java.util.HashMap;
import java.util.Set;

public class CharCount {
	public static void main(String[] args) {
		String str = "Bright IT Career";
		// Creating a HashMap
		HashMap map = new HashMap();

		char[] charString = str.toCharArray();

		for (Character ch : charString) {
			if (map.containsKey(ch)) {
				map.put(ch, map.get(ch) + 1);
			} else {
				map.put(ch, 1);
			}
		} // for
		// Printing the values of each each ke
		Set set = map.keySet();
		for (Character ch : set) {
			System.out.println("Char : " + ch + " Value :  " + map.get(ch));
		}
	}
}

import java.util.Scanner;
 
public class OddandEvenDemo {
 
public static void main(String []args )    {
 
    int number;
    Scanner in= new Scanner(System.in);
 
    System.out.println("Enter a number to check even or odd");
    number=in.nextInt();
 
    if((number % 2)==0){
        System.out.println(+number+" is Even number");
    }else{
        System.out.println(+number+" is Odd Number");
    }
 
}
}

Stay in the loop with our newsletter!