字符判断大小
Boolean Character.isLowerCase(char ch)
判断字符是否为小写(小写返回true)
Boolean Character.isUpperCase(char ch)
判断字符是否为大写(大写返回true)
if( Character.isLowerCase(str.charAt(i)) ^ Character.isLowerCase(str.charAt(1)) )
判断字符串第i个字符与字符串第1个字符串是否为相同大小写
异或运算法则是相同部位相同取0否则取1
队列方法
boolean offer(E e)方法->队列末尾添加元素
queue.offer("one");
queue.offer("two");
queue.offer("three");
queue.offer("four");
System.out.println(queue);//[one, two, three, four]
E poll()方法->取队首元素并出队
String str = queue.poll();
System.out.println(str); //one
System.out.println(queue); //[two, three, four]
E peek()方法->队首元素不出队
str = queue.peek();
System.out.println(str); //two
System.out.println(queue); //[two, three, four]
System.out.println("开始遍历!"); //输出:开始遍历!
add()与offer():在队尾增加元素,add方法在队列满会抛出队列已满异常,offer方法返回false表示队列已满。
remove()与poll():删除队列的头元素,remove方法在队列为空的情况下将抛异常,poll方法将返回null。
element()与peek():都是返回队列的头元素,但不删除头元素,element方法在队列为空的情况下,将抛异常,而peek方法将返回null。
Map搜索值
//map.getOrDefault(k,v)
//获取map中k的value值并返回,如果map中没有k值返回给定的v值。方法便于统计数组中相同值的数
public static void main(String[] args) {
int nums[]= {
11,1,1,5,3,2,3,8,6,4};
Map <Integer,Integer> map = new HashMap <Integer,Integer>();
for(int num : nums) {
map.put(num,map.getOrDefault(num, 0)+1);
}
System.out.print(map);
}
输出
{
1=2, 2=1, 3=2, 4=1, 5=1, 6=1, 8=1, 11=1}
优先队列PriorityQueue
小顶堆
PriorityQueue<Integer> queue = new PriorityQueue<Integer>(new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2){
return o1.compareTo(o2);
}
});
Map.Entry对象集与map.entrySet()取(k、v)值
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
entrySet()效率高于keySet()