java set 顺序

java set 顺序,第1张

在java语言中,提供多种不同的结构来组织对象,Set(集合)是其中的一种,本身是一个接口,其迭代时的顺序取决于其具体实现。典型的实现包括:

HashSet:哈希表是通过使用称为散列法的机制来存储信息的,元素并没有以某种特定顺序来存放;

LinkedHashSet:以元素插入的顺序来维护集合的链接表,允许以插入的顺序在集合中迭代;

TreeSet:提供一个使用树结构存储Set接口的实现,对象以升序顺序存储,访问和遍历的时间很快。

扩展资料

Set<String>set = new TreeSet<String>()   

set.add("f")

set.add("a")

set.add("b")

set.add("c")

set.add("d")

set.add("e")       

System.out.println(set)

参考资料:百度百科 set (计算机学)

// 如果使用Set进行排序,那么必须要满足以下两个条件:

// 1, 使用有序SET,即TreeSet

// 2, 被排序对象必须实现Comparable接口

// 这样做,其实是限制了排序的有效性(你可能不知道被排序对象是否实现了Comparable接口,而且你也不大可能要求程序中所有Model都实现这个接口)。

// 更好的排序方式是使用List进行排序,对List排序,只需要使用Collections.sort(list, comparator)静态方法。

// 第一个参数是要被排序的List,第二个参数是实现了Comparable接口的排序器。如果你要使用不同的排序方法,只需要替换第二个参数即可。

// 使用List进行排序,不需要被排序对象实现Comparable接口,而且排序算法是通过实现Comparable接口而来,这带来了极大的灵活性。

// 问题:我先将Set转为List,使用List排序后,再转为Set是否可行?

// 答案:基本不可行,如果将List转为无序Set,则不能保证顺序;若将List转为TreeSet,虽理论上说能保证顺序,但要求被排序对象实现Comparable接口。

// 可运行示例如下

import java.util.ArrayList

import java.util.Collections

import java.util.Comparator

import java.util.HashSet

import java.util.List

import java.util.Set

import java.util.TreeSet

public class Question {

class Person{

private int age

private String name

public String toString(){

return name + ":" + age

}

public int getAge(){

return age

}

public Person(String name, int age){

this.name=name

this.age = age

}

}

class PersonSortable extends Person implements Comparable<Person>{

public PersonSortable(String name, int age){

super(name, age)

}

@Override

public int compareTo(Person o) {

int thisAge = this.getAge()

int otherAge = o.getAge()

return thisAge - otherAge

}

}

class PersonComparator implements Comparator<Person>{

@Override

public int compare(Person o1, Person o2) {

int thisAge = o1.getAge()

int otherAge = o2.getAge()

return thisAge - otherAge

}

}

public Question(){

System.out.println("无序SET, Person未实现Comparable, 示例。")

try{

Set<Person>personSet = new HashSet<Person>()

personSet.add(new Person("John", 15))

personSet.add(new Person("Smith", 25))

personSet.add(new Person("Lee", 35))

personSet.add(new Person("Peter", 45))

personSet.add(new Person("Dick", 55))

System.out.println(personSet)

}catch(Exception e){

System.out.println("发生异常:" + e.getMessage())

}

System.out.println("有序SET,Person未实现Comparable, 示例。")

try{

Set<Person>personSet = new TreeSet<Person>()

personSet.add(new Person("John", 15))

personSet.add(new Person("Smith", 25))

personSet.add(new Person("Lee", 35))

personSet.add(new Person("Peter", 45))

personSet.add(new Person("Dick", 55))

System.out.println(personSet)

}catch(Exception e){

System.out.println("发生异常:" + e.getMessage())

}

System.out.println("无序SET, PersonSortable实现了Comparable, 示例。")

try{

Set<PersonSortable>personSet = new HashSet<PersonSortable>()

personSet.add(new PersonSortable("John", 15))

personSet.add(new PersonSortable("Smith", 25))

personSet.add(new PersonSortable("Lee", 35))

personSet.add(new PersonSortable("Peter", 45))

personSet.add(new PersonSortable("Dick", 55))

System.out.println(personSet)

}catch(Exception e){

System.out.println("发生异常:" + e.getMessage())

}

System.out.println("有序SET,PersonSortable实现了Comparable, 示例。")

try{

Set<PersonSortable>personSet = new TreeSet<PersonSortable>()

personSet.add(new PersonSortable("John", 15))

personSet.add(new PersonSortable("Smith", 25))

personSet.add(new PersonSortable("Lee", 35))

personSet.add(new PersonSortable("Peter", 45))

personSet.add(new PersonSortable("Dick", 55))

System.out.println(personSet)

}catch(Exception e){

System.out.println("发生异常:" + e.getMessage())

}

System.out.println("使用Collection.sort进行排序示例。")

try{

List<Person>personList = new ArrayList<Person>()

personList.add(new Person("John", 15))

personList.add(new PersonSortable("Smith", 25))

personList.add(new Person("Lee", 35))

personList.add(new PersonSortable("Peter", 45))

personList.add(new Person("Dick", 55))

Collections.sort(personList, new PersonComparator())

System.out.println(personList)

System.out.println("\t将排序后的结果封装回无序SET:")

Set<Person>personSetUnSortable = new HashSet<Person>()

for(Person person : personList){

personSetUnSortable.add(person)

}

System.out.println("\t" + personSetUnSortable)

System.out.println("\t将排序后的结果封装回有序SET:")

Set<Person>personSetSortable = new TreeSet<Person>()

for(Person person : personList){

personSetSortable.add(person)

}

System.out.println("\t" + personSetSortable)

}catch(Exception e){

System.out.println("发生异常:" + e.getMessage())

}

}

public static void main(String args[]){

new Question()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11957506.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存