java知识点记录
文章目录
学习笔记
一、基本知识
1. 数组的打印:
(1) 打印单层数组:Arrays.toString()
(2) 打印多层数组:Arrays.deepToString(intNestedArray)
2. stream输出数组 stream().maptoInt().toArray
int[] ints = wordMap.entrySet().stream()
.sorted((o1, o2) -> {
return o1.getValue() - o2.getValue();
}).mapToInt(key-> key.getKey()).toArray();
3. Integer
-
进制转换
// 十进制 转其他
Integer.toHexString(1);
Integer.toBinaryString(1);
Integer.toOctalString(1);// 其他 转十进制 System.out.println(Integer.valueOf("0010", 2));
二、stream 流
1. stream流生成map对象:用Collector.toMap
key和value都是对象中的某个属性值:
Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getId, User::getName));
2. stream流生成array对象:用maptoInt().toArray
int[] ints = wordMap.entrySet().stream()
.sorted((o1, o2) -> {
return o1.getValue() - o2.getValue();
}).mapToInt(key-> key.getKey()).toArray();
3. list 的stream流对所有元素求和
Double sum = yList.stream().reduce(0.0, (a, b) -> a + b);
Double sum = yList.stream().reduce(0.0, Double::sum);
三、HashMap
1. map的遍历方式
// hashMap的7种遍历方式
HashMap<Integer,Integer> hashMap = new HashMap<>();
hashMap.put(1,10);
hashMap.put(2,20);
/*
// ① keyset
Iterator it = hashMap.keySet().iterator();
Object key;
Object value;
while(it.hasNext()) {
key = it.next();
value = hashMap.get(key);
System.out.println(key + ":" + value);
}
*/
/*
// ②valueset Map.Entry
Iterator it = hashMap.entrySet().iterator();
Object key;
Object value;
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
key = entry.getKey();
value = entry.getValue();
System.out.println(key + "--" + value);
}
*/
/*
// 1:Iterator 迭代器遍历entryset --最高效
Iterator<Map.Entry<Integer,Integer>> it = hashMap.entrySet().iterator();
while(it.hasNext()) {
Map.Entry<Integer, Integer> next = it.next();
Object key = next.getKey();
Object value = next.getValue();
}
*/
// 2:Iterator遍历Keyset
Iterator<Integer> it = hashMap.keySet().iterator();
while(it.hasNext()) {
Integer key = it.next();
Integer value = hashMap.get(key);
}
// 3:foreach 遍历entrySet
for (Map.Entry tmp: hashMap.entrySet() ) {
Object key = tmp.getKey();
Object value = tmp.getValue();
}
Iterator<Map.Entry<Integer,Integer>> ite = hashMap.entrySet().iterator();
for (Map.Entry<Integer, Integer> tmp : hashMap.entrySet()
) {
tmp.getKey();
}
// 4:foreach 遍历entrySet
for (Integer keyTmp:hashMap.keySet() ) {
Object key = keyTmp;
Object value = hashMap.get(key);
}
// 5: lambda表达式遍历 --哇,厉害了
hashMap.forEach((key, value) -> {
System.out.println(key);
System.out.println(value);
}
);
// 6: Stream API单线程遍历
hashMap.entrySet().stream().forEach(integerIntegerEntry -> {
System.out.println(integerIntegerEntry.getKey());
System.out.println(integerIntegerEntry.getValue());
});
// 7: Stream API多线程遍历
hashMap.entrySet().stream().forEach(integerEntry -> {
integerEntry.getKey();
integerEntry.getValue();
});
hashMap.entrySet().stream()
.forEach(tmp ->{
System.out.println("----------");
System.out.println(tmp.getKey());
System.out.println(tmp.getValue());
});
Set<Integer> set = new HashSet<>();