集合
-
集合提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变。
-
集合类体系结构:collection 是单列集合(List是内容可重复 set是内容不可重复) Map是双列集合
在List中还有(ArrayList LinkedList 等) 在Set中有(HashSet TreeSet等) Map中有(HashMap等)
set集合没有带索引的方法,不能使用普通for循环遍历(HashSet对集合顺序不做任何保证)
public class SetDemo{ public static void main(String[] args){ Set<String> Set = new HashSet<String>(); set.add("hello"); set.add("world"); Iterator<String> s = Set.Iterator(); while(Set.hasNext()){ String i = s.hasNext(); System.out.println(i); } } }
-
Collection集合是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素;JDK不提供此接口的任何直接实现,它提供更具体地子接口(如set和List)实现。 创建Collection集合对象,采用多态的方式,具体的实现类ArrayList。
//创建Collection集合对象采用多态的方式Array List() import java.util.ArrayList; import java.util.Collection; public class CollectiongDemo{ public static void main(String[] args){ //创建Collection集合的对象 collection<String> c = new ArrayList<String>(); //添加元素:boolean add (E e) c.add("hello"); c.add("world"); c.add("java"); //输出集合对象 System.out.println(c); } } //结果:[hello,world,java] 说明在ArrayList集合中重写了toString方法
//Collection集合常用方法
方法名 | 说明 |
---|---|
boolean add(E e) | 添加元素 |
boolean remove(Object o) | 从集合中移除指定的元素 |
void clear() | 清空集合中的元素 |
boolean contains(Object o) | 判断集合中是否存在指定的元素 |
boolean isEmpty() | 判断集合是否为空 |
int size() | 集合的长度,也就是集合中元素的个数 |
//Collection 集合的遍历 : Iterator:迭代器,集合的专用遍历格式。Iterator<E>iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到;迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的
Iterator中的常用方法:E next():返回迭代中的下一个元素 boolean hasNext():如果迭代具有更多元素,则返回true
Public class IteratorDemo{ public static void main(String[] args){ Collection<String> c = new ArrayList<String>(); //添加元素 c.add("hello"); c.add("world"); c.add("java"); //Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到 Iterator<String> it = c.iterator(); //E next():返回迭代中的下一个元素 System.out.println(it.next()); System.out.println(it.next()); System.out.println(it.next()); //boolean hasNext():如果迭代具有更多元素,则返回true if(it.hasNext()){ System.out.println(it.next()); } if(it.hasNext()){ System.out.println(it.next()); } if(it.hasNext()){ System.out.println(it.next()); } if(it.hasNext()){ System.out.println(it.next());//因为共三个元素,第四个返回false,不进入循环不会报错 } //用while循环改进判断 while(it.hasNext()){ // System.out.println(it.next()); String s = it.next(); System.out.println(s); } } }
//案例:Collection集合存储学生对象并遍历
//定义学生类,创建Collection集合对象,把学生添加到集合,遍历集合(迭代器方式) public class Student{ private String name; private int age; public Student(){ } public Student(String name,int age){ this.name = name; this.age = age; } 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 class CollectionDemo{ public static void main(String[] args){ //创建Collection集合对象 Collection<student> c = new ArrayList<Student>(); //创建学生对象 Student s1 = new Student("令狐冲", 24); Student s1 = new Student("岳不群", 44); Student s1 = new Student("左冷禅", 47); //把学生添加到集合 c.add(s1); c.add(s2); c.add(s3); //遍历集合(迭代器方式) Iterator<Student> it = c.iterator(); while(it.hasNext()){ Student s = it.next(); System.out.println(s.getName() + "," + s.getAge()); } } }
-
List是有序集合也称为序列,用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引访问元素,并搜索列表中的元素。列表中通常允许重复元素。List集合存储和取出的元素顺序一致;存储的元素可以重复。
public class ListDemo{ public static void main(String[] args){ //创建集合对象 List<String> list = new ArrayList<String>(); //添加元素 list.add("hello"); list.add("world"); list.add("java"); list.add("world"); //遍历 Iterator<String> it = list.iterator(); while (it.hasNext()){ String s = it.next(); System.out.println(s); } } }
//List集合特有方法
方法名 | 说明 |
---|---|
void add(int index , E element) | 在此集合中的指定位置插入指定的元素 |
E remove (int index) | 删除指定索引处的元素,返回被删除的元素 |
E set (int index, E element) | 修改指定索引处的元素,返回被修改的元素 |
E get (int index) | 返回指定索引处的元素 |
improt java.util.ArrayList; import java.util.List; public class ListDemo{ public static void main(String[] args){ //创建集合对象 List<String> list = new ArrayList<String>(); //添加元素 list.add("hello"); list.add("world"); list.add("java"); //在此集合的指定位置插入指定元素 list.add(1,"javasee"); //list.add(11,"javasee");//IndexOUtOfBoundsException //删除指定索引处的元素,返回被删除的元素 System.out.println(list.remove(1)); System.out.println(list.remove(11));//IndexOUtOfBoundsException //修改指定索引处的元素,返回被修改的元素 System.out.println(list.set(1,"javasee")); //返回指定索引处的元素 System.out.println(list.get(1)); //输出集合对象 System.out.println(list); //遍历集合,for循环改进 for(int i=1; i<list.size(); i++){ String s = list.get(i); System.out.println(s); } } }
//List集合常用子类:ArrayList(底层数据结构是数组,查询快,增删慢)LinkedList:(底层数据结构是链表,查询慢,增删快)
public static void main(String[] args){ ArrayList<String> array = new ArrayList<String>(); array.add("hello"); array.add("world"); array.add("java"); //增强for循环遍历 for(String s : array){ System.out.println(s); } }
LinkedList集合的特有功能
方法名 | 说明 |
---|---|
public void addFirst(E e) | 在该列表开头插入指定的元素 |
public void addLast(E e) | 将指定的元素追加到此列表的末尾 |
public E getFirst() | 返回此列表中的第一个元素 |
public E getLast() | 返回此列表中的最后一个元素 |
public E removeFirst() | 从此列表中删除并返回第一个元素 |
public E removeLast() | 从此列表中删除并返回最后一个元素 |
-
哈希值是JDK根据对象的地址或者字符串或者数字算出来的int类型的数值。Object类中的public int hashCode();方法可以返回对象 额哈希码值。同一个对象多次调用hashCode()方法返回的哈希值是相同的;默认情况下,不同对象的哈希值是不同的。而重写hashCode()方法,可实现让不同对象的哈希值相同。
publci class HashSetDemo{ public static void main(String[] args){ //创建集合对象 HashSet<String> h = new HashSet<String>(); //添加元素 h.add("hello"); h.add("world"); h.add("java"); h.add("world");//不包含重复元素 //遍历 for(String s: h){ System.out.println(s) } } } //world java hello
//LinkedHashSet集合是哈希表和链表实现的Set接口,具有可预测的迭代次序;由链表保证元素有序,元素存储和取出顺序一致;由哈希表保证元素唯一。
publci class LinkedHashSetDemo{ public static void main(String[] args){ //创建集合对象 LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(); //添加元素 linkedHashSet.add("hello"); linkedHashSet.add("world"); linkedHashSet.add("java"); linkedHashSet.add("world");//不包含重复元素 //遍历 for(String s: linkedHashSet){ System.out.println(s) } } } // hello world java
//TreeSet集合,元素有序(不是指存储和取出顺序,而是按照规则排序,排序方法却决于构造方法),TreeSet():根据其元素的自然排序进行排序;TreeSet( Comparator comparator) :根据指定的比较器进行排序;没有带索引的方法,不能使用普通for循环遍历(增强for循环和Iterator迭代器可以);由于是Set集合,所以不包含重复元素的集合。
public class TreeSetDemo{ public static void main(String[] args){ //创建集合对象 TreeSet<Integer> ts = new TreeSet<Integer>();//因为是集合,所以存储类型只能是引用类型(不能直接写int),所以在存储类型中写int的包装类型Integer ts.add(1); ts.add(4); ts.add(3); ts.add(2); ts.add(5); //遍历集合 for(Integer i : ts){ System.out.println(i); } } } //结果:1 2 3 4 5 (自然排序)
//用TreeSet集合存储自定义对象,无参构造方法使用自然排序;自然排序是让元素所属的类实现comparable接口,重写compareTo(To)方法;重写方法时一定按照排序规则要求的主要和次要条件来写。
public class Student implements Comparable<Student>{ private String name; private int age; public Student(){} public Student(String name, int age){ this.name = name; this.age = age; } public void setName(String name){this.name = name; } public String getName(){ return name; } public void setAge(int age){this.age = age;} public int getAge(){return age;} @Override public int compareTo(Student s){ return 0;//返回值为0,会认为s2和s1(即后面的对象和前面的对象)是相同元素,因此后面元素添加失败,只有一个元素 return 1;//返回值为正数1,则s2与s1比较(返回正数)为s2>s1,结果会输出与输入相同顺序,升序存储 return -1;//返回值为负数-1,则s2与s1比较(返回负数)为s2<s1,结果会输出与输入相反顺序,降序存储 //按照年龄从小到大排序 int num = this.age - s.age;//this相当于后一个元素,即s2 - s1 //当年龄相同时,无法添加成功;因此还需要比较姓名 int num2 = num==0?this.name.compareTo(s.name):num; return num; } } public class TreeSetDemo{ public static void main(String[] args){ TreeSet<Student> ts = new TreeSet<Student>(); Student s1 = new Student("linhuchong",24); Student s2 = new Student("yangguo",22); Student s3 = new Student("lixunhuan",27); Student s4 = new Student("zhangwuji",30); Student s5 = new Student("liyapeng",30) ts.add(s1); ts.add(s2); ts.add(s3); ts.add(s4); ts.add(s5); for(Student s : ts){ System.out.println(s.getName() + "," + s.get.Age()); } } }