1.
- public class WordEntity implements Comparable {
-
private Integer count = null; -
public String getWord() { -
return word; -
} -
public void setWord(String word) { -
this.word = word; -
} -
public Integer getCount() { -
return count; -
} -
public void setCount(Integer count) { -
this.count = count; -
} -
private String word = null; -
public WordEntity(String word, Integer count) { -
this.count = count; -
this.word = word; -
} -
@Override -
public int compareTo(WordEntity o) { -
int cmp = count.intValue() - o.count.intValue(); -
return (cmp == 0 ? word.compareTo(o.word) : -cmp); -
} - }
2.
- import sun.reflect.generics.tree.Tree;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Set;
- import java.util.TreeSet;
- public class CountString {
-
public static TreeSet set = new TreeSet(); -
public static TreeSet outputStrs(String[] args) { -
int count = 0; -
for (int i = 0; i < args.length; i++) { -
String temp = args[i].replaceAll("your", ""); -
count = (args[i].length() - temp.length()) / "your".length(); -
if (count > 0) { -
set.add(new WordEntity(args[i], count)); -
} -
} -
return set; -
} -
public static void main(String[] args) { -
String[] strings = {"your", "assfyour", "dsfdfyouryour"}; -
TreeSet hi = outputStrs(strings); -
for (Iterator he = hi.iterator(); he.hasNext(); ) { -
WordEntity w = he.next(); -
System.out.println(w.getWord()); -
} -
} - }