022.day22 Character包装类 Collections工具类 Properties类

补充内容

一、Character包装类

针对字符有某些扩展用法

  • 是否小写字母:isLowerCase
  • 是否大写字母:isUpperCase
  • 是否为数字:isDigit
  • 是否为字母:isLetter
  • 是否为字母或数字:isLetterOrDigit
  • 转换为小写:toLowerCase
  • 转换为大写:toUpperCase
  • 是否为空格:isWhitespace
// TODO Character包装类
		// 判断某一个字符的类别
		Character character = '6';
		System.out.println(Character.isDigit(character));

二、Collections工具类

方法基本均为静态方法,本身只起到工具的作用

  • 同步线程:synchronizedList,synchronizedMap,synchronizedSortedMap,synchronizedSortedSet,synchronizedSet
  • 排序:sort
  • 元素反转:reverse
// TODO Collections - 同步线程
		// 本身是一共工具类,本身不是不是一个集合对象或接口
		// 支持的方法几乎都是静态方法,在调用时需要传入一个需要操作的结合作为参数
		// ArrayList,TreeSet,TreeMap,HashSet,HashMap - 线程不安全/效率较高
		List<Integer> list = new ArrayList<Integer>();
		list = Collections.synchronizedList(list);
		Map<String, Integer> map = new HashMap<>();
		map = Collections.synchronizedMap(map);
		// 经过工具类转换之后可以继续正常使用,支持多线程同步
// TODO Collections - sort排序
		Map<Student, Integer> map = new HashMap<>();
		map.put(new Student("sand",22),1);
		map.put(new Student("sand",21),2);
		map.put(new Student("sand",19),3);
		map.put(new Student("sand",20),4);
		ArrayList<Map.Entry<Student, Integer>> list1 = new ArrayList<>();
		list1.addAll(map.entrySet());
		Set<Integer> set = new HashSet<Integer>();
		set.add(100);
		set.add(300);
		set.add(155);
		set.add(722);
		set.add(244);
		set.add(45);
		set.add(6);
		List<Integer> list2 = new ArrayList<>();
		list2.addAll(set);
		// 当不传入定制比较器时 - 内部元素实现了自然排序接口,则可以实现自然升序
		Collections.sort(list2);
		for (Integer integer : list2) {
			System.out.println(integer);
		}
		System.out.println();
		// 当需要倒序存储时
		// 1.逆转定制比较器
		// 2.使用reverse方法
		Collections.reverse(list2);
		for (Integer integer : list2) {
			System.out.println(integer);
		}

三、Properties类

主要用于方便的操作配置文件,文件内容也是以键值对儿的形式存在

  • 实例化对象
Properties properties = new Properties();
  • 载入文件
properties.load(new FileInputStream(new File("src/db.properties")));
  • 通用写法
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
properties.load(is);
  • 读取内容
properties.get("xxx");
// TODO 读取properties配置文件
		// 1.通过文件流打开文件
		InputStream inputStream = new FileInputStream(new File("src/db.properties"));
		// 2.实例化一个Properties工具类
		Properties properties = new Properties();
		// 3.将文件内容载入到properties中
		properties.load(inputStream);
		// 4.取出信息
		System.out.println(properties.getProperty("user"));
		System.out.println(properties.getProperty("password"));
// TODO 通过加载器寻找资源
		// 1.通过文件流打开文件
		InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
		// 2.实例化一个Properties工具类
		Properties properties = new Properties();
		// 3.将文件内容载入到properties中
		properties.load(inputStream);
		// 4.取出信息
		System.out.println(properties.getProperty("user"));
		System.out.println(properties.getProperty("password"));
原文地址:https://www.cnblogs.com/yokii/p/9477467.html