Set集合实现有序

Set集合实现有序今天面试问到Set集合实现有序的问题,发现集合这部分知识要补一补…. 以下所有api描述来源:https://docs.oracle.com/javase/7/docs/api/实现Set接口的类如下,其中最常见的HashSet和TreeSet。InterfaceSet<E>AllKnownImplementingClasses:AbstractSet, Concurren…

大家好,又见面了,我是你们的朋友全栈君。

今天面试问到Set集合实现有序的问题,发现集合这部分知识要补一补…. 

以下所有api描述来源:https://docs.oracle.com/javase/7/docs/api/

实现Set接口的类如下,其中最常见的HashSet和TreeSet。

Interface Set<E>

其中,TreeSet的构造方法如下:

Constructor and Description
TreeSet()

Constructs a new, empty tree set, sorted according to the natural ordering of its elements.
TreeSet(Collection<? extends E> c)

Constructs a new tree set containing the elements in the specified collection, sorted according to the 
natural ordering of its elements.
TreeSet(Comparator<? super E> comparator)

Constructs a new, empty tree set, sorted according to the specified comparator.
TreeSet(SortedSet<E> s)

Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.

TreeSet提供了一个参数为Colleciton的构造方法,利用提供的集合的所有元素进行自然排序后构造一个新的TreeSet集合。

该方法详细描述:

Constructs a new tree set containing the elements in the specified collection, sorted according to the 
natural ordering of its elements. All elements inserted into the set must implement the 
Comparable interface. Furthermore, all such elements must be 
mutually comparable
e1.compareTo(e2) must not throw a 
ClassCastException for any elements 
e1 and 
e2 in the set.
Parameters:

c – collection whose elements will comprise the new set

Throws:

ClassCastException – if the elements in 
c are not 
Comparable, or are not mutually comparable
NullPointerException – if the specified collection is null

需要注意的是:1.所有插入set的元素都要实现Comparable接口,即可自然排序。

                        2.任意两个元素之间都是可以相互比较并且不会抛出类型转换异常,也就是类型一致。

综上,实现Set集合排序,可以通过直接使用TreeSet储存,或者将要实现排序的集合作为参数构造新TreeSet集合,得到的TreeSet集合就是有序集合了。

写个类测试一下…

假设现在有无序的HashSet集合装有若干Sort类型元素,要把元素按value值的大小排序。那么我在Sort类中实现Comparable接口,然后将该HashSet集合作为参数构造新的TreeSet即可得到有序的Set集合。

代码:

import java.util.HashSet;
import java.util.TreeSet;

public class Sort implements Comparable<Sort>{
	//排序依据
	private Integer value;
	//有参构造
	public Sort(Integer value){
		this.value = value;
	}
	
	@Override
	public int compareTo(Sort o) {
		//正序
		return this.value-o.value;
	}
	@Override
	public String toString() {
		return value+"";
	}
	
	public static void main(String[] args) {
		
		HashSet<Sort> set = new HashSet<Sort>();
		//生成数据
		for(int i=0; i<16; i+=2){
			set.add(new Sort(i));
		}
		//利用HashSet中的元素通过元素的自然排序构造一个新TreeSet
		TreeSet treeSet = new TreeSet(set);
		//打印结果
		System.out.println("HashSet:"+set);
		System.out.println("TreeSet:"+treeSet);
	}
}

控制台输出:

HashSet:[12, 4, 10, 8, 14, 0, 2, 6]
TreeSet:[0, 2, 4, 6, 8, 10, 12, 14]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/140023.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 屏蔽了网页里的二维码怎么取消_怎么把手机转成网页版

    屏蔽了网页里的二维码怎么取消_怎么把手机转成网页版最近在做微信公众号的开发,在菜单加入外部链接时,点击后一直提示“非微信官方网页,将由微信转换为手机预览模式”,请问怎么去掉这个提示页面直接进去外部链接?解决方法:设置一下业务域名即可,一共可以设

    2022年8月6日
    5
  • HttpCanary下载_简单自我介绍网页代码

    HttpCanary下载_简单自我介绍网页代码前言首先,我们无论学习哪个框架,都要带着问题,带着思考去学习思考1:HttpRunner是什么?思考2:HttpRunner的设计模式是什么?思考3:为什么我们要学习HttpRunner?他的

    2022年7月30日
    5
  • navigator 对象_monitor对象

    navigator 对象_monitor对象Navigator接口表示用户代理的状态和标识。它允许脚本查询它和注册自己进行一些活动

    2025年10月30日
    4
  • matlab——for循环「建议收藏」

    matlab——for循环「建议收藏」简单for循环for循环用来循环处理数据。例:输出1~100的和>>clear>>sum=0;>>fori=1:100sum=sum+i;end>>sumsum=5050解释:i从1到100,每次增加一个,该共循环100次注意:分号的位置;不能使用“+=”符号…

    2022年6月25日
    41
  • 回文数「建议收藏」

    回文数「建议收藏」回文数

    2022年4月24日
    36
  • Linux下的文本编辑器vi

    Linux下的文本编辑器vi在终端中打开输入vi[文件],启动vi。vi有三种工作模式:命令模式、文本编辑模式、最后行模式。命令模式是启动vi进入的工作模式,在此模式下输入i,I,a,A,o,O,r,R命令中的任何一个即可进入文本编辑模式。此时在状态/命令区出现“—INSERT—”字样。在文本编辑模式下可输入文本内容,用上、下、左、右方向键移动光标,使用【Del】键和【Backspace】键删除字符,按【Esc】键回到…

    2022年7月26日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号