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
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
  • 992
  • 3
  • 13
  • 29
5
votes
2 answers

equivalent of Java LinkedHashSet in Swift

In java we have: private Set hashList = new LinkedHashSet<>(); and: public class AutoComplete { private String name; private String id; //...geters and setters @Override public boolean equals(Object o) { …
Hoven
  • 563
  • 1
  • 5
  • 24
5
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
  • 1,289
  • 7
  • 15
  • 39
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
  • 21,946
  • 60
  • 170
  • 271
4
votes
4 answers

How to obtain first 5 values from a LinkedHashSet?

I have a LinkedHashSet which contains multiple number of values. LinkedHashSet lhs = new LinkedHashSet(); I want to iterate through the set of values and display the first five values from the number of items stored in the set. I…
coder
  • 203
  • 5
  • 15
4
votes
2 answers

LinkedHashMap LRU Cache - Determine what values are removed?

Background Information You can make a LRU cache with a LinkedHashMap as shown at this link. Basically, you just: Extend linked hash map. Provide a capacity parameter. Initialize the super class (LinkedHashMap) with parameters to tell it its…
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
4
votes
2 answers

Copying LinkedHashset content to new ArrayList?

I've a listView that has some content initially. If the same content it gets, i removed the duplication through linkedhashset. Now, i want copy the linkedhashset contents i.e without duplication contents to new ArrayList. I tried to copy through…
user3289108
  • 770
  • 5
  • 10
  • 29
4
votes
3 answers

LinkedHashMap vs HashMap, LinkedHashSet vs HashSet

I know the difference between all of them and I understand that LinkedHashMap and LinkedHashSet provide an insertion-ordering. I understand that LinkedHashMap extends HashMap and LinkedHashSet extends HashSet. Why don't we always use LinkedHashMap…
O Connor
  • 4,236
  • 15
  • 50
  • 91
4
votes
2 answers

LinkedHashSet Equals Method

I have a two sets of LinkedHashSet objects, within this objects I have other objects that have more LinkedHashSet. My question is: Does the equals method (default) checks if all the inside HashSets are the same? Or do I have to overwrite it?
Ivan Santos
  • 624
  • 1
  • 8
  • 21
3
votes
4 answers

Converting LinkedHashSet to ArrayList or just using ArrayList

Consider the following code: final Set allPaths = new HashSet(); for (final String path: paths) { allPaths.add(path); } final MyData d = new MyData(new ArrayList(allPaths)); MyData is some class I should not touch.…
TTaJTa4
  • 810
  • 1
  • 8
  • 22
3
votes
2 answers

Two unsorted lists Intersection returned as a list

My question is if its possible to get 2 unsorted lists, and get the intersection of both lists according to the order it was in the first list "List1". public static List intersection(List A, List B) { List outcome = null; try { …
Masteprh33r
  • 105
  • 9
3
votes
3 answers

What algorithm is used for converting an ArrayList to a LinkedHashSet in JRE

I wanted to get a list of unique elements from a list with duplicate elements and the order of elements occurring in the list should be maintained. To achieve this, I could have written an algorithm like: private ArrayList getUnique(ArrayList
Amit Upadhyay
  • 7,179
  • 4
  • 43
  • 57
3
votes
2 answers

Why doesn't LinkedHashSet implement List?

To my understanding, a List is an ordered collection of items. And a Set is a collection of unique items. Now my question is, why does LinkedHashSet, which describes an ordered collection of unique items, implement the Set interface (=> unique), but…
Felk
  • 7,720
  • 2
  • 35
  • 65
3
votes
4 answers

Why doesn't LinkedHashMap keyset() return LinkedHashSet vs Set?

I've read in java documentation and also in this post that LinkedHashMaps' keyset() maintains order. Is the order guaranteed for the return of keys and values from a LinkedHashMap object? My question is, if it guarantees order then why doesn't the…
Brandon Ling
  • 3,841
  • 6
  • 34
  • 49
3
votes
3 answers

Is it worth it to check LinkedHashSet.contains(...) before adding?

I'm using a LinkedHashSet to implement a set ordered by entry. If a duplicate element is added, it should go at the end and the entry already in the set should be removed. I'm debating whether it's worth it to check set.contains(entry) before I call…
Fred
  • 165
  • 2
  • 7
1
2
3
10 11