Java容器问题总结
作者:
手打小黑板 | 来源:发表于
2020-02-13 09:26 被阅读0次import java.util.*;
public class CollectionTest {
public static void main(String[] args){
/*
Collection; //接口定义集合
Set; //Set无序不可重复集合 接口
List; // 有序可重复列表 接口
HashSet;
LinkedList;
ArrayList;
Map; // 键值对数据表 hashcode等同于key value等同于equals对比的值
HashMap;
Hashtable;
*/
Collection c = new HashSet();
c.add("集合");
c.add(true);
c.add("Collection");
c.add(new Human(456));
c.add(new Float(888.666f));
//容器获取iterator迭代器位置始终从0号元素的下标开始
Iterator _iter = c.iterator();
while(_iter.hasNext()){
System.out.println(_iter.next());
}
/*System.out.println(c.add(new Human(456)));
System.out.println(c);
System.out.println("Hello".hashCode());
System.out.println(new String("Hello").hashCode());
*/
}
}
class Human{
private int gine;
Human(int gine){
this.gine = gine;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Human){
return this.gine == ((Human)obj).gine ? true : false;
}
return false;
}
@Override
public int hashCode() { //重写hashcode hashcode用于对比两个对象的引用的值 所以在set容器数组中必须重写
return Integer.hashCode(this.gine);
}
@Override
public String toString() {
return String.valueOf(this.gine);
}
}
本文标题:Java容器问题总结
本文链接:https://www.haomeiwen.com/subject/fjkkfhtx.html
网友评论