zoukankan      html  css  js  c++  java
  • Collection子接口(List/Set/Queue/SortedSet)

    Collection基本的子接口:

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


    List接口:

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




    List接口经常使用的子类:

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




    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接口:

    队列操作接口


    SortedSet接口:


  • 相关阅读:
    Parameter Binding in ASP.NET Web API
    Which HTTP methods match up to which CRUD methods?
    ErrorHandling in asp.net web api
    HttpStatusCode
    Autofac Getting Started(默认的构造函数注入)
    Autofac Controlling Scope and Lifetime
    luvit 被忽视的lua 高性能框架(仿nodejs)
    undefined与null的区别
    VsCode中使用Emmet神器快速编写HTML代码
    字符串匹配---KMP算法
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5267064.html
Copyright © 2011-2022 走看看