[英]How to sort the elements of an ArrayList according to their ascii values?
I have read a file using Scanner and then used a HashMap and ArrayList to sort the words according to the number of occurrences of words(ascending and descending) and everything is working fine. 我已经使用Scanner读取了一个文件,然后使用HashMap和ArrayList根据单词出现次数(升序和降序)对单词进行排序,一切正常。 But I want the output to be sorted in a way that it first displays numbers then uppercase and then lowercase.
但是我希望输出的排序方式是首先显示数字然后是大写,然后是小写。
Following is my code for the same: 以下是我的相同代码:
`Scanner scanner = new Scanner(new File("file"));
Map<String, Integer> map = new HashMap<String, Integer>();
int count=0;
String whole="";
while (scanner.hasNext())
{
count++;
String word = scanner.next();
whole=whole + " " + word;
if (map.containsKey(word))
{
map.put(word, map.get(word)+1);
}
else
{
map.put(word, 1);
}
}
List<Map.Entry<String, Integer>> entries = new ArrayList<Entry<String,Integer>>( map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
return a.getValue().compareTo(b.getValue());
}
});
System.out.println("Count: " +count);
System.out.print("Output 1(Ascending): ");
for(int j = 0; j < map.size(); j++){
System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
}
System.out.print("Output 2(Descending): ");
for(int i = 0; i < map.size(); i++){
System.out.println(entries.get(entries.size() - i - 1).getKey()+" "+entries.get(entries.size() - i - 1).getValue());
}`
My input is: 我的意见是:
I have 10 dogs and all the dogs are of different size
我有10只狗,所有的狗都有不同的大小
And my output is: 我的输出是:
Count: 12
数:12
Output 1(Ascending): all 1输出1(升序):全1
the 11
size 1大小1
are 1是1
and 1和1
of 11
have 1有1
I 1我1
different 1不同1
10 110 1
dogs 2狗2
Output 2(Descending): dogs 2输出2(降序):狗2
10 110 1
different 1不同1
I 1我1
have 1有1
of 11
and 1和1
are 1是1
size 1大小1
the 11
all 1全部1
Desired output: 期望的输出:
dogs 2 \\since it has more number of occurrences than any other word
狗2 \\因为它出现的次数比任何其他单词多
10 1 \\since it is a number10 1 \\因为它是一个数字
I 1 \\since it is an uppercase letter我1 \\因为它是一个大写字母
have 1 \\followed by all the lowercase words有1 \\后跟所有小写单词
I've written a code snippet and the following should work for print the input in ascending order. 我编写了一个代码片段,以下内容适用于按升序打印输入。
String input = "I have 10 dogs and all the dogs are of different size";
String [] inputSplit = input.split(" ");
Map<String, Integer> map = new HashMap<>();
for (int i=0; i < inputSplit.length; i++) {
String word = inputSplit[i];
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
}
else {
map.put(word, 1);
}
}
List<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
int compareWordCount = a.getValue().compareTo(b.getValue());
if (compareWordCount == 0) {
return a.getKey().compareTo(b.getKey());
}
return compareWordCount;
}
});
for (int j=0; j < entries.size(); j++) {
System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
}
Result 结果
10 1
I 1
all 1
and 1
are 1
different 1
have 1
of 1
size 1
the 1
dogs 2
Update with descending sort 使用降序排序更新
String input = "I have 10 dogs and all the dogs are of different size";
String [] inputSplit = input.split(" ");
Map<String, Integer> map = new HashMap<>();
for(int i = 0; i < inputSplit.length; i++){
String word = inputSplit[i];
if(map.containsKey(word)){
map.put(word, map.get(word) + 1);
}
else{
map.put(word, 1);
}
}
List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
Comparator <Entry<String, Integer>> ascComparator = new Comparator<Entry<String, Integer>>(){
@Override
public int compare(Entry<String, Integer> a, Entry<String, Integer> b) {
int compareWordCount = a.getValue().compareTo(b.getValue());
if(compareWordCount == 0){
return a.getKey().compareTo(b.getKey());
}
return compareWordCount;
}
};
Comparator <Entry<String, Integer>> descComparator = new Comparator<Entry<String, Integer>>(){
@Override
public int compare(Entry<String, Integer> a, Entry<String, Integer> b) {
int compareWordCount = a.getValue().compareTo(b.getValue());
if(compareWordCount == 0){
return b.getKey().compareTo(a.getKey());
}
return compareWordCount;
}
};
System.out.println("Ascending Sort");
Collections.sort(entries, ascComparator);
for(int j = 0; j < entries.size(); j++){
System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
}
System.out.println("\nDescending Sort");
Collections.sort(entries, descComparator);
for(int j = 0; j < entries.size(); j++){
System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
}
Results 结果
Ascending Sort
10 1
I 1
all 1
and 1
are 1
different 1
have 1
of 1
size 1
the 1
dogs 2
Descending Sort
the 1
size 1
of 1
have 1
different 1
are 1
and 1
all 1
I 1
10 1
dogs 2
you can extend your comparison logic to add ascii sort logic as well 您可以扩展比较逻辑以添加ascii排序逻辑
public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
int i = a.getValue().compareTo(b.getValue());
if(i==0) {
i = b.getKey().compareTo(a.getKey());
}
return i;
}
Hope this helps. 希望这可以帮助。
Just update your compare method part: 只需更新比较方法部分:
Scanner scanner = new Scanner(new File("file"));
Map<String, Integer> map = new HashMap<String, Integer>();
int count=0;
String whole="";
while (scanner.hasNext())
{
count++;
String word = scanner.next();
whole=whole + " " + word;
if (map.containsKey(word))
{
map.put(word, map.get(word)+1);
}
else
{
map.put(word, 1);
}
}
List<Map.Entry<String, Integer>> entries = new ArrayList<Entry<String,Integer>>( map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
int compareResult = a.getValue().compareTo(b.getValue());
if (compareResult != 0) {
return compareResult;
} else {
//Here, when the occurance numbers are equal, then compare
//with the key values where the texts are stored
return a.getKey().compareTo(b.getKey());
}
}
});
System.out.println("Count: " +count);
System.out.print("Output 1(Ascending): ");
for(int j = 0; j < map.size(); j++){
System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
}
System.out.print("Output 2(Descending): ");
for(int i = 0; i < map.size(); i++){
System.out.println(entries.get(entries.size() - i - 1).getKey()+" "+entries.get(entries.size() - i - 1).getValue());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.