Collection基本的子接口:

  • List:能够存放反复内容
  • Set:不能存放反复内容,全部反复的内容靠hashCode()和equals()两个方法区分
  • Queue:队列接口
  • SortedSet:能够对集合中的数据进行排序

List接口:

总结了List接口的扩展方法,即包括有增删改查方法.

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGVuZ2t2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">



List接口经常使用的子类:

ArrayList:能够直接通过对象的多态性为List接口实例化.
Vector:算是元老级的类,使用区别不大
LinkedList:表示链表的操作类,同一时候实现List接口和Queue接口

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGVuZ2t2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">



Set接口:

Set接口经常使用的子类:

HashSet:不能存放反复元素,并且採用散列的存储方式,所以没有顺序

TreeSet:不能存放反复元素,但对输入的数据进行有序排列(实现了SortedSet接口)


指定排序:
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) ;
}
};
执行结果:
[姓名:张三。年龄:30, 姓名:李四;年龄:31, 姓名:王五;年龄:32, 姓名:孙七;年龄:33, 姓名:赵六;年龄:33]

去反复元素:
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) ;
}
};

执行结果:

[姓名:李四;年龄:31, 姓名:张三;年龄:30, 姓名:孙七。年龄:33, 姓名:王五;年龄:32, 姓名:赵六;年龄:33]

Queue接口:

队列操作接口

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGVuZ2t2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">


SortedSet接口:


Collection子接口(List/Set/Queue/SortedSet)的更多相关文章

  1. Java基础-Collection子接口之Set接口

    Java基础-Collection子接口之Set接口 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 学习Collection接口时,记得Collection中可以存放重复元素,也可 ...

  2. Java基础-Collection子接口之List接口

    Java基础-Collection子接口之List接口 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们掌握了Collection接口的使用后,再来看看Collection接口中 ...

  3. java 数据结构(十):Collection子接口:Set接口

    1. 存储的数据特点:无序的.不可重复的元素具体的: 以HashSet为例说明:1. 无序性:不等于随机性.存储的数据在底层数组中并非照数组索引的顺序添加,而是根据数据的哈希值决定的.2. 不可重复性 ...

  4. java 数据结构(九):Collection子接口:List接口

    1. 存储的数据特点:存储序的.可重复的数据. 2. 常用方法:(记住)增:add(Object obj)删:remove(int index) / remove(Object obj)改:set(i ...

  5. Collection子接口:Set接口

    1.Set 存储的数据特点:无序的.不可重复的元素具体的:以HashSet为例说明: 1. 无序性:不等于随机性.存储的数据在底层数组中并非照数组索引的顺序添加,而是根据数据的哈希值决定的. 2. 不 ...

  6. Collection子接口:List接口

    1. 存储的数据特点:存储序的.可重复的数据. 2. 常用方法:(记住)增:add(Object obj)删:remove(int index) / remove(Object obj)改:set(i ...

  7. Collection接口的子接口——Set接口

    https://docs.oracle.com/javase/8/docs/api/java/util/Set.html public interface Set<E>  extends ...

  8. Collection接口的子接口——Queue接口

    https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html public interface Queue<E> exten ...

  9. 16、Collection接口及其子接口Set和List(常用类LinkedList,ArrayList,Vector和Stack)

    16.Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同 ...

随机推荐

  1. dotnet与各种数据库类型对应关系

    Data Provider Parameter format sqlclient @parametername oledb ?    mark odbc ?   mark oracleclient : ...

  2. display: -webkit-flex; 手机上图下文字,pad上有浮动。

    <article> <div class="boxt"> <div class="boxt-right"><img s ...

  3. 【USACO 2.3.2】奶牛家谱

    [题目描述] 农民约翰准备购买一群新奶牛.在这个新的奶牛群中,每一个母亲奶牛都生两小奶牛.这些奶牛间的关系可以用二叉树来表示.这些二叉树总共有N个节点(3 <= N < 200).这些二叉 ...

  4. 在ubuntu14.04上安装oracle java6 java7的方法

    注意: Debian建议安装openjdk,在release包中已包含. oracle的java需要自己安装,安装步骤如下: 1. 首先安装java-package,安装方法:apt-get inst ...

  5. chromedriver 与 chrome关联关系

    ----------ChromeDriver v2.22 (2016-06-06)---------- Supports Chrome v49-52 Resolved issue 1348: Time ...

  6. 02_ Windows与Linux双重引导

    1. Grub2引导window. ---------------------步骤1--------------------------------- vim /etc/grub.d/40_custo ...

  7. bootstrap导航条

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>我的 ...

  8. winform批量查询单号剔除重复

    //查询分单函数        private string GetQueryInSubbillNo()        {            string strSubbillNO = " ...

  9. 实用iPhone Cydia插件

      BadgeClear(角标清除):可以清除App推送所在图标右上角出现的红色角标.在桌面长按图标后,图标开始左右摇动,再双击图标即可清除点击的图标角标.   Bitesms(短信):收费插件,具有 ...

  10. HTML&CSS基础学习笔记1.28-给网页添加一个css样式

    CSS是什么? 当HTML配合CSS一起使用时,我们发现页面变得好看了很多.那么CSS到底是什么呢? CSS指层叠样式表 (Cascading Style Sheets),它主要是用于定义HTML标签 ...