Questions tagged [linkedhashset]

LinkedHashSet is a variant of HashSet

Fundamental:

LinkedHashSet is a variant of HashSet. Its entries are kept in a doubly-linked list. The iteration order is the order in which entries were inserted.

Null elements are allowed, and all the optional Set operations are supported.

Like HashSet, LinkedHashSet is not thread safe, so access by multiple threads must be synchronized by an external mechanism such as synchronizedSet(Set).

146 questions
161
votes
10 answers

HashSet vs LinkedHashSet

What is the difference between them? I know that A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. Use this class instead of HashSet when you care about the iteration order. When you…
Shikarn-O
  • 3,107
  • 7
  • 24
  • 26
34
votes
4 answers

LinkedHashSet - insertion order and duplicates - keep newest "on top"

I need a collection that keeps insertion order and has unique values. LinkedHashSet looks like the way to go, but there's one problem - when two items are equal, it removes the newest one (which makes sense), here's an…
rafakob
  • 2,759
  • 2
  • 17
  • 29
32
votes
6 answers

Creating subset of a Set in Java

I have a LinkedHashSet, i.e an ordered set. I'm trying to find a function to just return a subset of the set, i.e the first 20 elements of the set. I know I can do it by creating a new set and then populating using an iteration of the first set but…
Paul Taylor
  • 12,050
  • 34
  • 149
  • 295
24
votes
5 answers

Java LinkedHashSet backwards iteration

How can I iterate through items of a LinkedHashSet from the last item to the first one?
David Weng
  • 3,985
  • 12
  • 38
  • 49
17
votes
3 answers

Does hibernate preserve the order of a LinkedHashSet and if so, how?

Does hibernate preserve the order of a LinkedHashSet and if so, how? In case this depends on the type of database, I'd like to know this for PostgreSQL. Background: I know what a LinkedHashSet is for, and the reason I'm asking this is because I'm…
Erik Stens
  • 1,677
  • 6
  • 23
  • 38
9
votes
2 answers

Ordering of elements in Java HashSet

Why do the second and third sets preserve order: Integer[] j = new Integer[]{3,4,5,6,7,8,9}; LinkedHashSet i = new LinkedHashSet(); Collections.addAll(i,j); System.out.println(i); HashSet hi = new…
Sam Adamsh
  • 3,163
  • 6
  • 28
  • 50
9
votes
2 answers

LinkedHashSet .equals() vs LinkedList .equals() with same elements but different order

Consider the following SSCCE: public static void main(String[] args) { LinkedHashSet set1 = new LinkedHashSet<>(); set1.add("Bob"); set1.add("Tom"); set1.add("Sam"); LinkedHashSet set2 = new LinkedHashSet<>(); …
ryvantage
  • 11,982
  • 13
  • 54
  • 98
8
votes
2 answers

How to get one element from LinkedHashSet in Java?

I'm looking to write code that partitions a given set into disjoint subsets. For example, a set of football players and we partition them depending on the team they belong to. I want in the end a list of representatives, i.e. one player from each…
user1111929
  • 5,742
  • 9
  • 34
  • 68
7
votes
5 answers

What are the pros and cons of LinkedHashMaps vs. LinkedHashSets?

Could someone explain the main benefits for choosing one over the other and the detriments that come with that choice?
soldier.moth
  • 17,709
  • 14
  • 71
  • 88
6
votes
2 answers

Why LinkedHashSet has boolean accessOrder set to false

In Java LinkedHashSet is created with backing HashSet creating LinkedHashMap with following LinkedHashMap constructor map = new LinkedHashMap<>(initialCapacity, loadFactor); Now in LinkedHashMap, the above constructor in turn calls public…
Jyotirup
  • 2,652
  • 9
  • 28
  • 36
6
votes
2 answers

java LinkedHashSet

I've been studying for OCJP (former SCJP) and I came across the following example which uses LinkedHashSet: public class Test{ int size; public Test(int s){ this.size = s; } @Override public boolean equals(Object obj)…
Maggie
  • 7,253
  • 6
  • 39
  • 65
5
votes
1 answer

Junit test for order in LinkedHashSet

I'm attempting to write a Junit test which should test if the order of elements in two LinkedHashSets are the same. The follwoing is my existing code: Assert.assertEquals( Sets.newLinkedHashSet(Arrays.asList("a","d","c","b")), …
seriousgeek
  • 819
  • 2
  • 12
  • 24
5
votes
9 answers

Using the keySet() method in HashMap

I have a method that goes through the possible states in a board and stores them in a HashMap void up(String str){ int a = str.indexOf("0"); if(a>2){ String s =…
andandandand
  • 20,448
  • 55
  • 163
  • 248
4
votes
2 answers

Does LinkedHashSet constructor preserve order

Does the constructor LinkedHashSet(Collection c) guarantee preserved order of its argument, assuming the argument is an ordered collection? How can we be sure of that? The Javadoc documentation says nothing about order: Constructs a…
AnnTea
  • 502
  • 7
  • 15
4
votes
1 answer

Retrieve LinkedHashSet elements in the insertion order and save them to local variables

I have a linkedhashset from which I need to get elements in the same order they are inserted. To retrieve, am stuck getting the elements in exact insertion order. Converting the linkedHashset to List to get by index, but am afraid converting to list…
Rk R Bairi
  • 999
  • 6
  • 13
  • 33
1
2 3
9 10