Skip to main content

Posts

Showing posts from August, 2016

How To Remove Duplicate Elements From an ArrayList In Java?

In This Tutorial we will learn How To Remove Duplicate Elements From An ArrayList In java. Steps needs to be followed to Remove duplicate elements from an Arraylist in java. create an ArrayList Object and add elements to the ArrayList which includes Duplicate elements. Now create Linkedhashset instance and copy all the elements of ArrayList To LinkedHashSet. Here one point to note why we are choosing LinkedHashSet,because LinkedHashSet removes the duplicate elements and it follows Insertion order. Now empty the arraylist object by calling clear() method.and then Copying all the elements of LinkedHashSet To The ArrayList That,s it.you can see the below code. code: package com.practice; import java.util.ArrayList; import java.util.LinkedHashSet; public class RemveDupArrayList { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList&ltInteger&gt arraylist = new ArrayList&ltInteger&gt(); arraylist.add(10); arraylist....

How To Convert ArrayList into Array in Java

In This Tutorial We will Learn How To Convert ArrayList Into Array In java. We can do this by calling toArray() method .in ArrayList class We have toArray() method ,by using this method we can convert ArrayList into Array in java. Object[] toArray() method Returns an array containing all of the elements in this ArrayList in proper sequences. code: package com.practice; import java.util.ArrayList; public class ConvertArrayListToArray { public static void main(String[] args) { ArrayList&ltInteger&gt arraylist = new ArrayList&ltInteger&gt(); arraylist.add(10); arraylist.add(20); arraylist.add(30); arraylist.add(40); System.out.println("ArrayList Before conversion:"+arraylist); Integer intArray[] = new Integer[arraylist.size()]; intArray = arraylist.toArray(intArray); for(int x :intArray){ System.out.println(x); } } } output: ArrayList Before conversion:[10, 20, 30, 40] 10 20 30 40

Top Method Overriding Interview Questions And answers In Java

1.what is method overriding? If subclass has the same method name and same method arguments as declared in the super class is known as method overriding in java. Advantage of method overriding : using method overriding we can implement the super class method according to the specification of sub class. The Following code shows the method overriding. package com.practice; public class Parent { public void print(){ System.out.println("parent class print method"); } } package com.practice; public class Child extends Parent { public void print(){ System.out.println("im child class print method"); } public static void main(String[] args) { // TODO Auto-generated method stub Child c = new Child(); c.print(); } } output im child class print method in above example we are modifying the super class method in sub class . This is known as method overriding. 2.What are the rules of method overriding in java? The Following Rules Need to be followed while me...