Convert LinkedHashSet of String objects to ArrayList collection (java/ example)

  1. LinkedHashSet is Hashtable & linked list implementation of the Set interface, with predictable iteration order.
  2. LinkedHashSet maintains the Insertion order of elements using LinkedList
  3. LinkedHashSet is UnSynchronized and not thread safe.
  4. Iterator of LinkedHashSet is fail-fast.
    • Iterator will throw ConcurrentModificationException, if LinkedHashSet modified at any time after the iterator is created, in any way except through the iterator’s own remove method.
  5. Given a LinkedHashSet collection of String objects in java.
    • Set<String> cmputerGenerations = new LinkedHashSet<>();
  6. Convert LinkedHashSet of String object to ArrayList collection in java (with example)

1. Convert LinkedHashSet of String objects to ArrayList collection in java

package org.learn.collection.set.lhset;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;

public class DemoLinkedHashSetToArrayList {

	public static void main(String[] args) {
		Set<String> cmputerGenerations = new LinkedHashSet<>();
		cmputerGenerations.add("VacuumTubes");
		cmputerGenerations.add("Transistors");
		cmputerGenerations.add("IntegratedCircuits");
		cmputerGenerations.add("Microprocessors");
		cmputerGenerations.add("ArtificialIntelligence");		
		
		System.out.println("1. LinkedHashSet: "+ cmputerGenerations);
		ArrayList<String>linkedHashSetToArrayList = new ArrayList<>(cmputerGenerations);
		System.out.println("2. ArrayList:"+ linkedHashSetToArrayList);
	}
}

2. Convert LinkedHashSet of String objects to ArrayList collection (example)

1. LinkedHashSet: [VacuumTubes, Transistors, IntegratedCircuits, Microprocessors, ArtificialIntelligence]
2. ArrayList:[VacuumTubes, Transistors, IntegratedCircuits, Microprocessors, ArtificialIntelligence]
Scroll to Top