Set集合和List集合

来源:互联网 发布:httpclient js 编辑:程序博客网 时间:2024/06/03 07:44
List:有序可重复
         ArrayList: 写入数据时,数据在尾部的时候快,取数快

         LinkedList:  集合中间部分添加值和删除值得时候快

Set:不包含重复元素,但是无序,
           1、一个不包含重复元素(值)的 collection,
                   set在每次添加元素(值)的时候,都会把前面的元素和新增的元素进行比               较,如果是true,重复,丢弃;false:添加到集合当中。
    Set集合当中是没有重复数据的.
            1.1 HashSet:实现了Set接口,由哈希表支持,它不保证 set 的迭代顺序;特别       是它不保证该顺序恒久不变。此类允许使用 null 元素。此类事不同步的, 
HashSet执行add 方法添加元素
       1)执行set的add方法添加元素

           import java.util.Set;
           import java.util.HashSet;
     public class Demo{
    public static void main(String [] args){
Set set = new HashSet();
    // add String
set.add("xiaoming");
set.add("xiaohua");  
set.add("xiaogang");
set.add("xiaohua");

System.out.println("xiaoming".hashCode());
System.out.println("xiaohua".hashCode());
System.out.println("xiaogang".hashCode());
System.out.println("xiaohua".hashCode());
System.out.println("*****");
System.out.println(set);
}
}

set集合取数方式:
 迭代器:
 foreach循环:(加强版for循环,遍历(从第一个位置到最后一个位置))
语法:
for(每一次遍历接收的对象声明: 需要被遍历的集合或数

代码块;
}


TreeSet: 有序(大小排序)
是根据二叉树的结构进行自然顺序排序;
排序规则定义有:
自定义规则 :自己独立写一个排序规则,Comparator
本身需要排序的对象,实现接口,完成排序规则; Comparable

import java.util.*;
public class Demo2{
public static void main(String [] agrs ){
Set<Student> set = new HashSet<Student>();
//add  Student
Student s = new Student("xiaogang",18);
Student s1 = new Student("xiaohua",19);
Student s2 = new Student("xiaodu",18);
Student s3 = new Student("xiaohua",19);

set.add(s); // 1已经有值
set.add(s1);// 1.调用equals方法   true 丢弃  false
set.add(s2);
set.add(s3);
set.remove(s);//移除
for(Student ss: set){
System.out.println(ss);
}
}
}

class Student{
private String name;
private Integer age;
public Student(String name,Integer age){
this.name = name;
this.age = age;
}
// 获取你的哈希值,,,,
public int hashCode() {
       //名字相同
        //return 1;
        return name.hashCode() + age.hashCode();
    }
    //Object anObject 表示新加入的对象    this 表示要和他比较的对象
public boolean equals(Object anObject) {
       //把对象强制转换为Student
        Student s = (Student)anObject;
       //比较两个对象 是不是同一个对象
       if(this == s){
        return true;
        }
       //判断是不是同一个类型
       if(!(anObject instanceof Student)){
        return false;
        }
        //判断两个的年龄是否相等
        if(this.age == s.age){
        return true;//true 表示重复
        }
       return false;


    }
//重写父类的  hashCode方法, equals方法
   }


利用treeSet来自定义排序;

import java.util.*;


public class DemoTreeSet{
public static void main(String [] args){

Set <Student>set = new TreeSet<Student>(new MyComparator());

set.add(new Student("xiaohua",18));
set.add(new Student("xdfgdf",17));
set.add(new Student("fhfd",19));
set.add(new Student("sdfger",11));



for(Student s : set){
System.out.println(s);
}

}
}


/**
   自定义排序规则


class MyComparator implements  Comparator{

public int compare(Object o1,Object o2){

return 1;
}
}
*/


/**


 实现Comparable类,重写compareTo 方法,然后进行排序  
**/
class Student implements Comparable{
String name;
Integer age;

public Student(String name,Integer age){
this.name = name;
this.age = age;
}

public String toString(){

return "name = "+ name +";age = "+age;
}

public int compareTo(Object o){
 //把后面对象的值强制转换
Student s = (Student)o;
//把年龄 再一次进行排序
return this.age.compareTo(s.age);
}

}