Simple Java
Linked Hash Set
import
java.util.LinkedHashSet;
public class
SimpleLinkedHashSetExample
{
public static void
main(String[] args)
{
LinkedHashSet lhashSet
= new LinkedHashSet();
lhashSet.add(new
Integer("1"));
lhashSet.add(new
Integer("2"));
lhashSet.add(new
Integer("3"));
System.out.println("LinkedHashSet
contains.." + lhashSet);
}
}
Add elements at
beginning and end of Linked List Java
import
java.util.LinkedList;
public class
AddElementsAtStartEndLinkedListExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("LinkedList contains : " + lList);
lList.addFirst("0");
System.out.println("After
inserting 0 at beginning, LinkedList contains :"
+ lList);
lList.addLast("6");
System.out.println("After
appending 0 at end, LinkedList contains :" + lList);
}
}
Check if a
particular element exists in Linked List Java
import
java.util.LinkedList;
public class
CheckElementLinkedListExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
boolean blnElement =
lList.contains("4");
if(blnElement)
{
System.out.println("LinkedList
contains 4");
}
else
{
System.out.println("LinkedList
does not contain 4");
}
}
}
Create an
object array from elements of Linked List Java
import
java.util.LinkedList;
public class
ObjectArrayFromElementsOfLinkedListExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
Object[] objArray =
lList.toArray();
System.out.println("Object
array created from Java LinkedList.");
System.out.println("Object
array contains : ");
for(int i=0;
i
{
System.out.println(objArray[i]);
}
}
}
Get Sub List
from Linked List Java
import java.util.LinkedList;
import
java.util.List;
public class
GetSubListLinkedListJavaExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("LinkedList contains : " + lList);
List lst =
lList.subList(1,4);
System.out.println("Sublist
contains : " + lst);
lst.remove(2);
System.out.println("Sublist
now contains : " + lst);
System.out.println("Original
LinkedList now contains : " + lList);
}
}
Get elements
from Linked List Java
import
java.util.LinkedList;
public class
GetElementsLinkedListExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("LinkedList
contains : ");
for(int i=0; i<
lList.size(); i++)
{
System.out.println(lList.get(i));
}
}
}
Get first and
last elements from Linked List Java
import
java.util.LinkedList;
public class
GetFirstAndLastElementLinkedListExample
{
public static void
main(String[] args)
{
LinkedList lList = new
LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("First
element of LinkedList is : " + lList.getFirst());
System.out.println("Last
element of LinkedList is : " + lList.getLast());
}
}
No comments:
Post a Comment