Friday, 30 January 2015

Java Program To Convert String To Integer and Integer to String

public class StringToInteger
{
 public static void main(String[] args) 
 {
  String s = "2015";
  
  int i = Integer.parseInt(s);
  
  System.out.println(i);          //Output : 2015
 }
}

public class IntegerToString
{
 public static void main(String[] args) 
 {
  int i = 2015;
  
  String s = Integer.toString(i);
  
  System.out.println(s);     //Output : 2015
 }
}

Thursday, 15 January 2015

Java program for without duplicate elements from the two arrayList Objects

We can retrieve elements without duplicate elements from the two arrayList Objects by using Set interface.we can add the two arrayList Objects to set by using addAll() method.

Program :

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Resources;
public class ArrayList1 {

public static void main(String[] args) {

List l1=new ArrayList();
l1.add(1);
l1.add(2);
l1.add(3);

List l2=new ArrayList();

l2.add(3);
l2.add(2);
l2.add(4);
l2.add(1);
l2.add(5);

List l3=new ArrayList();

Set<Integer> s=new HashSet<Integer>();
s.addAll(l1);
s.addAll(l2);
System.out.println(s);
}
}

Out put:
[1, 2, 3, 4, 5]

What is sublist in java and how to get the sublist from the existing Arrayist?

Sublist is used to get the sublist from the existing ArrayList.

List subList(int fromIndex, int toIndex)

Here fromIndex is inclusive and toIndex is exclusive.In this we will see how to get a sublist from an existing ArrayList. We will be doing it using the subList method of ArrayList class.

List subList(int fromIndex, int toIndex)

Here fromIndex is inclusive and toIndex is exclusive. There are few important points regarding this method which I have shared at the end of this post.



Example of getting sub-list from an ArrayList

Points to Note in the below example:

The subList method returns a list therefore to store the sublist in another ArrayList we must need to type cast the returned value in same way as I did in the below example. On the other side if we are storing the returned sublist into a list then there is no need to type cast (Refer the example).

package beginnersbook.com;
import java.util.ArrayList;
import java.util.List;
public class SublistExample {

 public static void main(String a[]){
     ArrayList<String> al = new ArrayList<String>();
     al.add("Steve");
     al.add("Justin");
     al.add("Ajeet");
     al.add("John");
     al.add("Arnold");
     al.add("Chaitanya");

     System.out.println("Original ArrayList Content: "+al);

     //Sublist to ArrayList
     ArrayList<String> al2 = new ArrayList<String>(al.subList(1, 4));
     System.out.println("SubList stored in ArrayList: "+al2);

     //Sublist to List
     List<String> list = al.subList(1, 4);
     System.out.println("SubList stored in List: "+list);
  }
}


Output:

Original ArrayList Content: [Steve, Justin, Ajeet, John, Arnold, Chaitanya]
SubList stored in ArrayList: [Justin, Ajeet, John]
SubList stored in List: [Justin, Ajeet, John]