第七章:Java_集合
2017-02-12 22:40
337 查看
集合概述
对象的存储:①数组(基本数据类型 & 引用数据类型) ②集合(引用数据类型)数组存储数据的弊端:长度一旦初始化以后,就不可变;
真正给数组元素赋值的个数没有现成的方法可用。
Collection接口
Collection接口 :
方法:
①add(Object obj),addAll(Collection coll),size(),clear(),isEmpty();
②remove(Object obj),removeAll(Collection coll),retainAll(Collection coll),equals(Object obj),contains(Object obj),containsAll(Collection coll),hashCode()
③ iterator(),toArray();
Collection方法测试:
public class TestCollection { @Test public void testCollection3() { Collection coll = new ArrayList(); coll.add(123); coll.add(new String("AA")); coll.add(new Date()); coll.add("BB"); coll.add(new Person("MM", 23)); Collection coll1 = new ArrayList(); coll1.add(123); coll1.add(new String("AA")); // 10.removeAll(Collection coll):从当前集合中删除包含在coll中的元素。 coll.removeAll(coll1); System.out.println(coll); //11.equals(Object obj):判断集合中的所有元素是否完全相同 Collection coll2 = new ArrayList(); coll2.add(123); coll2.add(new String("AA1")); System.out.println(coll1.equals(coll2)); //12.hashCode(): System.out.println(coll.hashCode()); System.out.println(); //13.toArray() :将集合转化为数组 Object[] obj = coll.toArray(); for(int i = 0;i < obj.length;i++){ System.out.println(obj[i]); } System.out.println(); //14.iterator():返回一个Iterator接口实现类的对象,进而实现集合的遍历! Iterator iterator = coll.iterator(); //方式一:不用 /*System.out.println(iterator.next()); System.out.println(iterator.next()); System.out.println(iterator.next());*/ //方式二:不用 // for(int i = 0;i < coll.size();i++){ // System.out.println(iterator.next()); // } //方式三:使用 while(iterator.hasNext()){ System.out.println(iterator.next()); } } @Test public void testCollection2() { Collection coll = new ArrayList(); coll.add(123); coll.add(new String("AA")); coll.add(new Date()); coll.add("BB"); // Person p = new Person("MM",23); coll.add(new Person("MM", 23)); System.out.println(coll); // 6.contains(Object obj):判断集合中是否包含指定的obj元素。如果包含,返回true,反之返回false // 判断的依据:根据元素所在的类的equals()方法进行判断 // 明确:如果存入集合中的元素是自定义类的对象。要求:自定义类要重写equals()方法! boolean b1 = coll.contains(123); b1 = coll.contains(new String("AA")); System.out.println(b1); boolean b2 = coll.contains(new Person("MM", 23)); System.out.println(b2); // 7.containsAll(Collection coll):判断当前集合中是否包含coll中所有的元素 Collection coll1 = new ArrayList(); coll1.add(123); coll1.add(new String("AA")); boolean b3 = coll.containsAll(coll1); System.out.println("#" + b3); coll1.add(456); // 8.retainAll(Collection coll):求当前集合与coll的共有的元素,返回给当前集合 coll.retainAll(coll1); System.out.println(coll); // 9.remove(Object obj):删除集合中的obj元素。若删除成功,返回true。否则,返回false boolean b4 = coll.remove("BB"); System.out.println(b4); } @Test public void testCollection1() { Collection coll = new ArrayList(); // 1.size():返回集合中元素的个数 System.out.println(coll.size()); // 2.add(Object obj):向集合中添加一个元素 coll.add(123); coll.add("AA"); coll.add(new Date()); coll.add("BB"); System.out.println(coll.size()); // 3.addAll(Collection coll):将形参coll中包含的所有元素添加到当前集合中 Collection coll1 = Arrays.asList(1, 2, 3); coll.addAll(coll1); System.out.println(coll.size()); // 查看集合元素 System.out.println(coll); // 4.isEmpty():判断集合是否为空 System.out.println(coll.isEmpty()); // 5.clear():清空集合元素 coll.clear(); System.out.println(coll.isEmpty()); } }
List接口:
存储有序的,可以重复的元素.—相当于“动态”数组新增的方法:
删除remove(int index) 修改set(int index,Object obj) 获取get(int index)插入add(int index,Object obj)
添加进List集合中的元素(或对象)所在的类一定要重写equals()方法
ArrayList(主要的实现类)
LinkedList(更适用于频繁的插入、删除操作)
Vector(古老的实现类、线程安全的,但效率要低于ArrayList)
Set接口:
存储无序的,不可重复的元素。—相当于高中的“集合”概念Set使用的方法基本上都是Collection接口下定义的。
添加进Set集合中的元素所在的类一定要重写equals() 和 hashCode()。要求重写equals() 和 hashCode()方法保持一致。
1.无序性:无序性!= 随机性。真正的无序性,指的是元素在底层存储的位置是无序的。
2.不可重复性:当向Set中添加进相同的元素的时候,后面的这个不能添加进去。
HashSet(主要的实现类)
LinkedHashSet(是HashSet的子类,当我们遍历集合元素时,是按照添加进去的顺序实现的;频繁的遍历,较少的添加、插入操作建议选择此)
TreeSet(可以按照添加进集合中的元素的指定属性进行排序)
要求TreeSet添加进的元素必须是同一个类的!
两种排序方式:
自然排序:
①要求添加进TreeSet中的元素所在的类implements Comparable接口
②重写compareTo(Object obj),在此方法内指明按照元素的哪个属性进行排序
③向TreeSet中添加元素即可。若不实现此接口,会报运行时异常
定制排序:
①创建一个实现Comparator接口的实现类的对象。在实现类中重写Comparator的compare(Object o1,Object o2)方法
②在此compare()方法中指明按照元素所在类的哪个属性进行排序
③将此实现Comparator接口的实现类的对象作为形参传递给TreeSet的构造器中
④向TreeSet中添加元素即可。若不实现此接口,会报运行时异常
要求重写的compareTo()或者compare()方法与equals()和hashCode()方法保持一致。
Map接口
存储“键-值”对的数据 —-相当于高中的“函数y = f(x)” (x1,y1) (x2,y2)
key是不可重复的,使用Set存放。value可以重复的,使用Collection来存放的。一个key-value对构成一个entry(Map.Entry),entry使用Set来存放。
添加、修改 put(Object key,Object value) 删除remove(Object key) 获取get(Object key) size() / keySet() values() entrySet()
HashMap:主要的实现类,可以添加null键,null值
LinkedHashMap:是HashMap的子类,可以按照添加进Map的顺序实现遍历
TreeMap:需要按照key所在类的指定属性进行排序。要求key是同一个类的对象。对key考虑使用自然排序 或 定制排序
Hashtable:是一个古老的实现类,线程安全的,不可以添加null键,null值不建议使用。
子类:Properties:常用来处理属性文件
附:Properties的使用
Properties pros = new Properties();
pros.load(new FileInputStream(new File(“jdbc.properties”)));
String user = pros.getProperty(“user”);
System.out.println(user);
String password = pros.getProperty(“password”);
System.out.println(password);
Iterator接口
用来遍历集合Collection元素迭代器测遍历测试:
public class TestIterator { //面试题: @Test public void testFor3(){ String[] str = new String[]{"AA","BB","DD"}; for(String s : str){ s = "MM";//此处的s是新定义的局部变量,其值的修改不会对str本身造成影响。 System.out.println(s); } for(int i = 0;i < str.length;i++){ System.out.println(str[i]); } } @Test public void testFor2(){ String[] str = new String[]{"AA","BB","DD"}; for(int i = 0;i < str.length;i++){ str[i] = i + ""; } for(int i = 0;i < str.length;i++){ System.out.println(str[i]); } } //*********************************************** //使用增强for循环实现数组的遍历 @Test public void testFor1(){ String[] str = new String[]{"AA","BB","DD"}; for(String s:str){ System.out.println(s); } } //使用增强for循环实现集合的遍历 @Test public void testFor(){ Collection coll = new ArrayList(); coll.add(123); coll.add(new String("AA")); coll.add(new Date()); coll.add("BB"); coll.add(new Person("MM", 23)); for(Object i:coll){ System.out.println(i); } } //错误的写法 @Test public void test2(){ Collection coll = new ArrayList(); coll.add(123); coll.add(new String("AA")); coll.add(new Date()); coll.add("BB"); coll.add(new Person("MM", 23)); Iterator i = coll.iterator(); while((i.next())!= null){ //java.util.NoSuchElementException System.out.println(i.next()); } } //正确的写法:使用迭代器Iterator实现集合的遍历 @Test public void test1(){ Collection coll = new ArrayList(); coll.add(123); coll.add(new String("AA")); coll.add(new Date()); coll.add("BB"); coll.add(new Person("MM", 23)); Iterator i = coll.iterator(); while(i.hasNext()){ System.out.println(i.next()); } } }
Collections工具类
操作Collection及Map的工具类,大部分为static的方法。相关文章推荐
- 疯狂JAVA讲义---第七章(下):集合Map
- 疯狂JAVA讲义---第七章(中):集合List,Queue(转)
- 疯狂JAVA讲义---第七章(上):集合set
- 疯狂JAVA讲义---第七章(下):集合Map(转)
- JAVA-第七章 集合(二)
- JAVA -第七章 集合(一)
- Java基础学习第七章之Java 集合
- JAVA OOP第七章 集合框架
- JAVA 第七章集合(五)
- 疯狂JAVA讲义---第七章(中):集合List,Queue
- JavaPersistenceWithHibernate第二版笔记-第七章-005排序的集合(@org.hibernate.annotations.SortComparator)
- Java使用实现面向对象编程:第七章集合框架的解读=>重中之重
- JAVA第七章-集合(三)
- JAVA面试题大集合
- JAVA面试题大集合
- Java中Collection Frame中常用集合简析(ArrayList,Vector,HashSet,TreeSet...)
- 第七章[多线程]第一节、Java中的多线程
- Java中Collection Frame中常用集合简析(ArrayList,Vector,HashSet,TreeSet)
- java network programming 第七章
- JAVA中的集合类