linkedhashset

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

﹥>﹥吖頭↗ 提交于 2019-12-01 04:19:25
Consider the following SSCCE: public static void main(String[] args) { LinkedHashSet<String> set1 = new LinkedHashSet<>(); set1.add("Bob"); set1.add("Tom"); set1.add("Sam"); LinkedHashSet<String> set2 = new LinkedHashSet<>(); set2.add("Sam"); set2.add("Bob"); set2.add("Tom"); System.out.println(set1); System.out.println(set2); System.out.println(set1.equals(set2)); } This prints: [Bob, Tom, Sam] [Sam, Bob, Tom] true Yet if you change LinkedHashSet to LinkedList : public static void main(String[] args) { LinkedList<String> set1 = new LinkedList<>(); set1.add("Bob"); set1.add("Tom"); set1.add(

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

拟墨画扇 提交于 2019-12-01 00:34:39
问题 Consider the following SSCCE: public static void main(String[] args) { LinkedHashSet<String> set1 = new LinkedHashSet<>(); set1.add("Bob"); set1.add("Tom"); set1.add("Sam"); LinkedHashSet<String> set2 = new LinkedHashSet<>(); set2.add("Sam"); set2.add("Bob"); set2.add("Tom"); System.out.println(set1); System.out.println(set2); System.out.println(set1.equals(set2)); } This prints: [Bob, Tom, Sam] [Sam, Bob, Tom] true Yet if you change LinkedHashSet to LinkedList : public static void main

LinkedHashSet - insertion order and duplicates - keep newest “on top”

ε祈祈猫儿з 提交于 2019-11-30 16:49:08
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 example: set.add("one"); set.add("two"); set.add("three"); set.add("two"); The LinkedHashSet will print: one , two , three But what I need is: one , three , two What would be the best solution here? Is there any collection/collections method that can do this or should I implement it manually? Most of the Java Collections can be extended for tweaking. Subclass LinkedHashSet ,

Java LinkedHashSet backwards iteration

拈花ヽ惹草 提交于 2019-11-30 07:59:19
问题 How can I iterate through items of a LinkedHashSet from the last item to the first one? 回答1: If you want to continue to use collections, you could use the following: LinkedHashSet<T> set = ... LinkedList<T> list = new LinkedList<>(set); Iterator<T> itr = list.descendingIterator(); while(itr.hasNext()) { T item = itr.next(); // do something } If you're fine with using an array instead, you could take a look at hvgotcodes' answer. 回答2: er, assuming you mean LinkedHashSet... I would use toArray

What is the main difference between Hashset, Treeset and LinkedHashset, Hashmap and how does it work in Java?

拥有回忆 提交于 2019-11-29 12:24:25
I just understand that LinkedHashSet does not allows duplicate elements when it is inserting. But, I dont understand how does Hashset works in Hava? I know a bit that Hashtable is used in Hashset so the hashtable used to store the elements and here also does not allow the duplicate elements. Then, Treeset is also similar to Hashset it also does not allows duplicate entries so unique elements will be seen and it follows ascending order. I have one more doubt regarding HashMap - Hashmap does not maintains order. It may have one null key and multiple null values. I just dont understand this and

Java LinkedHashSet backwards iteration

和自甴很熟 提交于 2019-11-29 09:10:16
How can I iterate through items of a LinkedHashSet from the last item to the first one? Jeffrey If you want to continue to use collections, you could use the following: LinkedHashSet<T> set = ... LinkedList<T> list = new LinkedList<>(set); Iterator<T> itr = list.descendingIterator(); while(itr.hasNext()) { T item = itr.next(); // do something } If you're fine with using an array instead, you could take a look at hvgotcodes' answer . hvgotcodes er, assuming you mean LinkedHashSet... I would use toArray and just use a reverse for loop. There might be a better way to do it, but that should work.

Ordering of elements in Java HashSet

北战南征 提交于 2019-11-27 02:22:14
Why do the second and third sets preserve order: Integer[] j = new Integer[]{3,4,5,6,7,8,9}; LinkedHashSet<Integer> i = new LinkedHashSet<Integer>(); Collections.addAll(i,j); System.out.println(i); HashSet<Integer> hi = new HashSet<Integer>(i); System.out.println(hi); LinkedHashSet<Integer> o = new LinkedHashSet<Integer>(hi); System.out.println(o); Here's the output I get: 3,4,5,6,7,8,9 3,4,5,6,7,8,9 3,4,5,6,7,8,9 Behrang The second one (just using HashSet ) is only a coincidence. From the JavaDocs : This class implements the Set interface, backed by a hash table (actually a HashMap instance).

HashSet vs LinkedHashSet

回眸只為那壹抹淺笑 提交于 2019-11-26 23:29:30
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 iterate through a HashSet the order is unpredictable, while a LinkedHashSet lets you iterate through the elements in the order in which they were inserted. But in sourcecode of LinkedHashSet there are only calling constructors of HashSet. So where is double-linked List and insertion order? The answer lies in which constructors the LinkedHashSet uses to

Ordering of elements in Java HashSet

好久不见. 提交于 2019-11-26 10:02:06
问题 Why do the second and third sets preserve order: Integer[] j = new Integer[]{3,4,5,6,7,8,9}; LinkedHashSet<Integer> i = new LinkedHashSet<Integer>(); Collections.addAll(i,j); System.out.println(i); HashSet<Integer> hi = new HashSet<Integer>(i); System.out.println(hi); LinkedHashSet<Integer> o = new LinkedHashSet<Integer>(hi); System.out.println(o); Here\'s the output I get: 3,4,5,6,7,8,9 3,4,5,6,7,8,9 3,4,5,6,7,8,9 回答1: The second one (just using HashSet ) is only a coincidence. From the

HashSet vs LinkedHashSet

馋奶兔 提交于 2019-11-26 08:44:54
问题 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 iterate through a HashSet the order is unpredictable, while a LinkedHashSet lets you iterate through the elements in the order in which they were inserted. But in sourcecode of LinkedHashSet there are only calling constructors of HashSet. So where is double