Core Java Tutorials


Introduction

Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package

Array

Exception Handling

String

Immutable Class

File Handling

Serialization

MultiThreading

Volatile

Concurrent Package

Collection

Collection Internal

Generics

Cloning

Reflection In Java

Annotation

Class Loader

Java Inner classes

Garbage Collector

JDBC

References In Java

Heap Dump & Thread Dump

LinkedHashSet in Collection class Hierarchy:-


LinkedHashSet Example with String:-

import java.util.*;
public class LinkedHashSetExample {
  public static void main(String[] args) {
	//it will maintained the order
	//Please refer this Biscuit class from HashSet
	Set<Biscuit> linkedSet = new LinkedHashSet<>();
	Biscuit biscuit1=new Biscuit(1,"ParleG","sweet","open and eat");
	Biscuit biscuit2=new Biscuit(2,"ParleG","sweet","open and eat");
	Biscuit biscuit3=new Biscuit(3,"ParleG","salty","open and eat");
	Biscuit biscuit4=new Biscuit(4,"ParleG","sweet","open and eat");
	Biscuit biscuit5=new Biscuit(5,"ParleG","salty","open and eat");
		
	linkedSet.add(biscuit1);
	linkedSet.add(biscuit2);
	linkedSet.add(biscuit3);
	linkedSet.add(biscuit4);
	linkedSet.add(biscuit5);
	// retrieving all the elements.jdk 1.5
	for (Biscuit biscuit : linkedSet) {
		System.out.println("output =" + biscuit);
	}
 }
}
 output:-
output = Biscuit [bid=1, vendor=ParleG, taste=sweet, howtoeat=open and eat]
output = Biscuit [bid=2, vendor=ParleG, taste=sweet, howtoeat=open and eat]
output = Biscuit [bid=3, vendor=ParleG, taste=salty, howtoeat=open and eat]
output = Biscuit [bid=4, vendor=ParleG, taste=sweet, howtoeat=open and eat]
output = Biscuit [bid=5, vendor=ParleG, taste=salty, howtoeat=open and eat]


  LinkedHashSet class provides all of the optional Set operations, and permits null elements.

Like HashSet, it provides constant-time performance for the basic operations add,contains and remove, assuming the hash function disperses elements properly among the buckets.

Performance is likely to be just slightly below that of HashSet, due to the added expense of maintaining the linked list, with one exception: Iteration over a LinkedHashSet requires time proportional to the size of the set, regardless of its capacity.