Skip to main content

Java Interview Programs



Strings:

1.) Print Duplicates of an array with one Loop statement?



package com.java.testing;

import java.util.HashSet;
import java.util.Set;

class MyClass
{
public static Set<Integer> commonNumbers = new HashSet<>();

public Integer digits;

public MyClass(Integer digits) {
this.digits=digits;
}

@Override
public int hashCode() {
return 0;
}

@Override
public boolean equals(Object obj) {
if(obj instanceof MyClass)
{
if(this.digits.equals(((MyClass)obj).digits))
{
commonNumbers.add(((MyClass)obj).digits);
return true;
}
}
return false;
}
}

public class ObenStringQuestion {

public static void main(String[] args){

int[] mobileno = new int[]{1,8,8,8,8,8,8,1,4,2,8,2,1,1,6,2};

Set<MyClass> xx = new HashSet<>();

for(int i=0;i<mobileno.length;i++)
{
xx.add(new MyClass(mobileno[i]));
}

for (MyClass object : xx) {

System.out.println(object.digits);
}

System.out.println(MyClass.commonNumbers);
}

}

Comments

Popular posts from this blog

Use of Static Imports in Java with Example

The main reason behind the static import feature in java5 is to reduce the unnecessary reference of class name to call static methods/fields. package   import.static.test ; import   static   java.lang.Integer.MAX_VALUE ; import   static   java.lang.Integer.MIN_VALUE ; import   static   java.lang.System.out ; public   class  StaticImportExample  {      public   static   void  main ( String  args [])   {                //without Static import          System . out . println ( "Maximum value of int variable in Java without "  + "static import : "   +  Integer . MAX_VALUE ) ;          System . out . println ( "Minimum value of int variable in Java without "  + static import : "  +  Integer . MIN_VALUE ) ;             ...

COMPARING CONTAINERS AND VIRTUAL MACHINES

Containers and virtual machines have similar resource isolation and allocation benefits -- but a different architectural approach allows containers to be more portable and efficient. VIRTUAL MACHINES: Virtual machines include the application, the necessary binaries and libraries, and an entire guest operating system -- all of which can amount to tens of GBs. CONTAINERS: Containers include the application and all of its dependencies --but share the kernel with other containers, running as isolated processes in user space on the host operating system. Docker containers are not tied to any specific infrastructure: they run on any computer, on any infrastructure, and in any cloud.