Collection子接口(List/Set/Queue/SortedSet)
Collection基本的子接口:
- List:能够存放反复内容
- Set:不能存放反复内容,全部反复的内容靠hashCode()和equals()两个方法区分
- Queue:队列接口
- SortedSet:能够对集合中的数据进行排序
List接口:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGVuZ2t2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
List接口经常使用的子类:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGVuZ2t2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
Set接口:
Set接口经常使用的子类:
HashSet:不能存放反复元素,并且採用散列的存储方式,所以没有顺序
class Person implements Comparable<Person>{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public String toString(){
return "姓名:" + this.name + "。年龄:" + this.age ;
}
public int compareTo(Person per){
if(this.age>per.age){
return 1 ;
}else if(this.age<per.age){
return -1 ;
}else{
return this.name.compareTo(per.name) ; // 调用String中的compareTo()方法
}
}
};
public class TreeSetDemo{
public static void main(String args[]){
Set<Person> allSet = new TreeSet<Person>() ;
allSet.add(new Person("张三",30)) ;
allSet.add(new Person("李四",31)) ;
allSet.add(new Person("王五",32)) ;
allSet.add(new Person("王五",32)) ;
allSet.add(new Person("王五",32)) ;
allSet.add(new Person("赵六",33)) ;
allSet.add(new Person("孙七",33)) ;
System.out.println(allSet) ;
}
};
class Person{
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){ // 覆写equals。完毕对象比較
if(this==obj){
return true ;
}
if(!(obj instanceof Person)){
return false ;
}
Person p = (Person)obj ; // 向下转型
if(this.name.equals(p.name)&&this.age==p.age){
return true ;
}else{
return false ;
}
}
public int hashCode(){
return this.name.hashCode() * this.age ; // 定义一个公式
}
public String toString(){
return "姓名:" + this.name + ";年龄:" + this.age ;
}
};
public class RepeatDemo{
public static void main(String args[]){
Set<Person> allSet = new HashSet<Person>() ;
allSet.add(new Person("张三",30)) ;
allSet.add(new Person("李四",31)) ;
allSet.add(new Person("王五",32)) ;
allSet.add(new Person("王五",32)) ;
allSet.add(new Person("王五",32)) ;
allSet.add(new Person("赵六",33)) ;
allSet.add(new Person("孙七",33)) ;
System.out.println(allSet) ;
}
};
执行结果:
Queue接口:
队列操作接口
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGVuZ2t2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
SortedSet接口:
Collection子接口(List/Set/Queue/SortedSet)的更多相关文章
- Java基础-Collection子接口之Set接口
Java基础-Collection子接口之Set接口 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 学习Collection接口时,记得Collection中可以存放重复元素,也可 ...
- Java基础-Collection子接口之List接口
Java基础-Collection子接口之List接口 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们掌握了Collection接口的使用后,再来看看Collection接口中 ...
- java 数据结构(十):Collection子接口:Set接口
1. 存储的数据特点:无序的.不可重复的元素具体的: 以HashSet为例说明:1. 无序性:不等于随机性.存储的数据在底层数组中并非照数组索引的顺序添加,而是根据数据的哈希值决定的.2. 不可重复性 ...
- java 数据结构(九):Collection子接口:List接口
1. 存储的数据特点:存储序的.可重复的数据. 2. 常用方法:(记住)增:add(Object obj)删:remove(int index) / remove(Object obj)改:set(i ...
- Collection子接口:Set接口
1.Set 存储的数据特点:无序的.不可重复的元素具体的:以HashSet为例说明: 1. 无序性:不等于随机性.存储的数据在底层数组中并非照数组索引的顺序添加,而是根据数据的哈希值决定的. 2. 不 ...
- Collection子接口:List接口
1. 存储的数据特点:存储序的.可重复的数据. 2. 常用方法:(记住)增:add(Object obj)删:remove(int index) / remove(Object obj)改:set(i ...
- Collection接口的子接口——Set接口
https://docs.oracle.com/javase/8/docs/api/java/util/Set.html public interface Set<E> extends ...
- Collection接口的子接口——Queue接口
https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html public interface Queue<E> exten ...
- 16、Collection接口及其子接口Set和List(常用类LinkedList,ArrayList,Vector和Stack)
16.Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同 ...
随机推荐
- asp.net操作cookie类
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary ...
- JavaScript 标识符
JavaScript 标识符 和其他任何编程语言一样,JavaScript 保留了一些标识符为自己所用. JavaScript 同样保留了一些关键字,这些关键字在当前的语言版本中并没有使用,但在以后 ...
- 【转】lua random()
先来看看这两段代码: ① math.randomseed(os.time())for i=1,10 do n = math.random(10) print(n) 运行结果是: 63210754341 ...
- 虾米loop 摇头代码
.head{ position: absolute; -webkit-animation: shake 1s infinite; -webkit-transform-origin: center bo ...
- MYSQL主从不同步延迟原理
1. MySQL数据库主从同步延迟原理. 要说延时原理,得从mysql的数据库主从复制原理说起,mysql的主从复制都是单线程的操作, 主库对所有DDL和DML产生binlog,binlog是 ...
- PAT - IO - 螺旋方阵
所谓“螺旋方阵”,是指对任意给定的N,将1到N*N的数字从左上角第1个格子开始,按顺时针螺旋方向顺序填入NxN的方阵里.本题要求构造这样的螺旋方阵. 输入格式: 输入在一行中给出一个正整数N(< ...
- javascript 向上滚动
<html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Co ...
- 如何编写pythonGNURADIO应用
三个基本概念: flow graph 很大一部分GNURADIO程序就是一个流图.流图包括若干信号处理模块(BLock),以及一些用表示数据流的边. BLock 所有信号处理过程在block中进行.在 ...
- UVA - 1614 Hell on the Market(贪心)
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Descript ...
- 实验五:分析system_call中断处理过程
原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 如果我写的不好或者有误的地方请留言 ...