55561

List implementation that is both a Set a List (sequence)?

Question:

I'm in the position of extending LinkedList and implement Set, so that I have a list with no duplicates. I'm wondering if such an implementation doesn't exist already?

All I'm planning to do is to override the add(e) method to first look-up the element, and if present don't add it. Something like:

add(E){ if(get(E) == null) super.add(E); }

Answer1:

No Java implementation exists in the <em>standard</em> collections.

However, you can take a look at <a href="http://collections15.sourceforge.net/apidocs/net/sf/collections15/list/SetUniqueList.html" rel="nofollow">SetUniqueList</a> from the <a href="http://commons.apache.org/collections/" rel="nofollow">Common Collections</a> which may be along the lines of what you are looking for.

Answer2:

Maybe <a href="http://download.oracle.com/javase/1.4.2/docs/api/java/util/LinkedHashSet.html" rel="nofollow">LinkedHashSet</a> does what you want. It keeps elements in (by default) insertion order.

It is not possible to implement both interfaces at the same time (at least if you want to follow the specifications for List and Set), because the hashCode definitions conflict.

<blockquote>

Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:

</blockquote> hashCode = 1; Iterator i = list.iterator(); while (i.hasNext()) { Object obj = i.next(); hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode()); }

versus

<blockquote>

Returns the hash code value for this set. The hash code of a set is defined to be the sum of the hash codes of the elements in the set, where the hashcode of a null element is defined to be zero. This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two sets s1 and s2, as required by the general contract of the Object.hashCode method.

</blockquote>

Recommend

  • Inserting Rows Without Selecting Anything?
  • implementing euclidean distance based formula using numpy
  • What's wrong with my PNG IDAT chunk?
  • Angular Library Modules export components, services and others from module
  • Failure to Read Updated AnyLogic DB Values
  • AngularJS Dynamic Directives inside ng-repeat
  • Installing Kohana on OpenShift?
  • MySQL - Filter records which date is biggest
  • Specify the _id field using Bulk.IndexMany in ElasticSearch
  • Why processBuilder in java hangs after 5 mins?
  • List using with references, changes behavior when used as a member
  • Spring Cloud Config - Multiple Composite Repositories?
  • Identifying dates in strings using NLTK
  • Arc gradients in Flutter?
  • Send HTML Mail with Unicode
  • Azure NodeJS Error: ENOENT, open 'D:\\home\\site\\wwwroot\\bin\\views\\'
  • Align microsoft access queries
  • read.table returning character matrix, would like numeric
  • Convert current tmux session to configuration file
  • SpringBoot don't replacen System variable {user.home} in Spring Tool Suite Version: 3.8.4.RELEA
  • How to move to lines with the same indentation in Visual Studio Code
  • Php artisan optimize is failing on production server
  • How can I filter an array of dictionaries in 'updateSearchResultsForSearchController' to s
  • Unsupported ciphersuite TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  • Modifying native query cannot have named parameter bindings?
  • Adding native code to an existing Worklight hybrid app
  • PHP: Need to close STDIN in order to read STDOUT?
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • Swift manually rotate view controller
  • What does “T extends Junk” mean in a generic class in Java?
  • Swift: UIView.animate works unexpectedly
  • java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/SpringDispatcher-ser
  • Dynamic XML Schema Validates Subsection of Document
  • PHPMailer return to AJAX
  • Why is ordered choice in pyparsing failing for my use case?
  • Neo4j…how to get a visual representation of my data?
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Background transfer download task failed when app was closed
  • Call Microservice from another Microservice within Docker
  • Write to .csv file with PHP (Commas in Data Error)