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
2
votes
2 answers

How to remove element From Linked Hash Set in java?

I want to know Different ways to remove element from Linked Hash Set. I tried following code LinkedHashSet lhs = new LinkedHashSet(); for(int i=0;i<10;i++) lhs.add(String.valueOf(i)); Iterator
sar
  • 1,277
  • 3
  • 21
  • 48
2
votes
2 answers

Android HashSet cannot be cast to LinkedHashSet

I've the following sample code. The application is installed successfully at first time. However, it throws an error on reinstalling. public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState)…
Ibungo
  • 1,137
  • 12
  • 23
2
votes
1 answer

Fetch the most recent inserted element in LinkedHashSet efficiently

According to LinkedHashSet's Javadoc, it will keep the insertion order of inserted elements by using a doubly linked list internally. it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration…
nybon
  • 8,894
  • 9
  • 59
  • 67
2
votes
5 answers

Java LinkedHashSet remove some elements from the end

I am working on a problem where i'm required to store elements with requirements of No Duplication and Maintaining order. I chose to go with LinkedHashSet Since it fulfilled both my requirements. Let's say I have this code: LinkedHashSet hs = new…
Jazib
  • 1,200
  • 1
  • 16
  • 39
2
votes
2 answers

Java LinkedHashSet index validity

I've been porting a big chunk of Java code to C++ and have had to implement things like LinkedHashSet as I've gone. I've made a reasonable facsimile of LinkedHashSet/Map by using Boost's Multi-Index Containers. As I'm porting the code I'm running…
xwhatsit
  • 153
  • 1
  • 7
1
vote
1 answer

when I use @IdClass LinkedHashSet include duplicate items

I have tasked to prepare a quote search screen. I have referred to an Oracle view to create quote model. As there is no id column in the view table, I prefer to use composite id using @IdClass annotation via quoteId.class. I override hashCode and…
HRgiger
  • 2,750
  • 26
  • 37
1
vote
2 answers

Unique Java Objects in a LinkedHashSet

I want to create a top 5 list of unique key value pairs sorted according to a value. I have tried creating a Hashmap but since the original list that i read from JSON is sorted Hashmap overwrites the last value so they key will have the smallest…
AhmadAssaf
  • 3,556
  • 5
  • 31
  • 42
1
vote
3 answers

LinkedHashSet: hashCode() and equals() match, but contains() doesn't

How is the following possible: void contains(LinkedHashSet data, Object arg) { System.out.println(data.getClass()); // java.util.LinkedHashSet System.out.println(arg.hashCode() == data.iterator().next().hashCode()); // true …
Konrad Garus
  • 53,145
  • 43
  • 157
  • 230
1
vote
3 answers

hibernate: how to map a Set association so that HQL order by is preserved

OK, this looks mostly like (another) bug/unimplemented functionality in Hibernate. Is it possible to map a Set association in such a way that the "order by" clause in HQL is respected? For example, say you have 2 entities: PageEntity and…
1
vote
1 answer

Ambiguity in a CodeForces Problem - usage of HashSet Vs LinkedHashSet

I was solving a Codeforces problem yesterday. The problem's URL is this I will just explain the question in short below. Given a binary string, divide it into a minimum number of subsequences in such a way that each character of the string belongs…
Phenomenal One
  • 2,501
  • 4
  • 19
  • 29
1
vote
1 answer

What is _CompactLinkedHashSet in flutter?

I am trying to run my first flutter app and I am getting a redscreen error when I run it on my iPhone but it runs without a problem on the simulator: type '_CompactLinkedHashSet' is not a subtype of type 'Widget'. Does anyone know what this error…
Josh
  • 11
  • 3
1
vote
1 answer

java.util.LinkedHashMap$LinkedKeyIterator infinite loop in LinkedHashSet

System.out.print("Enter the size of linked hash set: "); Scanner s = new Scanner(System.in); // Create a Scanner object int size = s.nextInt(); HashSet lhashset = new HashSet<>(size); for(int i=0; i
1
vote
1 answer

Save order of set in hibernate

I have two collections, which by default are set to PersistentSet by hibernate. The issue is that under the hood hibernate casts them to HashSet, but I want them to be set to LinkedHashSet, as I want to preserve the order in which the elements are…
1
vote
1 answer

How to implement stack which stores only unique values in Java

I'm trying to implement the unique stack. If I try to extend the stack class and use the contains it will look the entire stack and the time complexity will become O(n). So I tried to extend the LinkedHashSet which will remove duplicates by itself…
Siva
  • 1,078
  • 4
  • 18
  • 36
1
vote
1 answer

LinkedHashSet does not main insertion order?

I am currently building an Android app where I display quotes from famous people. I have a home screen and 2 other screens, where I am displaying all quotes and the other where I display favourite quotes. So, when I hit the like button in the…
user12465043