dir.by  
Поиск >>

Компьютер, программы
→
Java
→
Java (class, interface, collections, etc.)
 class LinkedHashSet in Java 
посмотрели 1315 раз
обновлено: 9 October 2026
class LinkedHashSet<E>   implements interface Set<E>

LinkedHashSet implementation the same as HashSet and additionally maintains a doubly-linked list running through all of its elements. This linked list makes guarantees order of items in the set.

class LinkedHashSet is not synchronized. If multiple threads access a LinkedHashSet concurrently, and at least one of the threads modifies the LinkedHashSet, then LinkedHashSet must be synchronized. Usually this is synchronization, it is done like this:
Set mySet1 = Collections.synchronizedSet( new LinkedHashSet(...) );

 
  Java  
class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable, Serializable
{
     // Constructors
     LinkedHashSet()    
     LinkedHashSet(Collection<? extends E> c)    
     LinkedHashSet(int initialCapacity)    
     LinkedHashSet(int initialCapacity, float loadFactor)

     // Methods
     Spliterator<E> spliterator()

     // Methods inherited from class java.util.HashSet
     boolean add(E e)
     void clear()
     Object clone()
     boolean contains(Object o)
     boolean isEmpty()
     Iterator<E> iterator()
     boolean remove(Object o)
     int size()

     // Methods inherited from class java.util.AbstractSet
     boolean equals(Object o)
     int hashCode()
     boolean removeAll(Collection<?> c)

     // Methods inherited from class java.util.AbstractCollection
     boolean addAll(Collection<? extends E> c)
     boolean containsAll(Collection<?> c)
     boolean retainAll(Collection<?> c)
     Object[] toArray()
     <T> T[] toArray(T[] a)
     String toString()

     // Methods inherited from class java.lang.Object
     void finalize()
     Class<?> getClass()
     void notify()
     void notifyAll()
     void wait()
     void wait(long timeout)
     void wait(long timeout, int nanos)

     // Methods inherited from interface java.util.Set
     boolean add(E e)
     boolean addAll(Collection<? extends E> c)
     void clear()
     boolean contains(Object o)
     boolean containsAll(Collection<?> c)
     boolean equals(Object o)
     int hashCode()
     boolean isEmpty()
     Iterator<E> iterator()
     boolean remove(Object o)
     boolean removeAll(Collection<?> c)
     boolean retainAll(Collection<?> c)
     int size()
     Object[] toArray()
     <T> T[] toArray(T[] a)

     // Methods inherited from interface java.util.Collection
     Stream<E> parallelStream()
     boolean removeIf(Predicate<? super E> filter)
     Stream<E> stream()

     // Methods inherited from interface java.lang.Iterable
     void forEach(Consumer<? super T> action)
}


Learn more about methods in the Java documentation:https://docs.oracle.com ...
class LinkedHashSet[<]E[>] extends HashSet<E> implements SequencedSet<E>, Cloneable, java.io.Serializable {

public LinkedHashSet(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor, true);
}

public LinkedHashSet(int initialCapacity) {
super(initialCapacity, .75f, true);
}

// Constructs a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).
public LinkedHashSet() {
super(16, .75f, true);
}

@SuppressWarnings("this-escape")
public LinkedHashSet(Collection<? extends E> c) {
super(HashMap.calculateHashMapCapacity(Math.max(c.size(), 12)), .75f, true);
addAll(c);
}

@Override
public Spliterator<E> spliterator() {
return Spliterators.spliterator(this, Spliterator.DISTINCT | Spliterator.ORDERED);
}

public static <T> LinkedHashSet<T> newLinkedHashSet(int numElements) {
if (numElements < 0) {
throw new IllegalArgumentException("Negative number of elements: " + numElements);
}
return new LinkedHashSet<>(HashMap.calculateHashMapCapacity(numElements));
}

LinkedHashMap<E, Object> map() {
return (LinkedHashMap<E, Object>) map;
}

public void addFirst(E e) {
map().putFirst(e, PRESENT);
}

public void addLast(E e) {
map().putLast(e, PRESENT);
}

public E getFirst() {
return map().sequencedKeySet().getFirst();
}

public E getLast() {
return map().sequencedKeySet().getLast();
}

public E removeFirst() {
return map().sequencedKeySet().removeFirst();
}

public E removeLast() {
return map().sequencedKeySet().removeLast();
}
}
 
← Previous topic
class HashSet<E> in Java
 
Next topic →
Collections.singletonList("Hello") returns an immutable list containing exactly one specified element | Java collection
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

 
Download and install
• Installation JDK (download and install the Java library for Windows)
• Download and install IntelliJ IDEA to explore Java, Spring, Jakarta EE | Functionality and differences: Community | Ultimate
• Error "JDK is missing" | Java
Java (class, interface, collections, etc.)
• Create a simple console app in IntelliJ IDEA to learn Java
• Access modifiers (private, protected, public) for a class and for methods and fields. | Java
• Collections in Java
• interface List<E> in Java
• class ArrayList<E> in Java
• class Vector<E> in Java
• class Stack<E> in Java
• class CopyOnWriteArrayList<E> in Java
• interface Queue<E> in Java
• interface Deque<E> in Java
• class LinkedList<E> in Java
• interface Set in Java
• class HashSet<E> in Java
• class LinkedHashSet<E> in Java
• Collections.singletonList("Hello") returns an immutable list containing exactly one specified element | Java collection
• class Collections (all functions, algorithms) | Java
• stream.filter(...) select elements according to your criterion | Java collection | Example: allCars.stream().filter(item -> item.Price < 30000).toList()
• What is interface in Java ?
• Directly in the code (at runtime) we make an implementation for the interface | Java
• Threads in Java
• lambda is an unnamed method (no name and no class owned) | Java
• Exceptions and handling in Java. Example: try {...} catch (Exception e) {...}
• Why use throws Exception in the function name in Java? Answer: The method throws an exception and there is no handling of try catch. Example: float CalculateDensity(float mass, float volume) throws Exception { ... throw new Exception("Error! Volume is zero."); ... }
Java web application (servlet, jsp page, basic functionality and no frameworks)
• Creating a new simple web application (jsp web page) | Java, Maven
• Create a web servlet (the servlet is located on the web server, returns the result on request) | Java
• Installation Tomcat web server (download and install for Windows)
• Check, test Tomcat web server (create a new my.hml file)
• What is Maven
• What is a dynamic web page jsp (Java Server Page) ?
• How to install Smart Tomcat plugin in Intellij Idea
• Error "The SDK is not specified for module ... | Project SDK is not defined ..." in IntelliJ IDEA | Java
Spring in Java (Spring Framework, Spring Data, Spring Boot, ...)
• What is Spring? Why use Spring in Java?
• What is Spring Bean? What is Spring Container? IoC (inversion of control) | Creating a simple Spring project and configuration Spring Container in an XML file | Editor IntelliJ Idea and language Java
• What is Dependency Injection in Spring? Creating a simple Spring project with Dependency Injection in the constructor with the attributes @Component, @Autowired | Editor IntelliJ Idea and language Java
• Create a new Spring Web application (the request goes to the Controller → Controller creates a response as text or json) | Java, Spring Web, Spring Boot, Maven
• Creating a new Spring WebSocket application (Java WebSocket sends a message to the JavaScript WebSocket) | Java, Spring WebSocket, Spring Boot, Maven
• Create Azure Web App with the type Java (free). That is, create an empty web server
• Put the Java Spring web application in Azure
• Connect to the application Azure Web App (Java) via FTP | use File Explorer
• Error "The SDK is not specified for module ... | Project SDK is not defined ..." in IntelliJ IDEA | Java, Spring Boot
• How to install Spring WebSocket plugin in Intellij Idea
Jakarta EE / Java EE in Java (Web Applications, Web Services)
• What is Jakarta EE (Java EE)? | In which editor (program) is it convenient to write Jakarta EE (Java EE) code?
• Creating a simple web project in IntelliJ Idea Ultimate | Jakarta EE (Java EE)
Struts in Java (extends API Java Servlet using MVC)
• What is Struts | Java
Courses, training
• Course "Java developer" from Yandex
Interview
• Interviews on Java

  Ваши вопросы присылайте по почте: info@dir.by