首页 > 编程知识 正文

java 开发框架,java集合框架结构图

时间:2023-05-05 07:48:09 阅读:58253 作者:4205

Java收藏框架(公共类) JCF

为了实现某个目的或功能,设计了一个接口,该接口具有一系列预封装的继承关系或实现关系类;

收藏来源:

特点:要素类型可以不同,集合长度可变,空间不固定;

管理集合类和接口list、set、map 3类别

collection :所有集合中心的接口(装行李中心) )。

collections :操作集合的算法类

collection (三大阵营) :

列表:阵列列表,链接列表,向量(legacy ) )面试,

Set:HashSet,

Map:HashMap、Hashtahle、properties、

collection和迭代器接口:

集合的框架有两种类型的API :

程序示例:

1:List容器

publicstaticvoidmain (字符串[ ] args )//todo auto-generatedmethodstub//list-- -列表。 //List的特点:线性。 即有序,要素投入顺序与要素存储顺序一致。 //表象上,List的最大特点是有下标。 //ArrayList表现为数组的封装,底层在数组//LinkedList的底层封装了双向链表。 //需要大量查询动作时,使用ArrayList; //如果需要大量添加或删除,特别是需要在中间添加或删除,则在使用链接列表//的面试中,ArrayList经常与Vector进行比较//泛型---增加linkedliststudentbeanlst=newlinkedliling//lst.add (new student bean ) ' zhadd,以控制集合只能处理一个数据类型LST.add(newstudentbean ) ' Zhao4',32,76 ); lst.add (新事件bean ) ' lidaye ',55,62 ); lst.add (新事件bean ) ' Zhoudama ',60,84 ); //求出长度的int size=lst.size (; 修改//lst.set(0,newstudentbean ) ' 张3 feng ',102,45 ) ); //lst.remove(0 (删除0; //获取某个元素//StudentBean stu=(StudentBean ) lst.get(1) sudentbeanstu=lst.get(1); //增加通用性不需要强旋转//遍历- -依次取出集合中的各要素,进行相同的操作//1,使用普通的for循环进行遍历for(intI=0; i lst.size (; I ) studentbeantmpstu=lst.get(I; system.out.println (tmpstu.getname ); (//2,迭代器---使用迭代器完成遍历----集合框架类Collection直接分支专用) /没有下标,从头到尾iteratorstudentbeanit=lst while(it.Hasnext () ) { StudentBean tmpStu=it.next; system.out.println (tmpstu.getname ); (/3,for-each环路)迭代器是基本的,但语法更简单,数组for(studentbeantmpstu:lst ) system.out.println ) tmpstu.getname for (int tmp :阵列) tmp; s

ystem.out.println(tmp); } } public static void main(String[] args) { // TODO Auto-generated method stub //Map---映射 //Map的特点:键值对。键要求唯一,值可以重复 // 放入元素的顺序和存储顺序无关 //常用子类:HashMap(主要用于集合操作)、Properteis(专用于操作属性文件) //面试中:HashMap常常和Hashtable进行比较 HashMap<String, StudentBean> map = new HashMap<String, StudentBean>(); //增加元素 map.put("j34001", new StudentBean("zhang3",18,80)); map.put("j34002", new StudentBean("li4",28,85)); map.put("j34003", new StudentBean("wang5",18,87)); //得到长度 System.out.println(map.size()); //修改元素 map.put("j34003", new StudentBean("zhao6",24,75)); //删除元素---通过键去移除元素 map.remove("j34003"); //获取指定的元素对象 StudentBean stu = map.get("j34003"); //遍历Map //不能同时遍历键和值;只能分开遍历 //遍历所有的键 Set<String> keySet = map.keySet();//得到所有的键,装入一个Set集合中,返回给调用者 for(String key : keySet){ System.out.println(key); } //遍历所有的值 Collection<StudentBean> allStus = map.values();//得到所有的值,思考为什么不返回List或其它集合类型? for(StudentBean tmpStu : allStus){ System.out.println(tmpStu.getName()); } } public static void main(String[] args) { //Set---集 //Set的特点---不能放置重复元素、无序。 // 表象上,Set没有下标 //常用子类HashSet //HashSet如何判断两个元素是否重复呢? //1、调用equals方法得到两个对象比较为true //2、两个元素的hashcode值保持一致 //只有这两个条件同时满足,Java才认为这是同一个对象 //所以重写了equals方法一般也要重写hashcode方法,让equals返回true的对象 //hashcode也能返回同样的整数值 HashSet set = new HashSet(); //放入元素 set.add("hello"); set.add(new Date()); StudentBean stu0 = new StudentBean("zhang3",18,80); set.add(stu0); set.add(200); StudentBean stu1 = new StudentBean("zhang3",18,80); set.add(stu1); //放入元素的个数 System.out.println(set.size()); System.out.println(stu0.equals(stu1)); System.out.println(stu0.hashCode()); System.out.println(stu1.hashCode()); //修改---没有修改方法 //删除---只能根据对象进行删除,还是用的equals和hashCode来判断到底删除哪个对象 set.remove(new StudentBean("zhang3",18,80)); //获取某个元素---无法获取某个指定的元素 //遍历---将集合中的每个元素依次取出,做同样的操作 //1、不支持普通for循环,没有下标 //2、支持迭代器 Iterator it = set.iterator(); while(it.hasNext()){ Object obj = it.next(); System.out.println(obj); } //3、支持for-each循环 for(Object obj : set){ System.out.println(obj); } } public class StudentBean implements Comparable<StudentBean>{ private String name; private int age; private int score; public StudentBean(){ } public StudentBean(String name, int age, int score) { super(); this.name = name; this.age = age; this.score = score; } 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 int getScore() { return score; } public void setScore(int score) { this.score = score; } @Override public boolean equals(Object obj) { // TODO Auto-generated method stub if(obj instanceof StudentBean){ StudentBean stu = (StudentBean)obj; if(this.name.equals(stu.getName()) && this.age == stu.getAge() && this.score == stu.getScore()){ return true; } } return false; } @Override public int hashCode() { // TODO Auto-generated method stub int num = 0; if(this.name != null){ char[] array = this.name.toCharArray(); for(int a : array){ num += a; } } num += this.age; num += this.score; return num; } @Override public int compareTo(StudentBean stu) { // TODO Auto-generated method stub //返回的正数、负数,依赖于根据比较规则两个元素的位置之差// if(this.age < stu.getAge()){// return -1;// }else if(this.age > stu.getAge()){// return 1; // }else{// return 0;// } if(this.score > stu.getScore()){ return -1; }else if(this.score < stu.getScore()){ return 1; }else{ if(this.age > stu.getAge()){ return 1; }else if(this.age < stu.getAge()){ return -1; }else{ return 0; } } } @Override public String toString() { // TODO Auto-generated method stub return this.name + " " + this.age + " " + this.score; }} public class IntegerComparator implements Comparator<Integer>{ @Override public int compare(Integer o1, Integer o2) { // TODO Auto-generated method stub if(o1 > o2){ return -1; }else if(o1 < o2){ return 1; } return 0; }} public class StudentComparator implements Comparator<StudentBean>{ @Override public int compare(StudentBean o1, StudentBean o2) { // TODO Auto-generated method stub if(o1.getScore() > o2.getScore()){ return -1; }else if(o1.getScore() < o2.getScore()){ return 1; } return 0; }}

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。