Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7886801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:24:45+00:00 2026-06-03T05:24:45+00:00

When I added my datas on the treeSet I lost almost all my datas.

  • 0

When I added my datas on the treeSet I lost almost all my datas. It seems that I have only the first element of my set.

I read this question Hashset vs Treeset for an code optimization and I tried to do something like them. But I didn’t really succeed.

Input :

int iValue = 0;

    HashSet<TagResult> results = new HashSet<TagResult>();
    for(Document doc : docs) {
        NodeList nList = doc.getElementsByTagName("TEXT");

        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                iValue = searchWords.searchOnTag(eElement, szSearch);
                if(iValue > 0) {
                    results.add(new TagResult(eElement, iValue));
                }
            }
        }
    }
    System.out.println("Set :\n-------------------------");
    for(TagResult result : results) {
        System.out.println(getTagValue("Tag",result.getElement()));
    }
    Set<TagResult> sortedResult = new TreeSet<TagResult>(new Comparator<TagResult>() {

        public int compare(TagResult e1, TagResult e2) {
            return e2.getValue() - e1.getValue();
        }
    });

    sortedResult.addAll(results);

    System.out.println("Sorted Result :\n-------------------------");
    for(TagResult result : sortedResult) {
        System.out.println(getTagValue("Tag",result.getElement()));
}

The execution result give me that :

Output

Set :

TXT_KEY_RELIGION_POSITIF_MEDIAN_05
TXT_KEY_RELIGION_POSITIF_MEDIAN_02
TXT_KEY_RELIGION_POSITIF_04
TXT_KEY_RELIGION_POSITIF_06
TXT_KEY_RELIGION_POSITIF_05
TXT_KEY_RELIGION_POSITIF_03
TXT_KEY_RELIGION_POSITIF_MEDIAN_06
TXT_KEY_RELIGION_POSITIF_MEDIAN_01
TXT_KEY_RELIGION_POSITIF_MEDIAN_04
TXT_KEY_RELIGION_POSITIF_MEDIAN_08
TXT_KEY_RELIGION_POSITIF_MEDIAN_03
TXT_KEY_RELIGION_POSITIF_02
TXT_KEY_RELIGION_POSITIF_07
TXT_KEY_RELIGION_POSITIF_01
TXT_KEY_RELIGION_POSITIF_MEDIAN_09
TXT_KEY_RELIGION_POSITIF_MEDIAN_07

Sorted Result :

TXT_KEY_RELIGION_POSITIF_MEDIAN_05

I don’t understand why I have this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:24:47+00:00Added an answer on June 3, 2026 at 5:24 am

    I would imagine that

    e2.getValue() - e1.getValue();
    

    returns 0 for all the items in your set which are then considered equal. Try printing tag.getValue() for your tags to check if it is the case.

    Extract from TreeSet javadoc (emphasis mine):

    Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several Core Data model version that I have added to my .xcdatamodeld
I have got list that is filled with data from internet. Datas are downloaded
i have problem with addslashes and stripslashes. i have added data like this: $data['sender_name']=
I have a database tumblelog (using mongoengine) in which I added some data in
I have an existing iPad application to which I've just added core data versioning.
I have a problem with MySql.Data in a partial trusted environment. I've added MySql.Data
I have added TableView which data will be dynamically changes after filling form. and
I have a documentFragment with several child nodes containing some .data() added like so:
I have a search page where every search result is added to the page
I understand there are MANY posts on this, but I have tried every single

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.