目录
一、map集合特点以及遍历方式
二、字符串出现的 次数
三、泛型
四、工具类
一、map集合特点以及遍历方式
①、拿到key、在拿值
②、拿到映射关系、键值都有了
代码展示:
public static void main(String[] args) {
// Hashtable table = new Hashtable<>();
// table.put("a", 1);
// table.put(null, null);
Map map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
// map.put(null, null);
map.put("c", 3);
map.put("d", 4);
System.out.println(map);
//移除单个
Object remove = map.remove("b");
System.out.println(remove);
System.out.println(map);
//修改 也是调用put方法
map.put("c", 32);
System.out.println(map);
//查询单个
System.out.println(map.get("c"));
//查询所有
// 1.先拿到map集合中的所有的key
Set keys = map.keySet();
for (Object key : keys) {
System.out.println("键:"+key+ " 值:"+map.get(key));
}
// 2.拿到映射关系
Set> entrySet = map.entrySet();
for (Entry entry : entrySet) {
System.out.println("键: "+entry.getKey()+"值: "+entry.getValue());
}
}
效果如下:
二、字符串出现的次数
①、实现思路:
1、做字符串切割、得到一个字符数组
2、接下来遍历、拿到单个字符
3、如果该字符没有出现过、即value值为null、那么该字符为KEY、值初始化为1
4、如果已经出现过、拿到原来的值+1
②、debugger的使用、调式代码
1、打断点
2、采用debug的方式运行程序
3、进入调式窗口、左上方有一个step、over调式箭头、快捷键F6
4、接下来可以用个F6一步步调式当前程序对应的每一个变量
char[] arr = s.toCharArray(); Mapmap = new HashMap<>(); for (char c : arr) { Integer value = map.get(c); if(value == null) { map.put(c, 1); } else { map.put(c, value+1); } // System.out.println(value); } map.entrySet().stream().sorted((x,y)->x.getValue()-y.getValue()).forEach(System.out::print); // Set > entrySet = map.entrySet(); // for (Entry entry : entrySet) { // System.out.println(entry.getKey() + ":" +entry.getValue()); // } }
效果如下:
三、泛型
之前:不健壮 的代码、会在运行时才会把错误暴露出来
之后:将潜在的问题暴露出来、早预防早治疗
将运行期出现的异常转换为编译器的错误
public static void main(String[] args) {
Set set = new HashSet<>();
set.add(1);
set.add(2);
set.add(3);
set.add(4);
set.add(5);
for (Object obj : set) {
if(Integer.valueOf(obj.toString())%2==0) {
System.out.println(obj);
}
// set.remove(obj);
}
}
}
class BookDao{
// add(BooK book);
// del(BooK book);
// edit(BooK book);
// list(BooK book);
}
class UserDao extends BaseDao{
}
class BaseDao{
void add(T t){
}
void ael(T t){
}
void edit(T t){
}
List list(T t){
return null;
}
}
class result{
void del(T t) {
}
}
效果如下:
四、工具类
①、集合之间的相互转换
1、数组转成集合,本质上依然是一个数组、长度是不可变的
2、集合与数组所具备方法是不一样的、入对于数组而言、就没有判断内部包含哪个元素
public static void main(String[] args) {
String [] arr= {"a","b","c","d"};
List list = Arrays.asList(arr);
// Object[] array = list.toArray();
list.add("e");
// java.lang.UnsupportedOperationException
System.out.println(list.size());
}
效果:
②、对于工具类其他方法的应用
sort
toString
public static void main(String[] args) {
// List list = new ArrayList<>();
// list.add("b");
// list.add("c");
// list.add("a");
// System.out.println(list);
// Collections.sort(list);
// System.out.println(list);
// //这里面x、y值得是集合中的元素
// Collections.sort(list,(x,y) -> y.compareTo(x));
// System.out.println(list);
List list = new ArrayList<>();
list.add(new Person("b",16));
list.add(new Person("c",18));
list.add(new Person("b",19));
System.out.println(list);
// Collections.sort(list);
Collections.sort(list,(x,y) -> x.getName().compareTo(y.getName()));
System.out.println(list);
Integer[] arr = {3,6,9,2,5,8};
Arrays.sort(arr,(x,y) -> y- x);
System.out.println(Arrays.toString(arr));
}
}
class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
// @Override
// public int compareTo(Person o) {
// // TODO Auto-generated method stub
// return o.getName().compareTo(this.name);
// }
效果:



