发表于: 2019-11-01 21:02:58

1 1042


今天完成的事情:上午学习了JAVA基础:Map集合

public class Demo1_Map {

public static void main(String[] args) {

//demo1();//添加数据

//demo2();//删除数据,判断是否包含键或者值

demo3();//获取map中的所有的值

}

private static void demo3() {

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

map.put("张三", 23);

map.put("李四", 24);

map.put("王五", 25);

map.put("赵六", 26);

Collection<Integer> c = map.values();

System.out.println(c);

System.out.println(map.size());

}

public static void demo2() {

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

map.put("张三", 23);

map.put("李四", 24);

map.put("王五", 25);

map.put("赵六", 26);

//Integer value = map.remove("张三"); //根据键删除元素,返回键对应的值

//System.out.println(value);

System.out.println(map.containsKey("张三")); //判断是否包含传入的键

System.out.println(map.containsValue(100)); //判断是否包含传入的值

System.out.println(map);

}

public static void demo1() {

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

Integer i1 = map.put("张三", 23);//存入的过程先判断有没有"张三",没有就添加,有就覆盖,返回的是被覆盖的部分

Integer i2= map.put("李四", 24);

Integer i3 = map.put("王五", 25);

Integer i4 = map.put("赵六", 26);

Integer i5 = map.put("张三", 26); //相同的键不存储,值覆盖,把被覆盖的值返回

System.out.println(map);

System.out.println(i1);

System.out.println(i2);

System.out.println(i3);

System.out.println(i4);

System.out.println(i5);

}

public class Demo2_Iterator {

/**

* 通过查看Map集合的api发现没有iterator方法,那么双列集合如何迭代呢?

* 根据键获取值

*/

public static void main(String[] args) {

//demo1();//使用keySet方法,通过获取所有的键的集合,在遍历此集合,根据键再获取其对应的值

demo2();

}

private static void demo1() {

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

map.put("张三", 23);

map.put("李四", 24);

map.put("王五", 25);

map.put("赵六", 26);

// Integer i = map.get("张三"); //根据键获取值

// System.out.println(i);

//获取所有的键

Set<String> keySet = map.keySet(); //获取所有键的集合

Iterator<String> it = keySet.iterator(); //获取迭代器

while(it.hasNext()) { //判断集合中是否有元素

String key = it.next(); //获取每一个键

Integer value = map.get(key); //根据键获取值

System.out.println(key + "=" + value);

}

}

//使用增强for循环

private static void demo2() {

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

map.put("张三", 23);

map.put("李四", 24);

map.put("王五", 25);

map.put("赵六", 26);

//使用增强for循环遍历

for(String key : map.keySet()) { //map.keySet()是所有键的集合

System.out.println(key + "=" + map.get(key));

}

public class Demo3_Iterator {

/**

* Map集合的第二种迭代,根据键值对对象,获取键和值

*  A:键值对对象找键和值思路:

* 获取所有键值对对象的集合

* 遍历键值对对象的集合,获取到每一个键值对对象

* 根据键值对对象找键和值

*/

public static void main(String[] args) {

//demo1();

demo2();

}

public static void demo1() {

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

map.put("张三", 23);

map.put("李四", 24);

map.put("王五", 25);

map.put("赵六", 26);

//Map.Entry说明Entry是Map的内部接口,将键和值封装成了Entry对象,并存储在Set集合中

Set<Map.Entry<String, Integer>> entrySet = map.entrySet();

//获取每一个对象

Iterator<Map.Entry<String, Integer>> it = entrySet.iterator();

while(it.hasNext()) {

//获取每一个Entry对象

Map.Entry<String, Integer> en = it.next(); //父类引用指向子类对象

//Entry<String, Integer> en = it.next(); //直接获取的是子类对象(上下两个是可以的)

String key = en.getKey(); //根据键值对对象获取键

Integer value = en.getValue(); //根据键值对对象获取值

System.out.println(key + "=" + value);

}

}

//使用增强for循环进行迭代

public static void demo2() {

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

map.put("张三", 23);

map.put("李四", 24);

map.put("王五", 25);

map.put("赵六", 26);

//使用增强for循环进行迭代

for(Map.Entry<String, Integer> en : map.entrySet()) {

System.out.println(en.getKey() + "=" + en.getValue());

}

}

 需求:统计字符串中每个字符出现的次数

* 分析:

* 1,定义一个需要被统计字符的字符串

* 2,将字符串转换为字符数组

* 3,定义双列集合,存储字符串中字符以及字符出现的次数

* 4,遍历字符数组获取每一个字符,并将字符存储在双列集合中

* 5,存储过程中要做判断,如果集合中不包含这个键,就将该字符当作键,值为1存储,如果集合中包含这个键,就将值加1存储

* 6,打印双列集合获取字符出现的次数

*/

public static void main(String[] args) {

//1,定义一个需要被统计字符的字符串

String s = "aaaabbbbbccccccccccccc";

//2,将字符串转换为字符数组

char[] arr = s.toCharArray();

//3,定义双列集合,存储字符串中字符以及字符出现的次数

HashMap<Character, Integer> hm = new HashMap<>();

//4,遍历字符数组获取每一个字符,并将字符存储在双列集合中

for(char c: arr) {

//5,存储过程中要做判断,如果集合中不包含这个键,就将该字符当作键,值为1存储,如果集合中包含这个键,就将值加1存储

/*if(!hm.containsKey(c)) { //如果不包含这个键

hm.put(c, 1);

}else {

hm.put(c, hm.get(c) + 1);

}*/

hm.put(c, !hm.containsKey(c) ? 1 : hm.get(c) + 1);

}

//6,打印双列集合获取字符出现的次数

for (Character key : hm.keySet()) { //hm.keySet()代表所有键的集合

System.out.println(key + "=" + hm.get(key));//hm.get(key)根据键获取值

}

}

复习了一遍JDBCTemplate

public class StudentJDBCTemplate implements StudentDAO{

    private DataSource dataSource;

    private JdbcTemplate jdbcTemplateObject;

    public void setDataSource(DataSource dataSource) {

            this.dataSource = dataSource;

            this.jdbcTemplateObject =new JdbcTemplate(dataSource);

        }

    public void create(String name, Integer age) {

        String SQL = "insert into student (name, age) values (?, ?)";

        jdbcTemplateObject.update(SQL, name, age);

        System.out.println("Created Record Name = " + name + " Age = " + age);

        return;

    }

    public List<Student> listStudents() {

        String SQL ="select * from student";

       List<Student> students = jdbcTemplateObject.query(SQL,new StudentMapper());

        return students;

 明天计划的事情:任务17开始再写一下 理解清楚

之前只是照着敲 

遇到的问题:两处拼写错误导致的BUG 师兄带着百度解决
收获:

敲了一遍任务17 理解更深了 已经很熟悉JDBC的6步 

试着看报错提示 更快的搜索找到解决办法

试着去翻源码 虽然有许多还是不太理解 但解决了一个疑问 通过看源码理解了为什么要注入dataSource



返回列表 返回列表
评论

    分享到