黑马程序员_java集合在类之间存在包含体现
来源:互联网 发布:linux网络工程师 编辑:程序博客网 时间:2024/06/06 01:28
------- android培训、java培训、期待与您交流! ----------
import java.util.Set;
import java.util.TreeSet;
class Student implements Comparable<Student>{
private String id ;
private String name ;
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (getClass() != obj.getClass())
throw new ClassCastException(obj + "不能被转成Student类对象");
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
throw new RuntimeException(name+"的id:"+id+"已被"+other.name+"所使用");
return true;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Student o) {
int num = id.compareTo(o.id);
if(num == 0){
if( !(this.toString().equals(o.toString())))
throw new RuntimeException(name+"的id:"+id+"已被"+o.name+"所使用");
return 0 ;
}
return num ;
}
}
class Banji implements Comparable<Banji>{
private String name;
private Collection<Student> studentColl ; //Banji 里定义学生集合
public Banji(String ronmName, Collection<Student> studentColl) {
super();
this.name = ronmName;
this.studentColl = studentColl;
}
Banji(String name){
this.name = name ;
this.studentColl = new TreeSet<Student>();
}
Banji(String name ,TreeSet<Student> studentColl){
this.name = name ;
this.studentColl = studentColl;
}
//提供相应的方法来管理学生集合
public boolean addStudent(Student stu){
return studentColl.add(stu);
}
public Student removeStudentByid(String id){
for(Iterator<Student> it = studentColl.iterator() ; it.hasNext() ;){
Student s = it.next();
if( s.getId().equals(id)){
it.remove();
return s;
}
}
return null;
}
@Override
public int compareTo(Banji o) {
// TODO Auto-generated method stub
return 0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
一个学校会有很多个班级,每个班级又会对应很多学生,可通过上级类中包含下级类的集合来实现.
/* ------学 -------校--------
* | 班级号A——————————班级体A
* | 学号——姓名
* | 学号——姓名
* | 学号——姓名
* | ...
* |
* | 班级号B——————————班级体B
* | ......
*/
import java.util.Collection;
import java.util.Iterator;import java.util.Set;
import java.util.TreeSet;
class Student implements Comparable<Student>{
private String id ;
private String name ;
public Student(String id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (getClass() != obj.getClass())
throw new ClassCastException(obj + "不能被转成Student类对象");
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
throw new RuntimeException(name+"的id:"+id+"已被"+other.name+"所使用");
return true;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Student o) {
int num = id.compareTo(o.id);
if(num == 0){
if( !(this.toString().equals(o.toString())))
throw new RuntimeException(name+"的id:"+id+"已被"+o.name+"所使用");
return 0 ;
}
return num ;
}
}
class Banji implements Comparable<Banji>{
private String name;
private Collection<Student> studentColl ; //Banji 里定义学生集合
public Banji(String ronmName, Collection<Student> studentColl) {
super();
this.name = ronmName;
this.studentColl = studentColl;
}
Banji(String name){
this.name = name ;
this.studentColl = new TreeSet<Student>();
}
Banji(String name ,TreeSet<Student> studentColl){
this.name = name ;
this.studentColl = studentColl;
}
//提供相应的方法来管理学生集合
public boolean addStudent(Student stu){
return studentColl.add(stu);
}
public Student removeStudentByid(String id){
for(Iterator<Student> it = studentColl.iterator() ; it.hasNext() ;){
Student s = it.next();
if( s.getId().equals(id)){
it.remove();
return s;
}
}
return null;
}
@Override
public int compareTo(Banji o) {
// TODO Auto-generated method stub
return 0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
0 0
- 黑马程序员_java集合在类之间存在包含体现
- 黑马程序员_java集合类
- 黑马程序员_java集合类
- 黑马程序员_java集合类
- 黑马程序员_java集合类
- 黑马程序员_java集合
- 黑马程序员_java集合
- 黑马程序员_Java集合
- 黑马程序员_java集合
- 黑马程序员_java 集合
- 黑马程序员_JAVA:集合
- 黑马程序员_JAVA集合类函数总结
- 黑马程序员_Java中的集合
- 黑马程序员_java基础集合
- 黑马程序员_java集合框架
- 黑马程序员_Java 集合笔记
- 黑马程序员_java之集合
- 黑马程序员_java集合框架
- (原创)Hanoi塔问题的递归方法与非递归方法(java实现)
- 【郝斌数据结构自学笔记】14-15_链表的重要性_typedef的用法
- 瀑布流的认识
- Unity键值意义
- Android蓝牙开发的一些经验
- 黑马程序员_java集合在类之间存在包含体现
- HttpClient-----2、使用HTTP的POST方式与网络交互通信
- 微信服务器之三次数据传递,排重的问题
- Android各种小技巧(持续更新)
- 浅浅谈Java反射机制( JAVA Reflection)
- 对sublime的认识(1)
- 物联网与嵌入式系统概论-week3- Hardware and Software-Lesson3: Operating Systems
- EditText自定义重写
- HttpClient-----3、使用HttpClient进行GET方式通信