「setjava排序」set排序方法

博主:adminadmin 2023-01-08 09:48:10 768

今天给各位分享setjava排序的知识,其中也会对set排序方法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java set 顺序

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

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

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

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

扩展资料

SetString set = new TreeSetString();    

set.add("f");

set.add("a");

set.add("b");

set.add("c");

set.add("d");

set.add("e");        

System.out.println(set);

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

java里面的集合框架的set用法应该怎么做

1、Set:它是无序、不重复、该接口中的方法和Collection接口中的方法一致。

继承它的子类有HashSet和TreeSet。

HashSet:底层是哈希表数据结构,不同步的它保证元素的唯一性的方式。

根据元素的两个方法来完成的,一个是HashCode、一个是equals方法,只有当hashCode方法算出哈希值相同时,会再次判断两个元素的equals方法是否为true,如果是true说明两个元素相同,不存储,所以往hashSet集合中存储自定义对象时,要覆盖hashCode,equals方法,通过自定义对象具备的特有数据定义hashCode、equals的具体实现。

treeSet:用于给集合中的额元素按照指定的顺序进行排序,底层是二叉树结构,线程是不同步的。

它保证元素的唯一性,就是通过元素对象的比较方法返回值来确定的,如果为0,则视为两个元素时相同的元素,不存储。

treeset的两种排序方式:

1、让元素自身具备比较功能,就是强制让元素类去实现comparable接口,覆盖compareTo方法,这时元素具备的自然排序,可是如果元素自身不具备比较功能,获取具备的比较功能不是所需要的,这时排序方式就不能用了。

2、让集合自身具备比较功能,需要定义比较器,其实就实现了comparetor接口的子类对象作为参数传递给treeSet集合的构造函数,让treeSet集合一创建就具备了比较功能,该子类必须要覆盖compare方法。

java中set排序怎么实现

set没有排序的方法,

可以Set

set

=

new

TreeSet()

TreeSet存储的时候已经是排好序的

set排序比较的是什么?java

// 如果使用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 ComparablePerson{

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 ComparatorPerson{

@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{

SetPerson personSet = new HashSetPerson();

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{

SetPerson personSet = new TreeSetPerson();

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{

SetPersonSortable personSet = new HashSetPersonSortable();

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{

SetPersonSortable personSet = new TreeSetPersonSortable();

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{

ListPerson personList = new ArrayListPerson();

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:");

SetPerson personSetUnSortable = new HashSetPerson();

for(Person person : personList){

personSetUnSortable.add(person);

}

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

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

SetPerson personSetSortable = new TreeSetPerson();

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();

}

}

setjava排序的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于set排序方法、setjava排序的信息别忘了在本站进行查找喔。