javaSE21/9/22
2021/9/22 22:40:42
本文主要是介绍javaSE21/9/22,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
TreeSet
- 基于HashCode计算元素存放位置
- 基于排列顺序实现元素不=重复
public class Person implements Comparable<Person>{//必须先继承 private String name ; private int age,phone; @Override public int compareTo(Person o) {//重写compareTo方法 int n = this.getName().compareTo(o.getName()); int n1= this.age-o.getAge(); return n==0?n1:n; } public Person() { } public Person(String name, int age, int phone) { this.name = name; this.age = age; this.phone = phone; } @Override public String toString() { return name+":"+"年龄"+age+"电话"+phone; } @Override public int hashCode() { int n1 = this.name.hashCode(); int n2 = age; int n3 = phone; return n1 + n2 + n3; } @Override public boolean equals(Object obj) { if (this==obj) return true; if (obj==null) return false; if (obj instanceof Person){ Person p = (Person) obj; if (this.name.equals(p.getName())&&this.age==p.getAge()&&this.phone==p.phone){ return true; } } return false; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getPhone() { return phone; } public void setPhone(int phone) { this.phone = phone; } }
/** * TreeSet中的元素必须实现Comparable接口 * 否则程序无法判断将数据放在哪儿 * compareTo方法返回值为0,认为是元素重复 */ public class Test { public static void main(String[] args){ TreeSet<Person> treeSet = new TreeSet<>(); Person p1 = new Person("莉莉",19,190); Person p2 = new Person("小明",20,110); Person p3 = new Person("小刚",19,180); treeSet.add(p1); treeSet.add(p2); treeSet.add(p3); System.out.println(treeSet.size()); System.out.println(treeSet.toString()); } }
Comparator比较器
- 可实现定制比较
- 有比较器就不用实现Comparable接口
public class Test { public static void main(String[] args){ TreeSet<Person> persons = new TreeSet<>(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { int n1 = o1.getAge()-o2.getAge(); int n2 = o1.getName().compareTo(o2.getName()); return n1==0?n2:n1; } }); Person p1 = new Person("莉莉",19,190); Person p2 = new Person("小明",20,110); Person p3 = new Person("小刚",19,180); persons.add(p1); persons.add(p2); persons.add(p3); System.out.println(persons.size()); System.out.println(persons.toString()); } }
用Comparator接口实现字符串按照长度排序
public class Test { public static void main(String[] args) { TreeSet<String> str = new TreeSet<String>(new Comparator<String>() { @Override public int compare(String o1, String o2) { int n1 = o1.length()-o2.length(); int n2 = o1.compareTo(o2); return n1==0?n2:n1; } }); str.add("xyz"); str.add("abcde"); str.add("abc"); str.add("f"); System.out.println(str.size()); System.out.println(str); } }
Map集合
Map接口
- 特点:存储一对数据(Key—Value),无序,无下标,键不可重复,值可以重复
这篇关于javaSE21/9/22的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-28MQ底层原理资料详解:新手入门教程
- 2024-11-28MQ项目开发资料详解:新手入门教程
- 2024-11-28MQ项目开发资料详解:入门与初级用户指南
- 2024-11-28MQ消息队列资料入门教程
- 2024-11-28MQ消息队列资料:新手入门详解
- 2024-11-28MQ消息中间件资料详解与应用教程
- 2024-11-28MQ消息中间件资料入门教程
- 2024-11-28MQ源码资料详解与入门教程
- 2024-11-28MQ源码资料入门教程
- 2024-11-28RocketMQ底层原理资料详解