为了账号安全,请及时绑定邮箱和手机立即绑定

关于List Map Set总结

标签:
Java
package com.imooc.collection;
/**
 * 课程类
 * @author Okawari
 *
 */
public class Course {
    public String id;
    public String name;
    public Course(String id,String name)
    {
        this.id = id;
        this.name = name;
    }
    public Course()
    {

    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Course))
            return false;
        Course other = (Course) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

//  public boolean equals(Object obj) {
//      if(this==obj)
//      {
//          return true;
//      }
//      if(obj==null)
//      {
//          return false;
//      }
//      if(!(obj instanceof Course))
//      {
//          return false;
//      }
//      Course course = (Course)obj;
//      if(this.name==null)
//      {
//          if(course.name==null)
//          {
//              return true;
//          }
//          else{
//              return false;
//          }
//      }else
//      {
//          if(this.name.equals(course.name))
//          {
//              return true;
//          }
//          else
//          {
//              return false;
//          }
//      }
//  }
}
package com.imooc.collection;

import java.util.HashSet;
import java.util.Set;

/**
 * 学生类
 * @author Okawari
 *
 */
public class Student {
    public String id;
    public String name;
    public Set <Course>courses;
    public Student(String id,String name)
    {
        this.id = id;
        this.name = name;
        this.courses = new HashSet<Course>();
    }
}
package com.imooc.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class SetText {
    public List<Course> coursesToSelect;
    public Student student;
    public SetText()
    {
        coursesToSelect = new ArrayList<Course>();
    }
    public void textAdd()
    {
        //创建一个课程对象,通过调用List方法,添加到备选课程List当中
        Course cr1 = new Course("1","数据结构");
        coursesToSelect.add(cr1);
        Course temp = (Course)coursesToSelect.get(0);
        System.out.println("添加了课程:"+temp.id+","+temp.name);
        Course[]course = {new Course("3","离散数学"),new Course("4","高等数学")};
        coursesToSelect.addAll(Arrays.asList(course));
        Course temp2 = (Course)coursesToSelect.get(1);
        Course temp3 = (Course)coursesToSelect.get(2);
        System.out.println("添加了两门课程:"+temp2.id+","+temp3.name+'\n'+temp3.id+","+temp3.name);
    }
    public void textIterator()
    {
        Iterator it = coursesToSelect.iterator();
        System.out.println("有如以下课程待选(通过迭代器访问):");
        while(it.hasNext())
        {
            Course cr = (Course)it.next();
            System.out.println("课程:"+cr.id+","+cr.name);
        }
        String names[ ][ ]={{"tom","jack","mike"},{"zhangsan","lisi","wangwu"}}; 
    }
    public void modify()
    {
        coursesToSelect.set(2,new Course("4","体育"));
    }
    public void textmove()
    {
        Course ct = (Course) coursesToSelect.get(1);
        System.out.println("即将删除的课程为:"+ct.id+","+ct.name);
        coursesToSelect.remove(ct);

    }
    public void textcontains(){
        Course course = coursesToSelect.get(0);
        System.out.println("取得课程:"+course.name);
        System.out.println("备选课程中是否包含"+course.name+","+coursesToSelect.contains(course));
        Course course2 = new Course(course.id,course.name);
        System.out.println("取得课程"+course2.name);
        System.out.println("备选课程中是否包含"+course2.name+","+coursesToSelect.contains(course2));
    }
    public void CreatestudentandSelectcourse()
    {
        Student a = new Student("1","okawari");
        System.out.println("welcome you:" +a.name+" please choise your course!");
        Scanner input = new Scanner(System.in);
        for(int i = 0;i<3;i++)
        {
            System.out.println("please input your coursenumber!");
            String id = input.next();
            for (Course cr : coursesToSelect) {
                if(cr.id.equals(input))
                {
                    a.courses.add(cr);
                }
            }
        }
        for (Course cr:coursesToSelect) {
            System.out.println("have already choise the course:"+cr.id+","+cr.name);
        }
    }
    public void textSetContains()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Please input the Courses had choice by student:");
        String name = input.next();
        Course course = new Course();
        System.out.println("new course is:"+course.name);
        System.out.println("alternative Course is:"+ course.name + student.courses.contains(course));
    }
    public static void main(String[] args) {
        SetText num1 = new SetText(); 
        num1.textAdd();
        num1.textcontains();
        num1.CreatestudentandSelectcourse();
        num1.textSetContains();

    }
}
package com.imooc.collection;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;

public class MapText {
    public Map<String,Student> students;
    public MapText()
    {
        this.students = new HashMap<String, Student>();
    }
    public void textadd()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Please input the students amount");
        int i = input.nextInt();
        while(i>0)
        {
            System.out.println("Please input the "+(3-i+1)+" student ID");
            String id1 = input.next();
            Student st1 = students.get(id1);
            if(st1==null)
            {
                System.out.println("PLease input the student's name");
                String name1 = input.next();
                Student stu1 = new Student(id1,name1);
                students.put(id1, stu1);
                System.out.println("input student finished:"+students.get(id1).name);
            }else{
                System.out.println("this id had already register!");
                continue;
            }
            i--;
        }
    }
    public void keyset()
    {
        Set<String>keyset = students.keySet();
        System.out.println(" have students total number is :"+students.size());
        for (String stuId : keyset) {
            Student st = students.get(stuId);
            if(st!=null)
            {
                System.out.println("student---"+st.name);
            }
        }
    }
    public void removetext()
    {
        Scanner input2 = new Scanner(System.in);
        while(true)
        {
            System.out.println("Please input removed student ID:");
            String id = input2.next();
            Student st = students.get(id);
            if(st==null)
            {
                System.out.println("cannot find student!");
                continue;
            }
            students.remove(id);
            System.out.println("remove successful!");
            break;
        }
    }
    public void EntrySettext()
    {
        Set<Entry<String,Student>>entryset = students.entrySet();
        for(Entry<String,Student>entry:entryset)
        {
            System.out.println("gain the set:" +entry.getKey());
            System.out.println("and the set's value is:"+entry.getValue());
        }
    }
    public void textModify(){
        Scanner input3 = new Scanner(System.in);
        System.out.println("Please input the ID need modify:");
        while(true)
        {
            String ID = input3.next();
            Student student1 = students.get(ID);
            if(student1==null)
            {
                System.out.println("The ID is not contain in Map");
                continue;
            }
            System.out.println("this ID matched with :"+student1.name);
            System.out.println("please input the new student name:");
            String name = input3.next();
            Student newstudent = new Student(ID,name);
            students.put(ID, newstudent);
            System.out.println("change is successful!");
            break;
        }
    }
    public static void main(String[] args) {
        MapText a1 = new MapText();
        a1.textadd();
        a1.keyset();
        a1.removetext();
        a1.EntrySettext();
        a1.textModify();
    }
}
package com.imooc.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 备选课程类
 * @author Okawari
 *
 */
public class ListText {
     /**
      * 用于存放备选课程的List  
      */
    public List <Course>coursesToSelect;
    public ListText()  
    {
        this.coursesToSelect = new ArrayList<Course>();
    }
    /**
     * 用于往courseToSelect中添加备选课程
     */
    public void textAdd()
    {
        //创建一个课程对象,通过调用List方法,添加到备选课程List当中

        coursesToSelect.add(new Course("1","数据结构"));
        Course temp = (Course)coursesToSelect.get(0);
        System.out.println("添加了课程:"+temp.id+","+temp.name);
        Course[]course = {new Course("3","离散数学"),new Course("4","高等数学")};
        coursesToSelect.addAll(Arrays.asList(course));
        Course temp2 = (Course)coursesToSelect.get(1);
        Course temp3 = (Course)coursesToSelect.get(2);
        System.out.println("添加了两门课程:"+temp2.id+","+temp3.name+'\n'+temp3.id+","+temp3.name);
    }
    public void textIterator()
    {
        Iterator it = coursesToSelect.iterator();
        System.out.println("有如以下课程待选(通过迭代器访问):");
        while(it.hasNext())
        {
            Course cr = (Course)it.next();
            System.out.println("课程:"+cr.id+","+cr.name);
        }
        String names[ ][ ]={{"tom","jack","mike"},{"zhangsan","lisi","wangwu"}}; 
    }
    public void modify()
    {
        coursesToSelect.set(2,new Course("4","体育"));
    }
    public void textmove()
    {
        Course ct = (Course) coursesToSelect.get(1);
        System.out.println("即将删除的课程为:"+ct.id+","+ct.name);
        coursesToSelect.remove(ct);

    }
    public static void main(String[] args)
    {
        ListText It = new ListText();
        It.textAdd();
        It.modify();
        It.textIterator();
        It.textmove();
        It.textIterator();
    }

}
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消