栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java基础笔记11-TreeSet-集合

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java基础笔记11-TreeSet-集合

java基础笔记11-TreeSet
  • TreeSet通过构造方法实现自定义排序
  • TreeMap通过构造方法实现自定义排序
  • Collections工具类
    • 排序操作

TreeSet通过构造方法实现自定义排序
TreeSet treeSet = new TreeSet(new Comparator() {
    @Override
    public int compare(Object o1, Object o2) {
        return ((String) o1).length() - ((String) o2).length();
    }
});
treeSet.add("bbb");
treeSet.add("a");
treeSet.add("ee");
treeSet.add("cccc");
System.out.println(treeSet);
TreeMap通过构造方法实现自定义排序
TreeMap map = new TreeMap<>(new Comparator() {
    @Override
    public int compare(String o1, String o2) {
        return o1.length() - o2.length();
    }
});
map.put("aaa", "cat");
map.put("bb", "pig");
map.put("c", "pig");
System.out.println(map);
Collections工具类
  • Collections是一个可以操作Set、List、Map等的工具类
  • 提供了一系列静态方法对集合元素进行查询、修改和排序等操作
排序操作
  • 反转list:Collections.reverse(list)
  • 对集合进行随机排序: Collections.shuffle(list)
  • 根据元素的自然顺序对指定集合的元素进行排序:Collections.sort(list)
  • 根据指定的Comparator产生的顺序对指定的集合元素排序
Collections.sort(list, new Comparator() {
	@Override
	public int compare(Object o1, Object o2) {
		return ((String)o1).length() - ((String)o2).length();
	}
})
  • swap(list, int i, int j):将指定list集合中的i和j元素交换
  • Collections.max(list):根据元素的自然顺序返回给定集合中的最大元素
  • Collections.max(Collection, comparator):根据Comparator指定的顺序,返回集中中最大的元素
  • Collections.min(list):根据元素的自然顺序返回给定集合中的最小元素
  • Collections.min(Collection, comparator):根据Comparator指定的顺序,返回集中中最小的元素
  • Collections.frequency(Collection, object):返回指定集合中指定元素出现的次数
  • Collections.copy(dest, list):把list中的元素拷贝到dest中
  • Collections.replaceAll(list, oldVal, newVal):把list中oldVal用newVal代替
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937417.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号