`
javacto
  • 浏览: 86751 次
  • 性别: 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

    Collection接口_演练.pdf

    在Java编程语言中,接口(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应用程序开发中常用...

    OpenMP Application Program Interface Version 4.0

    The collection of compiler directives, library routines, and environment variables described in this document collectively define the specification of the OpenMP Application Program Interface (OpenMP ...

    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 ...

    UIKit User Interface Catalog.pdf

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

    powerpc_eabi_app.rar_PowerPC EABI_PowerPC_EABI_Tools_powerpc_pow

    标题中的"powerpc_eabi_app.rar"提示我们讨论的主题是关于PowerPC(Power Architecture Enhanced Instruction Set Architecture)处理器的嵌入式应用二进制接口(Embedded Application Binary Interface, EABI)。...

    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...

Global site tag (gtag.js) - Google Analytics