Monday, January 12, 2015

Java Important Program



How to reverse Linked List content

package com.java2novice.linkedlist;
import java.util.Collections;
import java.util.LinkedList;
public class MyLinkedListReverse
{
public static void main(String a[])
{
        LinkedList list = new LinkedList();
        list.add("Java");
        list.add("Cric");
        list.add("Play");
        list.add("Watch");
        list.add("Glass");
        Collections.reverse(list);
        System.out.println("Results after reverse operation:");
        for(String str: list)
        {
            System.out.println(str);
        }
    }
}

How to shuffle elements in Linked List

package com.java2novice.linkedlist;
import java.util.Collections;
import java.util.LinkedList;
public class MyLinkedListShuffle
{
public static void main(String a[])
{
        LinkedList list = new LinkedList();
        list.add("Java");
        list.add("Cric");
        list.add("Play");
        list.add("Watch");
        list.add("Glass");
        list.add("Movie");
        list.add("Girl");
        Collections.shuffle(list);
        System.out.println("Results after shuffle operation:");
        for(String str: list)
       {
            System.out.println(str);
        }
    Collections.shuffle(list);
        System.out.println("Results after shuffle operation:");
        for(String str: list)
        {
            System.out.println(str);
        }
    }
}

How to swap two elements in a Linked List

package com.java2novice.linkedlist;
import java.util.Collections;
import java.util.LinkedList;
public class MyLinkedListSwap
{
 public static void main(String a[])
 {
        LinkedList list = new LinkedList();
        list.add("Java");
        list.add("Cric");
        list.add("Play");
        list.add("Watch");
        list.add("Glass");
        list.add("Movie");
        list.add("Girl");
        Collections.swap(list, 2, 5);
        System.out.println("Results after swap operation:");
        for(String str: list)
       {
            System.out.println(str);
        }
    }
}

How to convert list to csv string format

package com.java2novice.linkedlist;
import java.util.LinkedList;
import java.util.List;
public class MyListToCsvString
{
public String getListAsCsvString(List list)
{
        StringBuilder sb = new StringBuilder();
        for(String str:list)
        {
            if(sb.length() != 0)
        {
                sb.append(",");
            }
            sb.append(str);
        }
        return sb.toString();
    }
              public static void main(String a[])
   {
    List li1 = new LinkedList()
    {
            {
                this.add("animal");
                this.add("nuts");
                this.add("java");
            }
        };
        MyListToCsvString mtc = new MyListToCsvString();
        System.out.println(mtc.getListAsCsvString(li1));
        List li2 = new LinkedList()
         {
            {
                this.add("java");
                this.add("unix");
                this.add("c++");
            }
        };
        System.out.println(mtc.getListAsCsvString(li2));
    }
}

How to add element at first position in Linked List

package com.java2novice.linkedlist;
import java.util.LinkedList;
public class MyAddFirst
{
public static void main(String a[])
{
        LinkedList arrl = new LinkedList();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        System.out.println(arrl);
        System.out.println("Adding element at first position...");
        arrl.addFirst("I am first");
        System.out.println(arrl);
        System.out.println("Adding element at first position...");
        arrl.offerFirst("I am first - 2");
        System.out.println(arrl);
    }
}

No comments:

Post a Comment