Shuffle elements in linked list in java

We can use Collections.shuffle() method to shuffle elements in linked list in java. It generates different order of output every time when it is invoked. Syntax: Collections.shuffle(linkedList); Example: package com.w3schools;   import java.util.Collections; import java.util.LinkedList;   public class Test { public static void main(String args[]){ LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add("Jai"); linkedList.add("Mahesh"); linkedList.add("Naren"); linkedList.add("Vivek"); linkedList.add("Vishal"); … Read more

Reverse linked list in java

We can use Collections.reverse() method to reverse linked list in java. Syntax: Collections.reverse(linkedList); Example: package com.w3schools;   import java.util.Collections; import java.util.LinkedList;   public class Test { public static void main(String args[]){ LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add("Jai"); linkedList.add("Mahesh"); linkedList.add("Naren"); linkedList.add("Vivek"); linkedList.add("Vishal"); linkedList.add("Hemant"); System.out.println("Actual LinkedList:"+linkedList); Collections.reverse(linkedList); System.out.println("Results after reverse operation:" + linkedList); } }package com.w3schools; import … Read more

Sort linkedlist using comparator in java

We can use Collections.sort() method to sort linkedlist using comparator in java. We have to pass Comparator object in it. Example: package com.w3schools;   import java.util.Collections; import java.util.Comparator; import java.util.LinkedList;   class Employee{ private String name; private int salary;   public Employee(String name, int salary){ this.name = name; this.salary = salary; }   public String … Read more

Linked list to array in java

We can use toArray() method to convert linked list to array in java. Syntax: linkedList.toArray(strArray); Example: package com.w3schools;   import java.util.LinkedList;   public class Test { public static void main(String args[]){ LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add("Jai"); linkedList.add("Mahesh"); linkedList.add("Naren"); linkedList.add("Vivek"); linkedList.add("Vishal"); linkedList.add("Hemant"); System.out.println("Actual LinkedList:"+linkedList); String[] strArray = new String[linkedList.size()]; linkedList.toArray(strArray); System.out.println("Array created:"); for(String str:strArray){ System.out.println(str); … Read more

Remove all elements from a linked list in java

We can use clear() method to remove all elements from a linked list in java. Syntax: linkedList.clear(); Example: package com.w3schools;   import java.util.LinkedList;   public class Test { public static void main(String args[]){ LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add("Jai"); linkedList.add("Mahesh"); linkedList.add("Naren"); linkedList.add("Vivek"); linkedList.add("Vishal"); linkedList.add("Hemant"); System.out.println("Actual LinkedList:"+linkedList); linkedList.clear(); System.out.println("LinkedList after clear :"+linkedList); } }package com.w3schools; import … Read more

Add all elements of a list to linkedlist in java

We can use addAll() method to add all elements of a list to linkedlist in java. Syntax: linkedList.addAll(list); Example: package com.w3schools;   import java.util.ArrayList; import java.util.LinkedList; import java.util.List;   public class Test { public static void main(String args[]){ LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add("Jai"); linkedList.add("Mahesh"); linkedList.add("Naren"); linkedList.add("Vivek"); linkedList.add("Vishal"); linkedList.add("Hemant"); System.out.println("Actual LinkedList:"+linkedList); List<String> list = new … Read more

Copy or clone linked list in java

We can use clone() method to copy or clone linked list in java. Syntax: linkedList.clone(); Example: package com.w3schools;   import java.util.LinkedList;   public class Test { public static void main(String args[]){ LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add("Jai"); linkedList.add("Mahesh"); linkedList.add("Naren"); linkedList.add("Vivek"); linkedList.add("Vishal"); linkedList.add("Hemant"); System.out.println("Actual LinkedList:"+linkedList); LinkedList<String> copy = (LinkedList<String>) linkedList.clone(); System.out.println("Cloned LinkedList:"+copy); } }package com.w3schools; import … Read more

Read all elements in linkedlist in java

hasNext(): It returns true if the iteration has more elements. next(): It returns the next element in the iteration. Example: package com.w3schools;   import java.util.Iterator; import java.util.LinkedList;   public class Test { public static void main(String args[]){ LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add("Jai"); linkedList.add("Mahesh"); linkedList.add("Naren"); linkedList.add("Vivek"); linkedList.add("Vishal"); linkedList.add("Hemant"); Iterator<String> iterator = linkedList.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } … Read more

Swap two elements in an arraylist in java

We can use Collections.swap() method to swap two elements in an arraylist in java. We have to pass the indexes which we need to swap. Syntax: Collections.swap(arrayList, element1, element2); Example: package com.w3schools;   import java.util.ArrayList; import java.util.Collections;   public class Test { public static void main(String args[]){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Jai"); arrayList.add("Mahesh"); arrayList.add("Vivek"); … Read more

Shuffle elements in an arraylist in java

We can use shuffle() method which generates different order of output, to shuffle elements in an arraylist in java. Syntax: Collections.shuffle(arrayList); Example: package com.w3schools;   import java.util.ArrayList; import java.util.Collections;   public class Test { public static void main(String args[]){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Jai"); arrayList.add("Mahesh"); arrayList.add("Vivek"); arrayList.add("Naren"); arrayList.add("Hemant"); arrayList.add("Vishal"); System.out.println("Actual ArrayList:"+arrayList); Collections.shuffle(arrayList); System.out.println("Results after … Read more