Hash-based collections vs. mutable hashcodes

I've been getting ConcurrentModificationExceptions on HashTables (derivatives) in a single-threaded app that does no overt mods to the collection being iterated. The algorithm is a classic Observer, iterating all listeners on an event. These listeners are rather heavyweight classes with hashcode() (along with equals()) essentially calculating the current object internal state, which is mutable.
What I think is going on is that during an event (iteration over the set) a listener state may change, resulting in a changed hashcode(). While this doesn't overtly modify the collection, it may modify something like the internal ordering. i.e. the "old" object would appear to have disappeared since it can no longer be resolved with its current hashcode. Can anyone confirm that this is indeed a possible phenomenon? If so, it raises an interesting general issue.
Should hashcode() and equals() be built on an immutable subset of state? Assuming logical identity is desirable (i.e. state matters to equality), and hashcode should reflect the result of equals (re: Josh Bloch in EJ (2nd) Item# 8), this seems to dictate that such objects are not viable elements of a hash-based collection. Discussion?

jverd wrote:
796912 wrote:
Should hashcode() and equals() be built on an immutable subset of state? hashCode() should, yes. Or, if it's mutable, then when using a hash-based collection you have to remove and re-add after a change, or possibly remove, change, re-add. Additionally, there's the iteration gotcha that you found that prompted this thread.you definitely have to remove before you change, otherwise remove won't work.
equals() shouldn't matter. Changing an object's hash can change which bucket it's in, and that's why things get screwed up. The equals() comparison comes into play when doing a linear search of that bucket, so changing state that contributes to equals won't hurt that. not entirely true. changing equals could change the validity of the uniqueness of the set. if you change the equals criteria of an object already in the set so that it is now equal to another object in the set, the set is no longer valid. while this may not break quite as spectacularly as changing the hashcode, it can still do interesting things. for instance, if you add another element such that the hashmap decides to re-hash, one of your now duplicate elements will be silently dropped on the floor. long answer short, don't change hashcode or equals.
However, in a SortedSet or SortedMap, changing state that contributes to equals() could lead to problems, so the same remove/re-add rules would apply.nope, sorted collections don't use hashcode or equals, they use compareTo/compare. but, the same rules certainly apply to the comparison criteria for the given sorted collection.

Similar Messages

  • Query based collections SCCM 2012 R2

    Hi to everyone. Is there any book or other material needed to master process of creating query-based collections? It is done using WQL not T-SQL so it is a little bit tricky, not only syntax itself but also classes where particular information useful to
    create custom query-based collection can be found. I used WQL before only to create WMI filters for some GPOs in order to narrow their scope e.g. only to Windows 7 workstations in domain. These WMI filters were usually created
    with one short WQL line, it was very simple WQL query. Bare in mind all classes that exist in SCCM database googling can be useful to solve some problems but it would be better to have some book and to master creation of query-based
    collections.  

    Hi,
    You could review the file smsprov.log.
    We
    are trying to better understand customer views on social support experience, so your participation in this
    interview project would be greatly appreciated if you have time.
    Thanks for helping make community forums a great place.

  • SCCM 2012 R2 - query based Collection based on missing update

    Hi,
    I was wondering if anyone knew what attribute to use in console to create query based collection for missing patches? I viewed the entire list of attributes available in console but I cannot seem to find anything that says "Windows Updates" or
    something similar.
    thanks

    You might also consider creating a saved search with all updates that are deployed but still required.
    Kent Agerlund | My blogs: blog.coretech.dk and
    SCUG.DK | Twitter: @Agerlund | Linkedin:
    Kent Agerlund | Author:
    Mastering System Center 2012 R2 Configuration Manager

  • How to deploy configuration manager client package to a query based collection

    Hi
    I have created one OU based collection.Now i wants to deploy client package to this collection so that the client automatically get installed,whenever a new machine added to the OU and so on to the collection.
    But the issue is with configuration manager client package deployment,as we can't deploy the default configuration package to the collection(Deploy option is grayed out). For this i created a new configuration manager client package and deployed this package
    to the query based collection. 
    Now when a new machine added to this collection,the client did not appear to deploy on this machine. Please help

    If you want all the devices in the OU to get the client, you can use a Computer Startup Script to deploy it.  Everybody just uses Jason's since it does everything and is well documented.
    http://blog.configmgrftw.com/configmgr-client-startup-script/
    I hope that helps,
    Nash
    Nash Pherson, Senior Systems Consultant
    Now Micro -
    My Blog Posts
    If you've found a bug or want the product worked differently,
    share your feedback.
    <-- If this post was helpful, please click "Vote as Helpful".

  • Machines not showing up in AD group based collections in CM 2012

    Hey Folks
    I am facing a wired issue, with collection based on AD group. We have added multiple machines to a AD security group. All these machines are already discovered in CM12. AD group has been added in AD group discovery & we a collection is created based
    on this particular AD group. The issue is that there are few machines which are not showing up in AD group collection.I have tried to manually update collection membership, run full discovery : AD system discovery & AD group discovery. When i check that
    machines properties in SCCM console the specified AD Group is not showing there. Not sure how do i proceed further.
    Thanks in anticipation.
    Sumit

    We need the your collection query and colleval.log to check the issue. Can you please provide the info here?
    Juke Chou
    TechNet Community Support

  • Issue with AD group based collection

    Hi,
    We are using AD security group based membership. Also, we are uing the same SCCM infra to cover other domains as well.
    From Activie Direcoty, it could able to discover the machines (computer objects) belongs to other domains but those computer objects not reflecting onto the collections.
    Any suggestions woud help us!
    Many Thanks.

    Check your collection refresh interval also is any machines added to the AD group getting populated ie. from the same domain xxx ? if so, i think you need to add the IP sub-net details on the boundary and
    also  discover the another domain machines using LDAP query for it(ex. sccm discovers and manages xxx domain where as its not discovering yyy domain machines becoz its not discovered ,you can also do it using the IP subnet details ie n/w discovery) the
    machines to the ALL systems then it may get added to the collection.
    Kamala kannan.c| Please remember to click “Mark as Answer” or Vote as Helpful if its helpful for you. |Disclaimer: This posting is provided with no warranties and confers no rights

  • Query based collection

    Hi everyone, i  am using sccm 2012 R2 
    I want to create a device collection  based on operating system versions like winxp x86, winxp x64, win7x86, win7x64, win8 x86, win8 x64, win8.1 x64 win8.1 x86.
    please do guide me how to create query based collection

    Operating system
    Version number
    Windows 8.1
    6.3*
    Windows Server 2012 R2
    6.3*
    Windows 8
    6.2
    Windows Server 2012
    6.2
    Windows 7
    6.1
    Windows Server 2008 R2
    6.1
    Windows Server 2008
    6.0
    Windows Vista
    6.0
    Windows Server 2003 R2
    5.2
    Windows Server 2003
    5.2
    Windows XP 64-Bit Edition
    5.2
    Windows XP
    5.1
    Windows 2000
    5.0
    For more infomation, pleaes review the link below:
    List of Microsoft Windows versions
    http://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
    Use the following query to build collection.
    select
    SMS_R_SYSTEM.ResourceID,
    SMS_R_SYSTEM.ResourceType,
    SMS_R_SYSTEM.Name,
    SMS_R_SYSTEM.SMSUniqueIdentifier,
    SMS_R_SYSTEM.ResourceDomainORWorkgroup,
    SMS_R_SYSTEM.Client
    from
    SMS_R_System
    where
    SMS_R_System.OperatingSystemNameandVersion like "%Workstation%"/"%Server%"
    and SMS_R_System.OperatingSystemNameandVersion like "%6.2%"
    We
    are trying to better understand customer views on social support experience, so your participation in this
    interview project would be greatly appreciated if you have time.
    Thanks for helping make community forums a great place.

  • Hash table/Collection

    How do you translate a hash table to a collection? Specifically with the Vector class.

    That question doesn't really make sense. You can easily take data in a hashtable and put it in a subclass of Collection, but how one does so, or whether it's appropriate, is doing to depend on the situation. There isn't a "this is how you do it" answer to this.
    By the way, Hashtables are a collection, in the sense of being part of the Collections Framework, although not in the sense of being a subclass of Collection.

  • Building hash based on IMAP message headers to maintain uniqueness

    This is more of a general java question than Javamail but since it has relevance to IMAP, i thought of posting it here. Basically, I discovered it hard way that the IMAP server that I am working with (MS Exchange) isn't returning unique UID for the same message (for the same UIDVALIDITY on the folder). Under certain condition it simply purges the old message, creates a new message with a new UID (for example when read status on a message is changed, rather than changing the flag status, it purges the existing one and creates a new one with a new UID). I don't want to fight with Microsoft over this as all my previous attempts at pointing their errors have gone in vein.
    Given this, I want to build a hash out of all the message headers and probably the internal date. This i know will guarantee uniquness in majority of the cases. The question is how do I do that. Is doing an MD5 on all the headers the best way to go about it? Will i run into a hash collision type situation with this?

    Thanks bill. What's apparently happening in our exchange envrionment is that we have let's say a client A that deposits the message in Exchange store over MAPI. That client can create new message or change flags on existing, change priority and do all kinds of the stuff. When that client does whatever it does over MAPI, exchange somehow decides to flush the old message and create a new message even though the change was merely an update. I've confirmed with client A that they aren't doing anything unusual, its just that Exchange decides to do it their way. They actually are not violating the IMAP protocol either coz all they are doing is flushing the old message and creating a new one. My outlook client connected over IMAP handles this just fine in that as an end-user you don't see a delete followed by an add and hence i am assuming they must use something along the lines that I am planning on doing.
    Totally agree that not using UID is going to be expensive but they haven't left me with any choice.

  • Extending String class

    My job sucks. I need a Rope. Since I cannot extend final String (which I shouldn't anyway) I'm (thankfully) forced to create an aggregation of them. Or should I extend the new StringBuilder to make it create something stronger than an ordinairy String?
    Today's just one of these days...

    Because if you override equals and not hashCode you
    violate the general contract for Object.hashCode and
    your class will not function properly in conjunction
    with all hash-based collections.
    purpose of hashcode() I quote
    "Since computing an object's equality is a time-consuming task, Java also provides a quick way of determining if an object is equal or not, using hashCode(). This returns a small number based on the object's internal datastructure; if two objects have different hash codes, then they cannot be equal to each other. (Think of it like searching for two words in a dictionary; if they both begin with "A" then they may be equal; however, if one begins with "A" and the other begins with "B" then they cannot be equal.)
    The purpose of computing a hash code is that the hash should be quicker to calculate and compare than computing full object equality. Datastructures such as the HashMap implicitly use the hash code to avoid computing equality of objects where possible. One of the reasons why a HashMap looks up data faster than a List is because the list has to search the entire datastructure for a match, whereas the HashMap only searches those that have the same hash value.
    Importantly, it is an error for a class to have an equals() method without overriding the default hashCode() method. In an inheritance hierarchy, only the top class needs to provide a hashCode() method. This is discussed further below. "

  • HashCode method execution

    Dear Members :
    I have custom hashCode method in conjunction with overriden equals method, in my program and have following doubts/observations :
    [1] In order to verify whether the hashCode method is executed, I put a println statement within method, but this doesn't output when the program is run.
    [2] The program is a simple equality testing of two dates and doesn't make use of any map. I want to know when and who gives (implicit ?) call to this hashCode method, when equals method is overriden, I mean the program flow.
    Since SUN suggests to override hashCode, whenever equals method is overriden, and my program is not making use of map, how and when the hashCode method gets called and if so why my test print is not executed ?
    Thanks in advance.
    Atanu

    uj_ wrote:
    jverd wrote:
    uj_ wrote:
    One way to graciously avoid the contract is to override hashCode and let it throw an exception (or assertion) stating "hashCode not implemented. This class is not intended for use in hash-based collections" or something.What would be the advantage of doing that over just taking the 5 minutes needed to implement it properly?Who are you to stipulate other people's design needs? Just because something takes just 5 minutes doesn't mean it's the right thing to do.
    You may want a class to have an equals relation based on content but you still may not want the class to be in a hash-based collection. The most compelling reason may be that it's not immutable.
    The equals/hashCode contract is mostly a Sun promise regarding "standard" classes. Everybody else design their classes the way they see fit.Calm down UJ. I have as much right to offer my opinion as you have to offer yours. I just asked a simple question.
    The requirement that a class not be used in a hash-based collection would be silly. It's up to the user of the class how to store instances. If the fields that go into determining hashCode are mutable, then that should be a documentation issue. There are good reasons to keep hashCode and equals in sync. The only reason not to do so would be the time it takes to write the code. Since that time is trivial for any well-designed class, it's an extremely weak reason.

  • HashCode

    Hi all,
    I studied some docs reagarding hashCode() and have some questions.
    From: java.lang.Object: hashCode():
    1) "Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application." Why?
    2) From: Java Glossory: http://www.mindprod.com/jgloss/hashcode.html
    Object.hashCode Implementation: The default hashCode() method uses the 32-bit internal JVM address of the Object as its hashCode. However, if the Object is moved in memory during garbage collection, the hashCode stays constant. This default hashCode is not very useful, since to look up an Object in a HashMap, you need the exact same key Object by which the key/value pair was originally filed. Normally, when you go to look up, you don't have the original key Object itself (Why?)
    just some data for a key. So, unless your key is a String, nearly always you will need to implement a hashCode and equals method on your key class.
    3) From the same text:
    "As a rule of thumb, any time you use an Object as a key in a Map or Set (e.g. Hashtable, HashMap, HashSet, TreeMap etc.) you must redefine both equals and hashCode in such a way both incorporate all the fields of the logical key. Fields in the key Object irrelevant to lookup should not be included in either method."
    Why do I need to implement the hashCode() method anyway? Why the 32-bit internal JVM address of the Object which is returned from hashCode() method wouldn't be enough to give me the key I need?
    4) From Java theory and practice: Hashing it out: http://www-128.ibm.com/developerworks/java/library/j-jtp05273.html
    "Why override equals() and hashCode()?
    What would happen if Integer did not override equals() and hashCode()? Nothing, if we never used an Integer as a key in a HashMap or other hash-based collection. However, if we were to use such an Integer object for a key in a HashMap, we would not be able to reliably retrieve the associated value, unless we used the exact same Integer instance in the get() call as we did in the put() call. This would require ensuring that we only use a single instance of the Integer object corresponding to a particular integer value throughout our program. Needless to say, this approach would be inconvenient and error prone."
    Why its error prone? Why can't we say that we are using the same instance of the Integer object?
    5) Why do I need to override hashCode() for HashSet? We don't have the concept of key in HashSet?
    6) Its also said that no matter what algorithm is used for calculating hashCode, its possible that two non-equal objects have the same hashCode, so if we are using those objects as keys in HashTable, how can we avoid having the same hashCode or the same key for two different values? On the other word, how can we avoid collisions, (if there is anyway) ?
    I stopped going through more documents at this point, since it seems more questions will arise! If I know the answer to the above questions it will help to understand the rest of document. Any help is greatly appreciated.

    comparisons on the object is modified. This
    integer need not remain consistent from one execution
    of an application to another execution of the same
    application." Why? Because there's no reason to do so. HashCode isn't intended as a persistent identifier for an object. It's simply intended as a fingerprint of the object's current state.
    key/value pair was originally filed. Normally,
    when you go to look up, you don't have the original
    key Object itself (Why?) I do map.put(joe.name, joe) later, when somebody asks for the object named "joe"--using the String "joe"--I use that to do person = map.get(requestedName); . When the string "joe" comes in, that lookup is how I find the associated object.
    hy do I need to implement the hashCode() method
    anyway? Why the 32-bit internal JVM address of the
    Object which is returned from hashCode() method
    wouldn't be enough to give me the key I need? Hashcode cannot use any data that equals does not use. The default hashCode uses (in general, and roughly) the object's address.
    The way a hashMap works is you use the object's hashCode to get to the right bucket--the small subset of all the entries that might potentially match--and then do a linear search on the bucket using equals.
    If I say map.get("joe"), the get method first computes the hashCode of the "joe" String object. Every time I provide "joe", I have to get the same hashCode, in order to get the right bucket--the right subset--to search. If String didn't override hashCode, then two different "joe"String objects would have two different hashCodes, and I'd never be able to zoom in on the bucket containing the "joe" entry.
    Likewise, any object that you want to use as a key in a HashMap, or store in a HashSet, must override hashCode so that the hashCode can be used to find the correct subset that might contain an entry for which equals() is true.
    http://en.wikipedia.org/wiki/Hashing
    Why its error prone? Why can't we say that we are
    using the same instance of the Integer object? I don't have the time to explain this any more. Read the wiki link above, or an introductory text on data structures.

  • Performance issue in linux while using set with URL object

    Hi,
    I am facing performance issue while using Set(HashSet) with URL object on linux. But it is running perfectly on windows.
    I am using
    set.contains(urlObject)
    Above particular statement is taking 40 sec on Linux, and only a fraction of ms on windows.
    I have checked the jre version on both OS. It is the same version (jre6)
    on both the OS.
    Could anyone please tell me what is the exact reason, why the same statement is taking more time on linux than windows.
    Thanks & Regards
    Naveen

    jtahlborn wrote:
    I believe the URL hashCode/equals implementations have some /tricky behavior which involves network access in order to run (doing hostname lookups and the like). you may want to either use simple Strings, or possibly the URI class (i think it fixed some of this behavior, although i could be wrong).The second new thing I have learned today. I was wrong in reply # 1 because looking at the URL code for 1.6 I see that the hash code is generated from the IP address and this has a lazy evaluation. Each URL placed in a HashMap (or other hash based collection) requires a DNS lookup the first time the hash code is used.
    P.S. 40 seconds does seem a long time for a DNS lookup!
    Edited by: sabre150 on Feb 13, 2008 3:40 PM

  • Write Behind HibernateCacheStore with Composite id entity

    I am creating a cache map whose key is the primary key of a table, and it is composite. Coherence is not letting me store these composite objects using HibernateCacheStore, and throws the following error.
    Conflicting identifier information between entity IntradayMarketData@1784427 and id IntradayMarketDataPK@ad455e8d
    Coherence document says the following.
    Hibernate entities accessed via the HibernateCacheStore module must use the "assigned" ID generator and also have a defined ID property.
    When I remove the composite key, and keep a single column as pk, it works fine.
    Any help regarding this would be appreciated.
    Thanks in advance
    Booshan

    Hi Booshan,
    I believe, you mix things up a bit.
    It is required by Coherence that the cache key must be the primary key as Hibernate uses it, so that when Coherence tries to resolve a cache miss (you request the value for a key which is not already in the cache, it will request that object from Hibernate by the cache key used where Hibernate expects the primary key. From what you write, this requirement is satisfied.
    The HibernateCacheStore requires that an assigned ID generator should be used only means that you must assign the new primary key value to an object yourself, you cannot rely on Hibernate to do so. This is required so that you are able to satisfy the previous requirement, and the primary key fields must be set within the cached value to the same value represented by the primary key object.
    So all you need is to set the primary key attributes in the entity to the same value as contained in the composite primary key object as used by Hibernate. Also this class must implement java.io.Serializable, but that is required by Hibernate, too.
    Also, for being able to use the key class as a key in a hash-based collection, the key class must properly override hashCode() and equals(). Please look at the requirements for this in the Javadoc of java.lang.Object.
    For performance reasons it is recommended for the key class to also implement com.tangosol.io.ExternalizableLite.
    E.g. in your case, using what I can from your example, your key class would look something like:
    import java.io.DataInput;
    import java.io.DataOutput;
    import java.io.IOException;
    import com.tangosol.io.ExternalizableLite;
    import com.tangosol.util.ExternalizableHelper;
    public class IntradayMarketDataPK implements ExternalizableLite
        private String left;
        private long right;
         * Parameterless constructor required by implementing ExternalizableLite
        public IntradayMarketDataPK() {
        public String getLeft()
            return left;
        public void setLeft(String left)
            this.left = left;
        public long getRight()
            return right;
        public void setRight(long right)
            this.right = right;
        @Override
        public void readExternal(DataInput input) throws IOException
            left = ExternalizableHelper.readSafeUTF(input);
            right = ExternalizableHelper.readLong(input);
        @Override
        public void writeExternal(DataOutput output) throws IOException
            ExternalizableHelper.writeSafeUTF(output, left);
            ExternalizableHelper.writeLong(output, right);
        @Override
        public boolean equals(Object obj)
            if (obj == this) {
                return true;
            if (obj == null) {
                return false;
            if (obj.getClass() == this.getClass()) {
                IntradayMarketDataPK other = (IntradayMarketDataPK)obj;
                if (other.right != right) {
                    return false;
                if (left == null) {
                    return other.left == null;
                } else {
                    return left.equals(other.left);
            return false;
        @Override
        public int hashCode()
            int hash = (int)(right ^ (right >>> 32));
            if (left != null) {
                hash ^= left.hashCode();
            return hash;
    }Your entity mapping should contain something like the following:
    <class name="IntradayMarketData" table="INTRADAY_MARKET_DATA">
      <composite-id name="pk" class="IntradayMarketDataPK">
        <key-property name="left" column="LEFT" />
        <key-property name="right" column="RIGHT" />
      </composite-id>
    </class>Your entity class should be something like:
    import java.io.DataInput;
    import java.io.DataOutput;
    import java.io.IOException;
    import java.io.Serializable;
    import com.tangosol.io.ExternalizableLite;
    import com.tangosol.util.ExternalizableHelper;
    public class IntradayMarketData implements ExternalizableLite
        private IntradayMarketDataPK pk;
         * Public parameterless constructor required by implementing ExternalizableLite
        public IntradayMarketData() {
        public IntradayMarketDataPK getPk()
            return pk;
        public void setPk(IntradayMarketDataPK pk)
            this.pk = pk;
        @Override
        public void readExternal(DataInput input) throws IOException
            IntradayMarketDataPK pk = new IntradayMarketDataPK();
            pk.readExternal(input);
            this.pk = pk;
        @Override
        public void writeExternal(DataOutput output) throws IOException
            pk.writeExternal(output);
    }You should always ensure that if you put an IntradayMarketData object to the cache, then the pk attribute on it must already be set to the object used as a cache key.
    Best regards,
    Robert

  • Collection based query based on domain

    Hello Everyone, 
    I am having a problem creating a custom collection in my SCCM enviornment
    I want query based collection All Windows Operating
    Systems  except one domain ( In our environment we have 20 domain , in this  i need to exclude one domain)
    Could you help anyone to get this please

    Understood Jeremy,
    Do you have sub domains under the one you want to exclude?
    Like sub.exclude.com under exclude.com?
    We don't have child domain on this.
    In our environment total 20000 machines are there. On the domain excluding having 9000 machines. 
    while I executing with this query i am getting total machines count is 17000 , suppose I have to get around 11000 machines. 
    Please help on this. I have to show my management today, because today is deadline for this. Please help on this 

Maybe you are looking for

  • Duplicate Messages in Mail

    My Mail app is loading multiple copies of the same email, even after being deleted multiple times. I've erased all rules, so it's not that. And on my iPhone, I only get it once, so I know it's the Mac mail app itself. Any thoughts?

  • Include the file dynamicly by file name

    in my jsp,I want include another jsp dynamicly <%@ include file="thefile.jsp" %> thefile.jsp should be dynamic like: <%=String filename=="thefile.jsp" %> <%@ include file=filename%> I know above is not working since file only take static name. is the

  • Discount for employees of a business.

    Why did my discount go away if I still work with a company that has discounts even though I sent proof that I work as a franchisee with that company? Does this not extend to franchisees? Are we not part of that system doing business?

  • TS3999 Help request for Iphone 4S calendar that won't sync with Mac OS X

    My I-Phone 4S (6.1.3) calendar changes won't sync with my MAC OS-X (10.8.2) after trying the Apple support suggestions.  Please provide your solutions if you have experience with this issue.  Thank you in advance.

  • LTD YTD & Month Values

    Hi, I'm displaying the units in columns as LTD YTD UNITS values. LTD & YTD is calculated as restricted keyfigures. I have the Calender Year/Month in free characteristics. When an user drills down by month only the UNITS column should expand by month