Collection: Sort to list Element, Convert Array to List, Covertion List to Array, Use of Iterator , Add collection in List

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


public class List1
{

public static void main(String a[])
{
List<Integer> l1= new ArrayList<Integer>();
l1.add(21);
l1.add(2);
l1.add(54);

System.out.println( "total no of list elements:\n"+l1.size());
System.out.println(l1);


List<Integer> l2= new ArrayList<Integer>();
   l2.add(211);
l2.add(211);
l2.add(5411);
System.out.println("Add the another collection in list\n");
l1.addAll(l2);
System.out.println( "After Addition of additional collection  in List, Size of list:\n"+l1.size());
System.out.println(l1);
//l1.retainAll(l2);




System.out.println( "verify list  contains  additional collection \n");
        if (l1.containsAll(l2))
System.out.println(true);

else
System.out.println(false);


System.out.println("sort the data of array\n");
Collections.sort(l1);


//clear the Element of the Array
    //l1.clear();



System.out.println("Iterate the Element of List\n");
Iterator<Integer> i1=l1.iterator();
while(i1.hasNext())
{
System.out.println(i1.next());
}



System.out.println("list to Array Conversion\n");
       Integer[] intArray=l1.toArray(new Integer[l1.size()]);
       for (Integer i : intArray)
       {
        System.out.println(i);
   }
     
     
       System.out.println("Array  to List Conversion\n");
    List<Integer> listOfArray=Arrays.asList(intArray);
    System.out.println(listOfArray);



}

}




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

Output:

total no of list elements:
3
[21, 2, 54]
Add the another collection in list

After Addition of additional collection  in List, Size of list:
6
[21, 2, 54, 211, 211, 5411]
verify list  contains  additional collection 

true
sort the data of array

Iterate the Element of List

2
21
54
211
211
5411
list to Array Conversion

2
21
54
211
211
5411
Array  to List Conversion

[2, 21, 54, 211, 211, 5411]

Post a Comment

2 Comments

  1. You are doing a good job and sharing your knowledge to others! it was one of the pretty post to read and useful to improve the knowledge as updated one, keep doing the good work.
    Selenium Training in Electronic City

    ReplyDelete
  2. I have been searching for a useful post like this on salesforce course details, it is highly helpful for me and I have a great experience with this Salesforce Training who are providing certification and job assistance. Salesforce training price in Gurgaon 

    ReplyDelete
Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)