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).

153 questions
1
vote
0 answers

Convert a HashMap> to HashMap>

I would like to convert a HashMap> to HashMap> so that I can print all their indexes. I would like to store only the distinct values from a list of 'String's that start with a letter. e.g. I have…
1
vote
0 answers

Groovy Jenkins Convert LinkedHashSet to String

Ultimate goal: a. Convert ArrayList to LinkedHashSet for getting unique results and then b. convert the LinkedHashSet to String with "\n" as delimeter ie. every record on a new line. I will be applying the conversion logic in a Jenkins pipeline as…
1
vote
0 answers

Is getting any element with iterator().next() from a HashSet slower than getting them from a LinkedHashSet?

I read this sentence on a Leetcode solution : I modified the code by replacing HashSet with LinkedHashSet because the set.iterator() might be costly when a number has too many duplicates. Using LinkedHashSet can be considered as O(1) if we…
Ricola
  • 2,621
  • 12
  • 22
1
vote
2 answers

Why doesn't LinkedHashSet have addFirst method?

As the documentation of LinkedHashSet states, it is Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running…
John McClane
  • 3,498
  • 3
  • 12
  • 33
1
vote
1 answer

How to avoid duplicate elements in Android ArrayObjectAdapter

I'm adding elements to an Android (Kotlin) adapter extending ArrayObjectAdapter. I use addAll to add items. From ArrayObjectAdapter: public void addAll(int index, Collection items) { int itemsCount = items.size(); if (itemsCount == 0) { …
alexbtr
  • 3,292
  • 2
  • 13
  • 25
1
vote
3 answers

Using a LinkedHashSet to store random numbers but has duplicates

I have an app that generates a random arithmetic expression. It then generates a correct answer in a random position (tag) and three subsequent incorrect answers allowing the user to choose. I originally went with an ArrayList but it provides…
Milie
  • 374
  • 1
  • 6
  • 22
1
vote
2 answers

Iterate through LinkedHashSet with custom Object and remove items

I have a custom Parcelable Object (ImageList) which is being filled with string names (test.jpeg) of picture files. These objects are added to a LinkedHashSet. The pictures are also saved in the internal storage of the device. If the Application is…
Simon
  • 1,691
  • 2
  • 13
  • 28
1
vote
2 answers

what is the cost of Collection.retainAll(Collection)

I wanted to find the common elements between two LinkedHashSet, Primarily I wrote my own function but the cost was o(n^2). Then I found a better solution of retainAll() java built in function. I was wondering what is the cost of this…
user1836957
  • 427
  • 2
  • 9
  • 24
1
vote
1 answer

Behaviour difference between HashSet and LinkedHashSet for Knights Path solution

I'm seeing a strange behaviour in a coding puzzle I was working on 'Knights Path'. I generate the set of possible moves and store these in a HashSet (the Move class simply has an x,y co-ordinates and a standard hashcode and equals). When I use a…
MDooley
  • 11
  • 1
1
vote
1 answer

How to add Strings to a LinkedHashSet?

LinkedHashSet lHs = new LinkedHashSet(); lHs.add("Beta"); When compiling the above (tutorialspoint uses a similar approach), I get the error: The method add(String) is undefined for the type LinkedHashSet And if it's generic (which I think it is…
knjk04
  • 115
  • 2
  • 11
1
vote
1 answer

How can I delete duplicate items in ArrayList with LinkedHashSet?
I have been trying many things to solve this problem and searching for the solution for hours, I need some help with this. I have a listview, importing data from MySQL and fetching data in Listview. My problem: the listview duplicate items (Only the…
SanHappy
  • 25
  • 7
1
vote
1 answer

Is there a way to opt for "unspecified behavior" rather than ConcurrentModificationException?

I know that code like for ( Object o: collection){ if (condition(i)){ collection.remove(i); } } will throw a ConcurrentModificationException, and I understand why: modifying the collection directly could interfere with the…
Gabriel Burns
  • 345
  • 1
  • 2
  • 9
1
vote
5 answers

Get a sublist last 5 elements of LinkedHashSet?

Is there a one liner to get the last 5 elements of a LinkedHashSet in a new LinkedHashSet? This is what I currently have, but it's not very efficient: new LinkedHashSet(new LinkedList(set) .subList(Math.max(0, set.size() - 5),…
Bob
  • 1,201
  • 1
  • 9
  • 22
1
vote
1 answer

sorting a LinkedHashset by date time

I have the following LinkedHashset It is as LinkedHashset because i have many duplicate entries upon addition and it prevents this. How can i then sort my LinkedHashset LinkedHashSet uniqueStrings = new…
Ingram
  • 654
  • 2
  • 7
  • 29
1
vote
3 answers

Why doesn't LinkedHashSet always retain order?

Set stringSet = new HashSet(); will not retain order obviously Set linkedHastSet = new LinkedHashSet(); while reading elements from the above set it should retain order but in some times it does not ? am I wrong in…
forum.test17
  • 2,119
  • 6
  • 30
  • 62