`
javacto
  • 浏览: 84090 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

collection__Interface【001】

阅读更多
[code="java"]马士兵 ___第七章__容器  学习笔记

package com.testcollection;
import java.util.ArrayList;
import java.util.Collection ;

//collection 接口的测试
//练习遍历容器 Iterator
public class TestCollection {
	public static void main(String args[]) {
	    // 接口引用指向具体实现类的对象,为实现多态打下了坚实的基础
		// 我们可以将new ArrayList()改成new LinkedList()等等,而不需要改动该程序中的其他部分
	    // 极大地提高了可扩充性,多态的作用也明显表现了出来
		Collection c =  new ArrayList() ; 
		c.add("hello") ;
		c.add(new Integer(100)) ; 
		c.add(new Name("f1","l1")) ;
		c.add(new Name("f2","22")) ;
//		c.remove("hello") ;
		
//		Iterator i = c.iterator();
//		while(i.hasNext()) {
//			Name n = (Name)i.next() ;
//			System.out.println(n.getFirstName()) ;
//		}
		System.out.println(c.size()) ;
		
		// 这3个类(String、Name、Integer)都已经重写了Object类的toString方法
		// 打印接口引用c,实际上是分别调用这个接口中各个对象的toString方法
		System.out.println(c) ; 
		System.out.println("下面测试remove") ;
		System.out.println(c.remove("hello")) ; //true
		System.out.println(c.remove(new Integer(100))) ;//true
		System.out.println(c.remove(new Name("f1","l1"))) ; //false 当Name的类中没有覆写equals方法时为FALSE
		//上面为false 的原因:此处新new 出来的对象,删除时比较的是equals方法,而Name类中未重写
        //equals方法,而String和Integer 这两个类已经实现了equals方法
 
	}
	
}
class Name{
	private String firstName, secondName ;
	Name(String firstName, String secondName) {
		this.firstName = firstName ;
		this.secondName = secondName ;
	}
	public String toString(){
		return firstName + " " + secondName ;
	}
	public String getFirstName() {
		return firstName ;
	}
	public String getSecondName() {
		return secondName;
	}
	public boolean equals (Object obj) {
		if(obj instanceof Name) {
			Name n = (Name) obj ;
		    //比较这两个对象的内容是否一致
			if (this.firstName.equals(n.firstName)
					&& this.secondName.equals(n.secondName))
				return true;
			else
				return false;
		}else {
			//交给Name的父类去处理(其父类是object类,比较equals就相当于还是比较=)
			return super.equals(obj) ;
		}
	}
	public int hashcode() {
		return this.firstName.hashCode() ;
	}
}

list :有序可重复;

set: 无序不可重复;

重写equals方法,应该也重写hashCode 方法。两个对象若 equals的话,则两个对象的hascode 相等。

hashCode 多做索引时使用;



分享到:
评论

相关推荐

    PS2__PC-AT__keyboard_Interface__GCC_.zip_interface_ps2_ps2 keybo

    GCC是GNU Compiler Collection的缩写,是一个开源的编译器套件,支持多种编程语言,包括C和C++,广泛用于嵌入式开发。 从标签“interface ps2 ps2_keyboard”我们可以推断,这个项目的核心内容是关于如何设计和实现...

    SAP Collection Ebooks: Data Interface

    SAP Collection Ebooks: Data Interface

    oracle call interface programmers guide(19c)

    - 使用oci_collection_create和oci_collection_append插入和检索集合元素。 5.游标变量(Cursor Variables) - 游标变量的概念:理解游标变量如何在PL/SQL和C之间传递结果集。 - 游标变量操作:oci_bind_by_name...

    Java-Collections

    Java集合## CIT360课程###集合接口: [收藏界面](1_The Collection Interface.txt) •[示例](collectionExample.js) [列表界面](2_The List Interface.txt) •[示例](interfaceExample.js) [The Set](3_The...

    AUTOSAR_MOD_AISpecification.zip

    5. **AUTOSAR_MOD_AISpecification_Collection_Body_Blueprint.arxml** 和 **AUTOSAR_MOD_AISpecification_Collection_Chassis_Blueprint.arxml**:这两个文件可能涉及到车身和底盘相关数据的集合,对于智能驾驶系统...

    AbstractListMultimap.rar_interface

    3. **批量操作**:支持`putAll(key, collection)`,将整个集合的值一次性添加到指定键的列表中,以及`getAll(key)`获取键对应的全部值。 4. **视图操作**:`ListMultimap`提供了`asMap()`方法,返回一个`Map`视图,...

    java_ms.rar_Math Class_java collection

    第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?  第三,Static Nested Class 和 Inner Class的不同,说得越多越好(面试题有的很笼统)。  第四...

    ios Human Interface Guidelines翻译版

    指南详细介绍了界面元素的使用,如控件(Buttons、Switches、Sliders等)、导航(Navigation Bars、Tab Bars、Toolbars)、表视图(Table Views)和收集视图(Collection Views)。每个元素都有其特定的用途和交互...

    IRPTrace2.00.002.zip

    _URB_CONTROL_GET_INTERFACE_REQUEST _URB_CONTROL_GET_STATUS_REQUEST _URB_CONTROL_TRANSFER _URB_CONTROL_VENDOR_OR_CLASS_REQUEST _URB_ISOCH_TRANSFER _URB_GET_CURRENT_FRAME_NUMBER _URB_GET_FRAME_...

    The Java Native Interface

    THIS book covers the Java™ Native Interface (JNI). It will be useful to you if you are interested in any of the following: • integrating a Java application with legacy code written in languages such...

    Get-LAN-interface-speed-1.zip_LAN speed

    标题"Get-LAN-interface-speed-1.zip_LAN speed"表明这是一个关于获取局域网(LAN)接口速度的程序或工具,而描述中的"用VB來讀取網路卡介面的, 廠牌型號及連線速度"提示我们这是通过Visual Basic(VB)编程语言实现...

    iOS 7 UIKit User Interface Catalog

    ### iOS 7 UIKit User Interface Catalog知识点详解 #### 一、概览 在iOS 7的UIKit框架中,苹果提供了一份详尽的人机交互指南——**iOS 7 UIKit User Interface Catalog**,该指南全面覆盖了iOS应用程序开发中常用...

    UIKit User Interface Catalog.pdf

    ### UIKit User Interface Catalog知识点概述 本文档主要介绍了iOS开发中UIKit框架下的各种用户界面元素及其使用方法、行为特征以及外观自定义等内容。UIKit是iOS应用开发的基础库之一,提供了丰富的UI组件来帮助...

    Implement with Class and Collection a List Collection with a

    标题 "Implement with Class and Collection a List Collection with a" 暗示了我们需要讨论如何使用类(Class)和集合(Collection)来实现一个列表集合,并具备添加(add)和移除(remove)数据元素的功能。...

    The Java Native Interface 原版pdf by Liang

    THIS book covers the Java™ Native Interface (JNI). It will be useful to you if you are interested in any of the following: • integrating a Java application with legacy code written in languages such...

    Collection code

    本项目名为"Collection code",可以推断出它涉及到的是使用C#编程语言进行集合操作的相关学习与实践。在大学的第二阶段学习中,这样的作业通常会涵盖基本的集合类的使用、自定义集合的实现以及集合算法的优化。 C#...

    Analog Circuit Design III - Design Note Collection

    Design Note Collection, the third book in the Analog Circuit Design series, is a comprehensive volume of applied circuit design solutions, providing elegant and practical design techniques. Design ...

    collection:数组很棒,但是有时候项目需要知道它们是其中的一部分。 然后,您需要一个收藏夹

    介面实作 Item ArrayItem 集合接口Cocur\Collection\CollectionInterface定义了Cocur\Collection\CollectionInterface的基本方法,并扩展了IteratorAggregate和Countable 。 Cocur \ Collection \ Collection...

    Vida_Postman_Collection

    1. **API测试**:API(Application Programming Interface)测试是软件测试的一部分,用于验证API直接的行为和功能。API作为系统间的通信桥梁,确保其正确性至关重要。 2. **Postman**:Postman是一款流行的API开发...

Global site tag (gtag.js) - Google Analytics