简体   繁体   中英

Iterator is missing one element from the TreeSet while printing value

I am working on problem where I have to count the occurrence of duplicate character from the input and print the string of those characters where count in greater than n times in descending order.

I have written the code but since my iterator is missing one character even though it fulfills the criteria

Sample input is :

abbababbabkeleeklkel 
3 (=N) // where N=3

Sample output is :

bae
as 
1)b appears 6 times
2)a appears 4 times
3)e appears 4 times

Code is :

 private static void printDuplicate(String s, int times){ 

    TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
    boolean isAppear = true;
    for (int i = 0; i < s.length(); i++) { 
        char ch = s.charAt(i); 
        int count = 1;
        if(tm.containsKey(ch)){
            count=tm.get(ch) + 1;
        }
        tm.put(ch, count);
    }
    StringBuilder temp = new StringBuilder("");
    TreeSet<CharItem> ts = new TreeSet<CharItem>(); 
    Iterator<Character> it = tm.descendingKeySet().iterator();


    while (it.hasNext()){
        char ch = (char) it.next();
        int count = tm.get(ch);
        CharItem ci= new CharItem(ch, count);
        ts.add(ci);
        System.out.println("Character is " + ch + " count is " + ci.getCount());
        }
        Iterator<CharItem> it2 = ts.iterator(); 


        while(it2.hasNext()){

            CharItem ci=it2.next();


            System.out.println("Ci key value " + ci.getCh() + " and count is " + ci.getCount());


            if(ci.getCount() >times){

                isAppear=false;

                temp.append(ci.getCh());
             }

        }

        if(isAppear){
            System.out.println("NONE");
        }
        else
         System.out.println(temp);
    }
}

But output I received is " be " . Some what character a is missing. Can someone suggest me what could be the problem?

CharItem is a class implements Comparable:

class CharItem implements Comparable<CharItem>{
    private int count;
    private char ch;
    public CharItem(char c, int i){
        count = i;
        ch = c;
    }
    public char getCh() {
        return this.ch;
    }
    public int getCount() {
        return this.count;
    }
    @Override
    public int compareTo(CharItem b) {
        return b.count - this.count ;
    }

The problem is the implementation of your Comparator in CharItem. Your CharItems are equal if they have the same amount of occurences in the String. So you have to take your char into account. You should change your Comparator to:

@Override
public int compareTo(CharItem b) {
  int occurences = Integer.compare(b.count, count);
  if (occurences == 0) {
    return Character.compare(ch, b.ch);
  }
  return occurences;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM