容器:我的java笔记(2)
生活随笔
收集整理的這篇文章主要介紹了
容器:我的java笔记(2)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
關(guān)于容器的remove方法:
有一個例子:
當remove中的對象和列表中的對象Equals函數(shù)為true時,
才會將列表中對象刪除掉。 注意:如果一個對象沒有重寫equals函數(shù)
將會調(diào)用父類的equals函數(shù) ,判斷是否兩個對象是否為同一個對象引用。
import java.util.*;
public class BasicContainer {
??? public static void main(String[] args) {
??????? Collection c = new HashSet();
??????? c.add("hello");
??????? c.add(new Name("f1","l1"));
??????? c.add(new Integer(100));
??????? c.remove("hello");
??????? c.remove(new Integer(100));
??????? System.out.println
????????????????? (c.remove(new Name("f1","l1")));
??????? System.out.println(c);
??? }
}
class Name implements Comparable {
??? private String firstName,lastName;
??? public Name(String firstName, String lastName) {
??????? this.firstName = firstName; this.lastName = lastName;
??? }
??? public String getFirstName() {? return firstName;?? }
??? public String getLastName() {?? return lastName;?? }
??? public String toString() {? return firstName + " " + lastName;? }
???
??? public boolean equals(Object obj) {
??? ??? if (obj instanceof Name) {
??? ??????? Name name = (Name) obj;
??? ??????? return (firstName.equals(name.firstName))
??? ??????????? && (lastName.equals(name.lastName));
??? ??? }
??? ??? return super.equals(obj);
??? ??? }
??? ??? public int hashCode() {
??? ??? ??? return firstName.hashCode();
??? ??? }
??? ???
??? ???
??? ???
??? ??? public int compareTo(Object o) {
??????? Name n = (Name)o;
??????? int lastCmp =
??????????? lastName.compareTo(n.lastName);
??????? return
???????????? (lastCmp!=0 ? lastCmp :
????????????? firstName.compareTo(n.firstName));
??? }
??? ???
}
?
有一個例子:
當remove中的對象和列表中的對象Equals函數(shù)為true時,
才會將列表中對象刪除掉。 注意:如果一個對象沒有重寫equals函數(shù)
將會調(diào)用父類的equals函數(shù) ,判斷是否兩個對象是否為同一個對象引用。
import java.util.*;
public class BasicContainer {
??? public static void main(String[] args) {
??????? Collection c = new HashSet();
??????? c.add("hello");
??????? c.add(new Name("f1","l1"));
??????? c.add(new Integer(100));
??????? c.remove("hello");
??????? c.remove(new Integer(100));
??????? System.out.println
????????????????? (c.remove(new Name("f1","l1")));
??????? System.out.println(c);
??? }
}
class Name implements Comparable {
??? private String firstName,lastName;
??? public Name(String firstName, String lastName) {
??????? this.firstName = firstName; this.lastName = lastName;
??? }
??? public String getFirstName() {? return firstName;?? }
??? public String getLastName() {?? return lastName;?? }
??? public String toString() {? return firstName + " " + lastName;? }
???
??? public boolean equals(Object obj) {
??? ??? if (obj instanceof Name) {
??? ??????? Name name = (Name) obj;
??? ??????? return (firstName.equals(name.firstName))
??? ??????????? && (lastName.equals(name.lastName));
??? ??? }
??? ??? return super.equals(obj);
??? ??? }
??? ??? public int hashCode() {
??? ??? ??? return firstName.hashCode();
??? ??? }
??? ???
??? ???
??? ???
??? ??? public int compareTo(Object o) {
??????? Name n = (Name)o;
??????? int lastCmp =
??????????? lastName.compareTo(n.lastName);
??????? return
???????????? (lastCmp!=0 ? lastCmp :
????????????? firstName.compareTo(n.firstName));
??? }
??? ???
}
?
轉(zhuǎn)載于:https://blog.51cto.com/344551/68978
總結(jié)
以上是生活随笔為你收集整理的容器:我的java笔记(2)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 6月份生蚝还肥吗
- 下一篇: nginx服务无法停止(Windows)