博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hasSet,TreeSet,ArrayList,LinkedList,Vector,HashMap,HashTable,TreeMap利用Iterator进行输出
阅读量:6240 次
发布时间:2019-06-22

本文共 9573 字,大约阅读时间需要 31 分钟。

基础类,没有重写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
set=new HashSet
(); 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
tree=new TreeSet
(); 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
treemap=new TreeMap
(); treemap.put(s1, "TreeMap one"); treemap.put(s2, "TreeMap two"); treemap.put(s3, "TreeMap three"); treemap.put(s4, "TreeMap four"); for(Map.Entry
entry: treemap.entrySet()) { System.out.println("The treemap key is "+entry.getKey().a+" The value is "+ entry.getValue()); }*/ //HasMap System.out.println("\nHashMap......"); Map
hashmap=new HashMap
(); hashmap.put(s1, "HashMap one"); hashmap.put(s2, "HashMap two"); hashmap.put(s3, "HashMap three"); hashmap.put(s4, "HashMap four"); for(Map.Entry
entry : hashmap.entrySet()) { System.out.println("The key is "+entry.getKey().a+" The value is "+entry.getValue()); } //HasTable System.out.println("\nHashTable......"); Map
hashtable=new Hashtable
(); hashtable.put(s1, "HashTable one"); hashtable.put(s2, "HashTable two"); hashtable.put(s3, "HashTable three"); hashtable.put(s4, "HashTable four"); for(Map.Entry
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 twoThe key is http://www.yjbys.com2/ The value is HashMap fourThe key is http://www.yjbys.com/ The value is HashMap oneThe key is http://www.yjbys.com2/ The value is HashMap threeHashTable......The HashTable key is http://www.yjbys.com2/ The HashTable value is HashTable fourThe HashTable key is http://www.yjbys.com2/ The HashTable value is HashTable threeThe HashTable key is http://www.yjbys.com1/ The HashTable value is HashTable twoThe HashTable key is http://www.yjbys.com/ The HashTable value is HashTable oneLinkedList.....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
set=new HashSet
(); 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
tree=new TreeSet
(); 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
treemap=new TreeMap
(); treemap.put(s1, "TreeMap one"); treemap.put(s2, "TreeMap two"); treemap.put(s3, "TreeMap three"); treemap.put(s4, "TreeMap four"); for(Map.Entry
entry: treemap.entrySet()) { System.out.println("The treemap key is "+entry.getKey().a+" The value is "+ entry.getValue()); }*/ //HasMap System.out.println("\nHashMap......"); Map
hashmap=new HashMap
(); hashmap.put(s1, "HashMap one"); hashmap.put(s2, "HashMap two"); hashmap.put(s3, "HashMap three"); hashmap.put(s4, "HashMap four"); for(Map.Entry
entry : hashmap.entrySet()) { System.out.println("The key is "+entry.getKey().a+" The value is "+entry.getValue()); } //HasTable System.out.println("\nHashTable......"); Map
hashtable=new Hashtable
(); hashtable.put(s1, "HashTable one"); hashtable.put(s2, "HashTable two"); hashtable.put(s3, "HashTable three"); hashtable.put(s4, "HashTable four"); for(Map.Entry
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 twoThe key is http://www.yjbys.com/ The value is HashMap oneThe key is http://www.yjbys.com2/ The value is HashMap fourHashTable......The HashTable key is http://www.yjbys.com1/ The HashTable value is HashTable twoThe HashTable key is http://www.yjbys.com/ The HashTable value is HashTable oneThe HashTable key is http://www.yjbys.com2/ The HashTable value is HashTable fourLinkedList.....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的输出。

 

转载地址:http://qgbia.baihongyu.com/

你可能感兴趣的文章
mysql AM/PM to 24 hours fromat
查看>>
远程唤醒UP Board
查看>>
网页打印
查看>>
Loading——spin.js
查看>>
Hadoop完全分布式环境搭建(四)——基于Ubuntu16.04安装和配置Hadoop大数据环境...
查看>>
Mule ESB工程的部署
查看>>
分离被碰撞物体, 求碰撞冲量
查看>>
js移动端 可移动滑块
查看>>
【kruscal】【最小生成树】poj3522 Slim Span
查看>>
jquery ajax提交表单数据的两种方式
查看>>
hdu 2102 A计划-bfs
查看>>
学习集合
查看>>
18校招借鉴
查看>>
JAVA第三次作业
查看>>
2017ICPC北京 J:Pangu and Stones
查看>>
Pandas 数据清洗保存
查看>>
SpringBoot + nodeJS + zookeeper 搭建微服务示例
查看>>
《互联网时代》第二集·浪潮
查看>>
8.10 exec函数
查看>>
Shell命令-文件及内容处理之sort、uniq
查看>>