hasSet,TreeSet,ArrayList,LinkedList,Vector,HashMap,HashTable,TreeMap利用Iterator进行输出
基础类,没有重写hashCode()和equals()方法:
package niukewang;
import java.util.Objects;
public class setClass {
String a;
String b;
public setClass(String a, String b)
{
this.a=a;
this.b=b;
}
}
测试类:
package niukewang; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector; public class test1 { public static void main(String args[])
{
setClass s1=new setClass("http://www.yjbys.com/", "");
setClass s2=new setClass("http://www.yjbys.com1/", "");
setClass s3=new setClass("http://www.yjbys.com2/", "");
setClass s4=new setClass("http://www.yjbys.com2/", ""); //hasSet
System.out.println("hasSet......");
Set<setClass> set=new HashSet<setClass>();
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
Iterator it= set.iterator();
while(it.hasNext())
{
setClass ob=(setClass)it.next();
System.out.println(ob.a);
} /* //TreeSet
System.out.println("\nTreeSet........");
Set<setClass> tree=new TreeSet<setClass>();
tree.add(s1);
tree.add(s2);
tree.add(s3);
tree.add(s4);
Iterator treeit=tree.iterator();
while(treeit.hasNext())
{
setClass ob=(setClass) treeit.next();
System.out.println(ob.a);
}
*/
//TreeMap
/* System.out.println("\nTreeMap.........");
Map<setClass,String> treemap=new TreeMap<setClass,String>();
treemap.put(s1, "TreeMap one");
treemap.put(s2, "TreeMap two");
treemap.put(s3, "TreeMap three");
treemap.put(s4, "TreeMap four");
for(Map.Entry<setClass, String> entry: treemap.entrySet())
{
System.out.println("The treemap key is "+entry.getKey().a+" The value is "+ entry.getValue());
}*/ //HasMap
System.out.println("\nHashMap......");
Map<setClass,String> hashmap=new HashMap<setClass, String>();
hashmap.put(s1, "HashMap one");
hashmap.put(s2, "HashMap two");
hashmap.put(s3, "HashMap three");
hashmap.put(s4, "HashMap four");
for(Map.Entry<setClass, String> entry : hashmap.entrySet())
{
System.out.println("The key is "+entry.getKey().a+" The value is "+entry.getValue());
} //HasTable
System.out.println("\nHashTable......");
Map<setClass,String> hashtable=new Hashtable<setClass, String>();
hashtable.put(s1, "HashTable one");
hashtable.put(s2, "HashTable two");
hashtable.put(s3, "HashTable three");
hashtable.put(s4, "HashTable four");
for(Map.Entry<setClass, String> entry: hashtable.entrySet())
{
System.out.println("The HashTable key is "+entry.getKey().a+" The HashTable value is "+entry.getValue());
} //LinkedList
System.out.println("\nLinkedList.....");
List list=new LinkedList();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
Iterator listit=list.iterator();
while(listit.hasNext())
{
setClass ob=(setClass)listit.next();
System.out.println(ob.a);
} //ArrayList
System.out.println("\nArrayList.....");
List array=new ArrayList();
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
Iterator arrayit=array.iterator();
while(arrayit.hasNext())
{
setClass ob=(setClass)arrayit.next();
System.out.println(ob.a);
} //vector
System.out.println("\nvector.....");
Vector v=new Vector();
v.add(s1);
v.add(s2);
v.add(s3);
v.add(s4);
Iterator vit=v.iterator();
while(vit.hasNext())
{
setClass ob=(setClass)vit.next();
System.out.println(ob.a);
} }
}
输出结果:
由于没有重写hasCode和equals方法,所以s3和s4对象认为是不相同的,因此在set中被看成了不同的对象;同理,在hashMap和hashTable中,其根据hashCode的值对数据进行存储的,所以,hashcode的值不一样,因此也存储了4个数。
hasSet......
http://www.yjbys.com1/
http://www.yjbys.com2/
http://www.yjbys.com/
http://www.yjbys.com2/ HashMap......
The key is http://www.yjbys.com1/ The value is HashMap two
The key is http://www.yjbys.com2/ The value is HashMap four
The key is http://www.yjbys.com/ The value is HashMap one
The key is http://www.yjbys.com2/ The value is HashMap three HashTable......
The HashTable key is http://www.yjbys.com2/ The HashTable value is HashTable four
The HashTable key is http://www.yjbys.com2/ The HashTable value is HashTable three
The HashTable key is http://www.yjbys.com1/ The HashTable value is HashTable two
The HashTable key is http://www.yjbys.com/ The HashTable value is HashTable one LinkedList.....
http://www.yjbys.com/
http://www.yjbys.com1/
http://www.yjbys.com2/
http://www.yjbys.com2/ ArrayList.....
http://www.yjbys.com/
http://www.yjbys.com1/
http://www.yjbys.com2/
http://www.yjbys.com2/ vector.....
http://www.yjbys.com/
http://www.yjbys.com1/
http://www.yjbys.com2/
http://www.yjbys.com2/
改变之后的。
基础类:
package niukewang;
import java.util.Objects;
public class setClass {
String a;
String b;
public setClass(String a, String b)
{
this.a=a;
this.b=b;
}
public int hashCode() {
return a.hashCode();
}
public boolean equals(Object obj)
{
if(obj==null) return false;
if(getClass() != obj.getClass()) return false;
final setClass s=(setClass)obj;
return Objects.equals(this.a, s.a);
}
}
测试类:
package niukewang; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector; public class test1 { public static void main(String args[])
{
setClass s1=new setClass("http://www.yjbys.com/", "");
setClass s2=new setClass("http://www.yjbys.com1/", "");
setClass s3=new setClass("http://www.yjbys.com2/", "");
setClass s4=new setClass("http://www.yjbys.com2/", ""); //hasSet
System.out.println("hasSet......");
Set<setClass> set=new HashSet<setClass>();
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
Iterator it= set.iterator();
while(it.hasNext())
{
setClass ob=(setClass)it.next();
System.out.println(ob.a);
} /* //TreeSet
System.out.println("\nTreeSet........");
Set<setClass> tree=new TreeSet<setClass>();
tree.add(s1);
tree.add(s2);
tree.add(s3);
tree.add(s4);
Iterator treeit=tree.iterator();
while(treeit.hasNext())
{
setClass ob=(setClass) treeit.next();
System.out.println(ob.a);
}
*/
//TreeMap
/* System.out.println("\nTreeMap.........");
Map<setClass,String> treemap=new TreeMap<setClass,String>();
treemap.put(s1, "TreeMap one");
treemap.put(s2, "TreeMap two");
treemap.put(s3, "TreeMap three");
treemap.put(s4, "TreeMap four");
for(Map.Entry<setClass, String> entry: treemap.entrySet())
{
System.out.println("The treemap key is "+entry.getKey().a+" The value is "+ entry.getValue());
}*/ //HasMap
System.out.println("\nHashMap......");
Map<setClass,String> hashmap=new HashMap<setClass, String>();
hashmap.put(s1, "HashMap one");
hashmap.put(s2, "HashMap two");
hashmap.put(s3, "HashMap three");
hashmap.put(s4, "HashMap four");
for(Map.Entry<setClass, String> entry : hashmap.entrySet())
{
System.out.println("The key is "+entry.getKey().a+" The value is "+entry.getValue());
} //HasTable
System.out.println("\nHashTable......");
Map<setClass,String> hashtable=new Hashtable<setClass, String>();
hashtable.put(s1, "HashTable one");
hashtable.put(s2, "HashTable two");
hashtable.put(s3, "HashTable three");
hashtable.put(s4, "HashTable four");
for(Map.Entry<setClass, String> entry: hashtable.entrySet())
{
System.out.println("The HashTable key is "+entry.getKey().a+" The HashTable value is "+entry.getValue());
} //LinkedList
System.out.println("\nLinkedList.....");
List list=new LinkedList();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
Iterator listit=list.iterator();
while(listit.hasNext())
{
setClass ob=(setClass)listit.next();
System.out.println(ob.a);
} //ArrayList
System.out.println("\nArrayList.....");
List array=new ArrayList();
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
Iterator arrayit=array.iterator();
while(arrayit.hasNext())
{
setClass ob=(setClass)arrayit.next();
System.out.println(ob.a);
} //vector
System.out.println("\nvector.....");
Vector v=new Vector();
v.add(s1);
v.add(s2);
v.add(s3);
v.add(s4);
Iterator vit=v.iterator();
while(vit.hasNext())
{
setClass ob=(setClass)vit.next();
System.out.println(ob.a);
} }
}
输出结果:
由于覆盖了hashCode和equals方法,因此s3和s4被认为是相同的对象。
hasSet......
http://www.yjbys.com1/
http://www.yjbys.com/
http://www.yjbys.com2/ HashMap......
The key is http://www.yjbys.com1/ The value is HashMap two
The key is http://www.yjbys.com/ The value is HashMap one
The key is http://www.yjbys.com2/ The value is HashMap four HashTable......
The HashTable key is http://www.yjbys.com1/ The HashTable value is HashTable two
The HashTable key is http://www.yjbys.com/ The HashTable value is HashTable one
The HashTable key is http://www.yjbys.com2/ The HashTable value is HashTable four LinkedList.....
http://www.yjbys.com/
http://www.yjbys.com1/
http://www.yjbys.com2/
http://www.yjbys.com2/ ArrayList.....
http://www.yjbys.com/
http://www.yjbys.com1/
http://www.yjbys.com2/
http://www.yjbys.com2/ vector.....
http://www.yjbys.com/
http://www.yjbys.com1/
http://www.yjbys.com2/
http://www.yjbys.com2/
但是TreeSet和TreeMap还是不能这么输出,因为要实现Comparable接口。因为TreesSet和TreeMap是一个有序的集合,必须知道你想怎么排列。你可以换成LinkedList或ArrayList就不用了。
下一篇文章是对TreeSet和TreeMap的输出。
hasSet,TreeSet,ArrayList,LinkedList,Vector,HashMap,HashTable,TreeMap利用Iterator进行输出的更多相关文章
- Set ,List,ArrayList,LinkedList,Vectory,HashMap,Hashtable,HashSet,TreeSet,TreeSet
Set与List区别: 两者都是接口,并继承Collection接口:List有序,允许重复:Set无序,不能重复: ArrayList与LinkList区别: ArrayList是动态数组,查询效率 ...
- 杨晓峰-Java核心技术-9 HashMap Hashtable TreeMap MD
目录 第9讲 | 对比Hashtable.HashMap.TreeMap有什么不同? 典型回答 考点分析 知识扩展 Map 整体结构 有序 Map HashMap 源码分析 容量.负载因子和树化 精选 ...
- ArrayList LinkedList Vector
ArrayList是基于数组实现的,没有容量的限制. 在删除元素的时候,并不会减少数组的容量大小,可以调用ArrayList的trimeToSize()来缩小数组的容量. ArrayList, Lin ...
- Collections+Iterator 接口 | Map+HashMap+HashTable+TreeMap |
Collections+Iterator 接口 1. Collections 是一个操作 Set.List 和 Map 等集合的工具类 Collections 中提供了大量方法对集合元素进行排序.查询 ...
- Java 集合系列14之 Map总结(HashMap, Hashtable, TreeMap, WeakHashMap等使用场景)
概要 学完了Map的全部内容,我们再回头开开Map的框架图. 本章内容包括:第1部分 Map概括第2部分 HashMap和Hashtable异同第3部分 HashMap和WeakHashMap异同 转 ...
- ArrayList, LinkedList, Vector - dudu:史上最详解
ArrayList, LinkedList, Vector - dudu:史上最详解 我们来比较一下ArrayList, LinkedLIst和Vector它们之间的区别.BZ的JDK版本是1.7.0 ...
- Map总结(HashMap, Hashtable, TreeMap, WeakHashMap等使用场景)
概要 学完了Map的全部内容,我们再回头开开Map的框架图. 本章内容包括:第1部分 Map概括第2部分 HashMap和Hashtable异同第3部分 HashMap和WeakHashMap异同 转 ...
- Map总结--HashMap/HashTable/TreeMap/WeakHashMap使用场景分析(转)
首先看下Map的框架图 1.Map概述 1.Map是键值对映射的抽象接口 2.AbstractMap实现了Map中绝大部分的函数接口,它减少了“Map实现类”的重复编码 3.SortedMap有序的“ ...
- OAF_开发系列17_实现OAF数组应用Vector / Hashmap / Hashtable / Arraylist(案例)
20150506 Created By BaoXinjian
随机推荐
- 2014 Super Training #4 E Paint the Grid Reloaded --联通块缩点+BFS
原题: ZOJ 3781 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 给一个n*m的X,O构成的格子,对 ...
- UVA 103 Stacking Boxes --LIS
实际上是一个扩展维度的矩形嵌套问题. 一个物体能嵌入另一个物体中,当且仅当这个物体的所有维度的长度都小于另外一个(本题是小于等于),又因为可以旋转等变换,所以干脆将每个箱子的边从小到大排序,以便于判断 ...
- 在springmvc中使用hibernate-validate
在springmvc.xml中加入 <!-- 国际化配置 --> <bean id="localeResolver" class="org.spring ...
- 转: EclipseIDE开发 for C++
Eclipse 开发C++ 程序 http://tangmingjie2009.iteye.com/blog/2088363 Eclipse 开发C++ 程序 (二) 静态库 http://tangm ...
- AutoMapper使用
1.安装 现在AutoMapper已经更新到5.0版本了,可查看 http://www.nuget.org/packages/AutoMapper/ 我环境是4.0的,nuget安装 http://w ...
- .net 4.0 自定义本地缓存策略的不同实现
在分布式系统的开发中,为了提高系统运行性能,我们从服务器中获取的数据需要缓存在本地,以便下次使用,而不用从服务器中重复获取,有同学可能要问,为什么不使用 分布式缓存等,注意,服务器端肯定是考虑到扩展, ...
- [py]编码 Unicode utf-8
什么是字符集 在介绍字符集之前,我们先了解下为什么要有字符集.我们在计算机屏幕上看到的是实体化的文字,而在计算机存储介质中存放的实际是二进制的比特流.那么在这两者之间的转换规则就需要一个统一的标准,否 ...
- no.1
#import requests import urllib import bs4 try: html=urllib.urlopen('http://anyun.org') except HTTPer ...
- Caffe学习系列(11):图像数据转换成db(leveldb/lmdb)文件
在深度学习的实际应用中,我们经常用到的原始数据是图片文件,如jpg,jpeg,png,tif等格式的,而且有可能图片的大小还不一致.而在caffe中经常使用的数据类型是lmdb或leveldb,因此就 ...
- 《图解tcp/ip》读书笔记(一)
我先讲三句话: 一."万物互联的时代到了."我们生活在这样一个互联网急速发展的时代,也许很快就会发现,你能接触到的一切都可以连接到互联网了,电脑.手机这 ...