欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA集合框架及其常用方法

发布时间:2025/3/17 编程问答 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 JAVA集合框架及其常用方法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

List接口和ArrayList类

/*** @author Administrator*学生类*/ public class Student {public String id;public String name;public Set courses;public Student(String id,String name){this.id=id;this.name=name;this.courses=new HashSet();}

-对象存入集合都变成Object类型,取出时需要进行类型转换。

/*** @author Administrator*备选课程类*/ public class ListTest {public List coursesToSelect;public ListTest(){this.coursesToSelect=new ArrayList();}public void testAdd(){//创建一个课程对象,并通过调用add方法,添加到List中Course cr1=new Course("1","数据结构");coursesToSelect.add(cr1);Course temp=(Course)coursesToSelect.get(0);System.out.println("添加了课程:"+temp.id+":"+temp.name);Course cr2=new Course("2","C语言");coursesToSelect.add(0, cr2);Course temp2=(Course)coursesToSelect.get(0);System.out.println("添加了课程:"+temp2.id+":"+temp2.name);//以下方法会抛出异常/* Course cr3=new Course("3","123");coursesToSelect.add(cr3);*/Course[]course={new Course("3","离散数学"),new Course("4","汇编语言")};coursesToSelect.addAll(Arrays.asList(course));Course temp3=(Course)coursesToSelect.get(2);Course temp4=(Course)coursesToSelect.get(3);//也可以设置插入位置如cr2,所以懒不写了System.out.println(temp2.id+temp2.name+temp3.id+temp3.name);Course []course2={new Course("5","高等数学"),new Course("6","大学英语")};}/*** 迭代器遍历,迭代器Iterator是用来遍历集合中元素的,不具备存储功能*/public void testIterator(){Iterator it=coursesToSelect.iterator();while(it.hasNext()){Course cr=(Course)it.next();System.out.println("课程(迭代):"+cr.id+":"+cr.name);}}/*** foreach遍历*/public void testForeach(){for(Object obj:coursesToSelect){Course cr=(Course)obj;}} }//当使用泛型时,foreach如下 for(Course cour:list){}

Arrays.asList(T a);//将某类型转化为List列表
set();//修改List中元素
remove()//删除List中元素

Set接口和Hashset类

Set是无序且不可重复的,当遍历时,只能选择使用foreach和地带器Iterator遍历

HashMap类

//修改映射public void testModify(){System.out.println("请输入要修改的学生id");Scanner console=new Scanner(System.in);while(true){String stuid=console.next();//从strudents中查找该学生id对应的学生对象Student st=students.get(stuid);if(st==null){System.out.println("该id不存在,请重新输入");continue;}System.out.println("当前该学生id,对应的学生为"+st.name);System.out.println("请输入学生的姓名");String name=console.next();Student newStudent=new Student(stuid,name);students.put(stuid, newStudent);System.out.println("修改成功");break;}} //测试Map的keySet方法public void testKeySet(){ //通过keyset方法,返回map中的所有键的set集合 Set<String>keySet=students.keySet(); //取得students容量System.out.println("有"+students.size()+"个" ); //遍历keyset,取得每一个键,再调用get方法取得每个键的valuefor(String stuID:keySet){Student st=students.get(stuID);if(st!=null){System.out.println("学生:"+st.name );}}} //删除学生 public void testRemove(){//获取从键盘输入的学生id Scanner console=new Scanner(System.in); while(true){//提示输入待删除学生的IDSystem.out.println("请输入要删除的学生ID");String id=console.next();//判断该id是否有对于的学生对象Student st=students.get(id);if(st==null){System.out.println("输入的id不存在");continue;}students.remove(id);System.out.println("成功删除学生"+st.name);break;} }

//检测序列是否包含某个对象用contains()方法,包含返回true,否则返回false
Course c=new Course();
boolean b=序列的引用.contains(c);

重写equals()方法

为什么要重写equals()方法?

因为有的时候我们判断两个对象是否相等,希望根据类的成员变量是否相等来判断,而不是两个引用对象是否指向同一个对象,因此这时候我们需要重写equals()方法。

如上述的contains()方法,当我们要判断一个对象是否存在集合中时,我们需要先判断集合中是否存在已知对象,其实是对集合进行遍历,然后每一个元素都调用equals()方法进行比较,这样就必须重写equals()。

@Override public boolean equal(obj){ if(this==obj)return true; if(obj==null)return false; if(!(obj instanceof Course))return false;//if(getClass!=obj.getClass())return false; Course course=(Course)obj; return this.name.equals(course.name);

快速重写hashCode()和

右键→source→generate hashCode()和equals()

转载于:https://www.cnblogs.com/leungjj/p/6428495.html

总结

以上是生活随笔为你收集整理的JAVA集合框架及其常用方法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。