Category Archives: Set

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 of String objects to ArrayList collection (java/ example)

LinkedHashSet is Hashtable & 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… 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 of String objects to TreeSet collection (java/ example)

LinkedHashSet is Hashtable & 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… Read More »

Page 1 of 212