您的位置:首页 > 教育 > 锐评 > 问题-解决

问题-解决

2025/8/25 22:07:04 来源:https://blog.csdn.net/2301_80708315/article/details/141707586  浏览:    关键词:问题-解决

在collection中的SetTest4_Student上面

此时我想解决LinkedHashSet的自定义降序身高问题是现在不行了

难道只能在构造器里面完成吗 

还是说明只能为int类型

Double.compare(o1.getHeight(),o2.getHeight()) 

在这道题中我在使用为什么不通过

 Map<String, Integer> map = new LinkedHashMap<String, Integer>(); 

正确答案 

  Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();

那么就要了解Character与String与char的区别

Character是char的包装类,就像Integer和int ,以及Long和long一样。

Character是char的包装类,注意它是一个类,提供了很多方法的。

 


public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String line = scanner.nextLine();//hashMap的使用Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();//遍历字符串int i;for(i=0;i<=line.length()-1;i++){char ch=line.charAt(i);//判断是否为字母if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'){//如果哈希表中存在该字符if(map.containsKey(ch)==true){//将哈希表中的该字符数+1map.put(ch,map.get(ch)+1);}//哈希表中不存在该字符else{//将该字符加入哈希表map.put(ch,1);   }}}//write your code here......Set<Map.Entry<Character, Integer>> entrys = map.entrySet();for (Map.Entry<Character, Integer> entry : entrys) {System.out.println(entry.getKey() + ":" + entry.getValue());}}
}

 

 System.out.println("请输入单词");Scanner  sc =new Scanner(System.in);String ch =sc.nextLine();Map<String,Integer> map =new LinkedHashMap<>();for (int i = 0; i <ch.length() ; i++) {char c =ch.charAt(i);if(c>='a'&&c<='z'||c>='A'&&c<='Z'){map.put(ch,map.get(c)+1);}else {map.put(ch,1);}}Set<Map.Entry<String, Integer>> entries = map.entrySet();for (Map.Entry<String, Integer> entry : entries) {System.out.println(entry.getKey()+":"+entry.getValue());}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com