Category Archives: Collection

Frequently asked questions in java collections framework. We will discuss various java collections like linkedlist, arraylist, hashmap, concurrenthashmap, queue ,priorityqueue etc.

Sort user defined objects in TreeSet (using Comparable)

Elements in TreeSet are ordered using natural order or Comparator/Comparable interface. In case of user defined objects or POJO, POJO needs to implement Comparable interface to sort objects. TreeSet will invoke the compareTo method of POJO, to sort POJOs (contained in TreeSet) We have already discussed to sort user defined objects using Comparator interface. Example – Sort Car POJO… Read More »

Sort user defined objects using TreeSet in java (Comparator interface)

What is TreeSet? TreeSet is a NavigableSet implementation based on a TreeMap. Elements in TreeSet are ordered using natural order or Comparator/Comparable interface. In case of user defined objects or POJO, we need to supply the Compartor (to specify on which fields, we would like to sort objects) Comparator is specified during construction of TreeSet.… Read More »

Convert LinkedHashSet collection of String objects to Array in java (example)

Hashtable and linked list implementation of the Set interface, with predictable iteration order. LinkedHashSet maintains the Insertion order of elements using LinkedList LinkedHashSet is UnSynchronized and not thread safe. Given a LinkedHashSet containing String objects in java. Set<String> computerGenerations = new LinkedHashSet<>() We would like to convert LinkedHashSet to Array of Strings in java. We… Read More »

Convert LinkedHashSet to ArrayList in java (example)

Given a LinkedHashSet containing String objects. Convert LinkedHashSet to ArrayList of strings. Set<String> cmputerGenerations = new LinkedHashSet<>(); Program – Convert LinkedHashSet to ArrayList 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(“Set: “+… Read More »

Iterate or loop LinkedHashSet collection of String objects in java8 (with example)

Hashtable and linked list implementation of the Set interface, with predictable iteration order. LinkedHashSet maintains the Insertion order of elements using LinkedList LinkedHashSet is UnSynchronized and not thread safe. 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… Read More »

Convert LinkedHashSet to TreeSet in java (example)

Given a LinkedHashSet containing Computer’s generation (String). Set<String> cmputerGenerations = new LinkedHashSet<>(); Convert LinkedHashSet to TreeSet. String objects will be sorted using TreeSet class. Program – Convert LinkedHashSet to TreeSet in java package org.learn.collection.set.lhset; import java.util.LinkedHashSet; import java.util.Set; import java.util.TreeSet; public class DemoLinkedHashSetToTreeSet { public static void main(String[] args) { Set<String> cmputerGenerations = new LinkedHashSet<>(); cmputerGenerations.add(“VacuumTubes”);… Read More »

Page 1 of 612345...Last »