华为OD机考题(基础API)
基础API
字典排序
List<String> list=new ArrayList();
//add list member
Arrays.sort(list);
List<Map<String,Integer>> list=new ArrayList();
//add list member
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2{
return o2.getValue()-o1.getValue();
}
});
进制转换
Integer.parseInt(hexString, 16)//16进制转10进制
Integer.toBinaryString(num);//十进制转二进制
int decimal = Integer.parseInt(binaryString, 2);//二进制转10进制
>> 有符号右移
>>>无符号右移
bit处理
Integer.bitCount(n);//二进制中1的个数
字典排序示例:
package huaweiod;
import java.util.*;
public class Test{
public static void main(String[] args) throws Exception{
Map<String,Map<String,Integer>> map=new HashMap();
Map<String,Integer> map1=new HashMap();
map1.put("004",160);
map1.put("003",150);
map1.put("002",150);
map1.put("001",160);
map1.put("005",150);
map.put("02",map1);
Map<String,Integer> map2=new HashMap();
map2.put("003",130);
map2.put("004",140);
map.put("01",map2);
Set<String> classNum=map.keySet();
Object[] array=classNum.toArray();
Arrays.sort(classNum.toArray());
for(Object o:array){
System.out.println("class:"+o);
HashMap<String,Integer> tempMap=(HashMap)map.get(o);
List<Map.Entry<String,Integer>> list=new ArrayList(tempMap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return o2.getValue()-o1.getValue();
}
});
for(Map.Entry<String,Integer> entry:list){
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
}
原文地址:https://blog.csdn.net/acuteeagle01/article/details/140512482
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!