亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java開發(fā)中的容器概念、分類與用法深入詳解

 更新時(shí)間:2017年11月07日 10:25:12   作者:安靜的技術(shù)控  
這篇文章主要介紹了Java開發(fā)中的容器概念、分類與用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了java容器的相關(guān)概念、分類、使用方法與注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Java開發(fā)中的容器概念、分類與用法。分享給大家供大家參考,具體如下:

1、容器的概念

在Java當(dāng)中,如果有一個(gè)類專門用來存放其它類的對(duì)象,這個(gè)類就叫做容器,或者就叫做集合,集合就是將若干性質(zhì)相同或相近的類對(duì)象組合在一起而形成的一個(gè)整體

2、容器與數(shù)組的關(guān)系

之所以需要容器:

① 數(shù)組的長(zhǎng)度難以擴(kuò)充
② 數(shù)組中數(shù)據(jù)的類型必須相同

容器與數(shù)組的區(qū)別與聯(lián)系:

① 容器不是數(shù)組,不能通過下標(biāo)的方式訪問容器中的元素
② 數(shù)組的所有功能通過Arraylist容器都可以實(shí)現(xiàn),只是實(shí)現(xiàn)的方式不同
③ 如果非要將容器當(dāng)做一個(gè)數(shù)組來使用,通過toArraylist方法返回的就是一個(gè)數(shù)組

示例程序:

package IT;
import java.util.ArrayList;
import java.util.Iterator;
//數(shù)組的所有功能通過ArrayList容器都可以實(shí)現(xiàn),只是實(shí)現(xiàn)的方式不同
public class App
{
  public static void main(String[] args)
  {
   ArrayList<Integer> arrayList = new ArrayList<Integer>();
   arrayList.add(12);
   arrayList.add(10);
   arrayList.add(35);
   arrayList.add(100);
   Iterator<Integer> iterator = arrayList.iterator();//獲取容器的迭代器
   while(iterator.hasNext())
   {
    Integer value = iterator.next();//獲取當(dāng)前游標(biāo)右邊的元素,同時(shí)游標(biāo)右移-->
    System.out.println(value);
   }
   System.out.println("通過ArrayList容器獲取一個(gè)數(shù)組arr:");
   Object[] arr = arrayList.toArray();
   for(int i=0;i<arr.length;i++)
   {
    System.out.println(arr[i]);
   }
  }
}

輸出結(jié)果:

12
10
35
100

通過ArrayList容器獲取一個(gè)數(shù)組arr:

12
10
35
100

3、容器常用的幾個(gè)方法

boolean add(Object obj):向容器中添加指定的元素
Iterator iterator():返回能夠遍歷當(dāng)前集合中所有元素的迭代器
Object[] toArray():返回包含此容器中所有元素的數(shù)組。
Object get(int index):獲取下標(biāo)為index的那個(gè)元素
Object remove(int index):刪除下標(biāo)為index的那個(gè)元素
Object set(int index,Object element):將下標(biāo)為index的那個(gè)元素置為element
Object add(int index,Object element):在下標(biāo)為index的位置添加一個(gè)對(duì)象element
Object put(Object key,Object value):向容器中添加指定的元素
Object get(Object key):獲取關(guān)鍵字為key的那個(gè)對(duì)象
int size():返回容器中的元素?cái)?shù)

實(shí)例程序:

package IT;
import java.util.ArrayList;
public class App
{
  public static void main(String[] args)
  {
   ArrayList<Integer> arrayList = new ArrayList<Integer>();
   arrayList.add(12);
   arrayList.add(10);
   arrayList.add(35);
   arrayList.add(100);
   System.out.println("原容器中的元素為:");
   System.out.println(arrayList);
   System.out.println("\n");
   /*******重置set(int index,Object element)*******/
   System.out.println("將下標(biāo)為1位置的元素置為20,將下標(biāo)為2位置的元素置為70");
   arrayList.set(1, 20);
   arrayList.set(2, 70);
   System.out.println("重置之后容器中的元素為:");
   System.out.println(arrayList);
   System.out.println("\n");
   /*******中間插隊(duì)add(int index,Object element)*******/
   System.out.println("在下標(biāo)為1的位置插入一個(gè)元素,-----插入元素:此時(shí)容器后面的元素整體向后移動(dòng)");
   arrayList.add(1, 80);//在下標(biāo)為1的位置插入一個(gè)元素,此時(shí)容量加1,-----位置后面的元素整體向后移動(dòng)
   System.out.println("插入之后容器中的元素為:");
   System.out.println(arrayList);
   System.out.println("插入之后容器中的容量為:");
   System.out.println(arrayList.size());
   System.out.println("\n");
   /*******中間刪除元素remove(int index)*******/
   System.out.println("將下標(biāo)為3位置的元素70刪除,-----刪除元素:此時(shí)容器位置后面的元素整體向前移");
   arrayList.remove(3);
   System.out.println("刪除之后容器中的元素為:");
   System.out.println(arrayList);
   System.out.println("刪除之后容器中的容量為:");
   System.out.println(arrayList.size());
  }
}

運(yùn)行結(jié)果:

原容器中的元素為:
[12, 10, 35, 100]
將下標(biāo)為1位置的元素置為20,將下標(biāo)為2位置的元素置為70
重置之后容器中的元素為:
[12, 20, 70, 100]
在下標(biāo)為1的位置插入一個(gè)元素,-----插入元素:此時(shí)容器后面的元素整體向后移動(dòng)
插入之后容器中的元素為:
[12, 80, 20, 70, 100]
插入之后容器中的容量為:
5
將下標(biāo)為3位置的元素70刪除,-----刪除元素:此時(shí)容器位置后面的元素整體向前移
刪除之后容器中的元素為:
[12, 80, 20, 100]
刪除之后容器中的容量為:
4

4、容器的分類

容器分為Set集、List列表、Map映射

Set集合:由于內(nèi)部存儲(chǔ)結(jié)構(gòu)的特點(diǎn),Set集合中不區(qū)分元素的順序,不允許出現(xiàn)重復(fù)的元素,TreeSet容器特殊,元素放進(jìn)去的時(shí)候自然而然就有順序了,Set容器可以與數(shù)學(xué)中的集合相對(duì)應(yīng):相同的元素不會(huì)被加入

List列表:由于內(nèi)部存儲(chǔ)結(jié)構(gòu)的特點(diǎn),List集合中區(qū)分元素的順序,且允許包含重復(fù)的元素。List集合中的元素都對(duì)應(yīng)一個(gè)整數(shù)型的序號(hào)記載其在容器中的位置,可以根據(jù)序號(hào)存取容器中的元素—有序,可以重復(fù)

Map映射:由于內(nèi)部存儲(chǔ)結(jié)構(gòu)的特點(diǎn),映射中不能包含重復(fù)的鍵值,每個(gè)鍵最多只能映射一個(gè)值,否則會(huì)出現(xiàn)覆蓋的情況(后面的value值會(huì)將前面的value值覆蓋掉),Map是一種把鍵對(duì)象和值對(duì)象進(jìn)行映射的集合,即Map容器中既要存放數(shù)據(jù)本身,也要存放關(guān)鍵字:相同的元素會(huì)被覆蓋

注意:對(duì)于Set和Map來說,元素放進(jìn)去之后是沒有順序的,如果希望元素放進(jìn)去之后是有順序的,可以用treeSet和treeMap存儲(chǔ)數(shù)據(jù)。

實(shí)例程序:

var set2 = mutable.Set.empty[Int]
set2 += 10
set2 ++= List(50,100,200)
set2 += 500
println("Set輸出的結(jié)果:")
println(set2)
var map3 = mutable.Map.empty[String,Double]
map3 += "Spark"->90.0
map3 += "Hadoop"->80.0
map3 ++= List("Scala"->100.0,"Java"->60.0)
println("Map輸出的結(jié)果:")
println(map3)

運(yùn)行結(jié)果:

Set輸出的結(jié)果:
Set(100, 50, 500, 10, 200)
Map輸出的結(jié)果:
Map(Hadoop -> 80.0, Spark -> 90.0, Scala -> 100.0, Java -> 60.0)

實(shí)例程序:

var treeSet = TreeSet(10,20,30,90,100,200,50)
println(treeSet)
/*鍵值對(duì)排序是根據(jù)key的值進(jìn)行排序的,沒有value的事情,讓我聯(lián)想到了MapReduce中排序的時(shí)候之所以根據(jù)k2
而不是v2的值進(jìn)行排序,這是因?yàn)楣S成鋬?nèi)部決定的,而不是MapReduce決定的
呵呵!注意:排序區(qū)分大小寫的哦?。。?/
var treeSet2 = TreeSet[String]("Spark","Anang","Baby","Hello")
println(treeSet2)
var treeMap = TreeMap[String,Integer]("Java"->100,"Scala"->88,"Python"->60,"Anglebaby"->500)
println(treeMap)

運(yùn)行結(jié)果:

TreeSet(10, 20, 30, 50, 90, 100, 200)
TreeSet(Anang, Baby, Hello, Spark)
Map(Anglebaby -> 500, Java -> 100, Python -> 60, Scala -> 88)

5、toString()方法的使用:凡是把類對(duì)象放到容器中,相應(yīng)的類都應(yīng)該實(shí)現(xiàn)Object類中的toString()方法;凡是Java中自帶的數(shù)據(jù)類型,都已經(jīng)重寫完了toString()方法

實(shí)例1:(未重寫toString()方法之前)

package IT;
public class App
{
  public static void main(String[] args)
  {
   //Java中自帶的類
   System.out.println("-----凡是Java中自帶的數(shù)據(jù)類型都已經(jīng)重寫完了toString()方法!---");
   System.out.println(new Integer(2).toString());
   System.out.println(new String("zhang").toString());
   //用戶自定義的類Student
   System.out.println(new Student("zhangsan",99.8).toString());
  }
}
class Student
{
  public String name;
  public double score;
  public Student(String name,double score)
  {
   this.name = name;
   this.score = score;
  }
}

輸出結(jié)果:

-----凡是Java中自帶的數(shù)據(jù)類型都已經(jīng)重寫完了toString()方法!---
2
zhang
IT.Student@1af2f973

實(shí)例2:(重寫完toString()方法之后)

package IT;
import java.util.ArrayList;
public class App
{
  public static void main(String[] args)
  {
   ArrayList<Student> arr = new ArrayList<Student>();
   arr.add(new Student("zhangsan",89.8));
   arr.add(new Student("lisi",90));
   arr.add(new Student("wangwu",60.6));
   System.out.println(arr);
  }
}
class Student
{
  public String name;
  public double score;
  public Student(String name,double score)
  {
   this.name = name;
   this.score = score;
  }
  public String toString()
  {
   return this.name+"\t"+this.score;
  }
}

輸出結(jié)果:

[zhangsan 89.8, lisi 90.0, wangwu 60.6]

6、Comparable接口中的compareTo()方法:凡是需要進(jìn)行比較排序的類都應(yīng)該實(shí)現(xiàn)Comparable接口中的compareTo()方法;凡是把類對(duì)象放到以樹為內(nèi)部結(jié)構(gòu)的容器中都應(yīng)該實(shí)現(xiàn)Comparable接口中的compareTo()方法

實(shí)例1:

package IT;
import java.util.ArrayList;
import java.util.Collections;
public class App
{
  public static void main(String[] args)
  {
   ArrayList<Student> arr = new ArrayList<Student>();
   arr.add(new Student("zhangsan",89.8));
   arr.add(new Student("lisi",90));
   arr.add(new Student("wangwu",60.6));
   arr.add(new Student("wangting",85.6));
   Collections.sort(arr);
   for (Student student : arr)
  {
   System.out.println(student);
  }
  }
}
class Student implements Comparable<Student>
{
  public String name;
  public double score;
  public Student(String name,double score)
  {
   this.name = name;
   this.score = score;
  }
  public String toString()
  {
   return this.name+"\t"+this.score;
  }
 public int compareTo(Student obj)
 {
   return (int) (this.score - obj.score);//比較的標(biāo)準(zhǔn)為score進(jìn)行升序
 }
}

輸出結(jié)果:

wangwu 60.6
wangting 85.6
zhangsan 89.8
lisi 90.0

實(shí)例2:

package IT;
import java.util.TreeSet;
public class App
{
  public static void main(String[] args)
  {
   TreeSet<Student> treeSet = new TreeSet<Student>();
   treeSet.add(new Student("wangwu",60.6));
   treeSet.add(new Student("lisi",90.0));
   treeSet.add(new Student("wangting",85.6));
   treeSet.add(new Student("zhangsan",60.6));
   for (Student student : treeSet)
   {
    System.out.println(student);
   }
  }
}
class Student implements Comparable<Student>
{
  public String name;
  public double score;
  public Student(String name,double score)
  {
   this.name = name;
   this.score = score;
  }
  public String toString()
  {
   return this.name+"\t"+this.score;
  }
 public int compareTo(Student obj)
 {
   if(this.score > obj.score)
    return 1;
   else
    return -1;
 }
}

輸出結(jié)果:

zhangsan 60.6
wangwu 60.6
wangting 85.6
lisi 90.0

7、凡是把類對(duì)象放到以哈希表為內(nèi)部存儲(chǔ)結(jié)構(gòu)的容器中,相應(yīng)的類必須要實(shí)現(xiàn)equals方法和hashCode方法,這樣才符合哈希表真實(shí)的邏輯功能.

實(shí)例程序1:(為重寫之前)

package IT;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class App
{
  public static void main(String[] args)
  {
   //Java中自帶的數(shù)據(jù)類型
   System.out.println("先測(cè)試Java中自帶的數(shù)據(jù)類型:");
   HashMap<String, Double> hashMap1 = new HashMap<String,Double>();
   hashMap1.put("zhangsan", 96.0);
   hashMap1.put("lisi", 88.6);
   hashMap1.put("wangwu", 98.6);
   hashMap1.put("wangting", 87.5);
   hashMap1.put("zhangsan", 96.0);
   hashMap1.put("lisi", 88.6);
   hashMap1.put("wangwu", 98.6);
   hashMap1.put("wangting", 87.5);
   Set<String> keySet = hashMap1.keySet();
   Iterator<String> iterator = keySet.iterator();
   while(iterator.hasNext())
   {
    String key = iterator.next();
    System.out.println(key+"\t"+hashMap1.get(key));
   }
   System.out.println("Java中自帶的數(shù)據(jù)類型:相同的對(duì)象會(huì)覆蓋!");
   System.out.println("\n");
   //用戶自定義的數(shù)據(jù)類型:為重寫之前
   System.out.println("測(cè)試用戶自定義的數(shù)據(jù)類型--未重寫兩個(gè)方法之前:");
   HashMap<Student, String> hashMap2 = new HashMap<Student,String>();
   hashMap2.put(new Student("zhangsan",88.8), "beijing");
   hashMap2.put(new Student("lisi",88.8), "beijing");
   hashMap2.put(new Student("wangwu",66.9), "beijing");
   hashMap2.put(new Student("zhangsan",88.8), "beijing");
   hashMap2.put(new Student("lisi",88.8), "beijing");
   hashMap2.put(new Student("wangwu",66.9), "beijing");
   Set<Student> keySet2 = hashMap2.keySet();
   Iterator<Student> iterator2 = keySet2.iterator();
   while(iterator2.hasNext())
   {
    Student key = iterator2.next();
    System.out.println(key+"\t"+hashMap2.get(key));
   }
   System.out.println("如果沒有重寫:導(dǎo)致相同的對(duì)象不會(huì)被覆蓋!");
  }
}
class Student implements Comparable<Student>
{
  public String name;
  public double score;
  public Student(String name,double score)
  {
   this.name = name;
   this.score = score;
  }
  public String toString()
  {
   return this.name+"\t"+this.score;
  }
 public int compareTo(Student obj)
 {
   if(this.score > obj.score)
    return 1;
   else
    return -1;
 }
}

輸出結(jié)果:

先測(cè)試Java中自帶的數(shù)據(jù)類型:
wangting 87.5
wangwu 98.6
lisi 88.6
zhangsan 96.0
Java中自帶的數(shù)據(jù)類型:相同的對(duì)象會(huì)覆蓋!
測(cè)試用戶自定義的數(shù)據(jù)類型--為重寫兩個(gè)方法之前:
zhangsan 88.8 beijing
wangwu 66.9 beijing
lisi 88.8 beijing
wangwu 66.9 beijing
zhangsan 88.8 beijing
lisi 88.8 beijing
如果沒有重寫:導(dǎo)致相同的對(duì)象不會(huì)被覆蓋!

實(shí)例程序2:重寫之后

package IT;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class App
{
  public static void main(String[] args)
  {
   //用戶自定義的數(shù)據(jù)類型:為重寫之后
   System.out.println("測(cè)試用戶自定義的數(shù)據(jù)類型--重寫兩個(gè)方法之后:");
   HashMap<Student, String> hashMap2 = new HashMap<Student,String>();
   hashMap2.put(new Student("zhangsan",88.8), "beijing");
   hashMap2.put(new Student("lisi",88.8), "beijing");
   hashMap2.put(new Student("wangwu",66.9), "beijing");
   hashMap2.put(new Student("zhangsan",88.8), "beijing");
   hashMap2.put(new Student("lisi",88.8), "beijing");
   hashMap2.put(new Student("wangwu",66.9), "beijing");
   Set<Student> keySet2 = hashMap2.keySet();
   Iterator<Student> iterator2 = keySet2.iterator();
   while(iterator2.hasNext())
   {
    Student key = iterator2.next();
    System.out.println(key+"\t"+hashMap2.get(key));
   }
   System.out.println("重寫過后:相同的對(duì)象會(huì)被覆蓋!");
  }
}
class Student implements Comparable<Student>
{
  public String name;
  public double score;
  public Student(String name,double score)
  {
   this.name = name;
   this.score = score;
  }
  public String toString()
  {
   return this.name+"\t"+this.score;
  }
 public int compareTo(Student obj)
 {
   if(this.score > obj.score)
    return 1;
   else
    return -1;
 }
 @Override
 public int hashCode()
 {
  return (int) (this.name.hashCode()*score);//保證相同對(duì)象映射到同一個(gè)索引位置
 }
 @Override
 public boolean equals(Object obj)
 {
  Student cc = (Student)obj;
  return this.name==cc.name&&this.score==cc.score;
 }
}

輸出結(jié)果:

測(cè)試用戶自定義的數(shù)據(jù)類型--重寫兩個(gè)方法之后:
wangwu 66.9 beijing
zhangsan 88.8 beijing
lisi 88.8 beijing
重寫過后:相同的對(duì)象會(huì)被覆蓋!

8、重要的一個(gè)邏輯:邏輯上來講,只要兩個(gè)對(duì)象的內(nèi)容相同,其地址(hashCode()返回值)以及這兩個(gè)對(duì)象就應(yīng)該相同(equals()),

實(shí)例程序(為重寫之前):

package IT;
public class App
{
  public static void main(String[] args)
  {
   //Java中自帶的數(shù)據(jù)類型
   System.out.println(new Integer(1).equals(new Integer(1)));
   System.out.println(new Integer(1).hashCode()==new Integer(1).hashCode());
   System.out.println(new String("zhang").equals(new String("zhang")));
   System.out.println(new String("zhang").hashCode()==new String("zhang").hashCode());
   System.out.println("\n");
   //用戶自定義的數(shù)據(jù)類型
   System.out.println(new Student("zhangsan",98.8).equals(new Student("zhangsan",98.8)));
   System.out.println(new Student("zhangsan",98.8).hashCode());
   System.out.println(new Student("zhangsan",98.8).hashCode());
  }
}
class Student implements Comparable<Student>
{
  public String name;
  public double score;
  public Student(String name,double score)
  {
   this.name = name;
   this.score = score;
  }
  public String toString()
  {
   return this.name+"\t"+this.score;
  }
 public int compareTo(Student obj)
 {
   if(this.score > obj.score)
    return 1;
   else
    return -1;
 }
}

輸出結(jié)果:

true
true
true
true
false
488676694
1211729930

重寫之后:

package IT;
public class App
{
  public static void main(String[] args)
  {
   System.out.println(new Student("zhangsan",98.8).equals(new Student("zhangsan",98.8)));
   System.out.println(new Student("zhangsan",98.8).hashCode());
   System.out.println(new Student("zhangsan",98.8).hashCode());
  }
}
class Student implements Comparable<Student>
{
  public String name;
  public double score;
  public Student(String name,double score)
  {
   this.name = name;
   this.score = score;
  }
  public String toString()
  {
   return this.name+"\t"+this.score;
  }
 public int compareTo(Student obj)
 {
   if(this.score > obj.score)
    return 1;
   else
    return -1;
 }
 @Override
 public int hashCode()
 {
  return (int) (this.name.hashCode()*score);
 }
 @Override
 public boolean equals(Object obj)
 {
  Student cc = (Student)obj;
  return this.name==cc.name&&this.score==cc.score;
 }
}

輸出結(jié)果:

true
-2147483648
-2147483648

上面的5、6、7、8可以歸結(jié)為4個(gè)”凡是”,1個(gè)“邏輯”:

1、凡是把類對(duì)象放到容器中,相應(yīng)的類都應(yīng)該實(shí)現(xiàn)Object類中的toString()方法;
2、凡是需要進(jìn)行比較排序的類都應(yīng)該實(shí)現(xiàn)Comparable接口中的compareTo()方法;凡是把類對(duì)象放到以樹為內(nèi)部結(jié)構(gòu)的容器中都應(yīng)該實(shí)現(xiàn)Comparable接口中的compareTo()方法
3、凡是把類對(duì)象放到以哈希表為內(nèi)部存儲(chǔ)結(jié)構(gòu)的容器中,相應(yīng)的類必須要實(shí)現(xiàn)equals方法和hashCode方法,這樣才符合哈希表真實(shí)的邏輯功能.
4、邏輯上來講,只要兩個(gè)對(duì)象的內(nèi)容相同,其地址(hashCode()返回值)以及這兩個(gè)對(duì)象就應(yīng)該相同(equals())。

9、哈希沖突的相關(guān)概念

本質(zhì)上講就是:hash(對(duì)象1.hashCode())=hash2(對(duì)象2.hashCode()),即第一個(gè)對(duì)象的hashCode()方法返回的哈希碼值帶入到哈希函數(shù)后得到的索引位置與第二個(gè)對(duì)象的hashCode()方法返回的哈希碼值帶入到哈希函數(shù)后得到的索引位置相同,這就是哈希沖突。

最常見的哈希算法是取模法。

下面簡(jiǎn)單講講取模法的計(jì)算過程。

比如:數(shù)組的長(zhǎng)度是5。這時(shí)有一個(gè)數(shù)據(jù)是6。那么如何把這個(gè)6存放到長(zhǎng)度只有5的數(shù)組中呢。按照取模法,計(jì)算6%5,結(jié)果是1,那么就把6放到數(shù)組下標(biāo)是1的位置。那么,7

就應(yīng)該放到2這個(gè)位置。到此位置,哈斯沖突還沒有出現(xiàn)。這時(shí),有個(gè)數(shù)據(jù)是11,按照取模法,11%5=1,也等于1。那么原來數(shù)組下標(biāo)是1的地方已經(jīng)有數(shù)了,是6。這時(shí)又計(jì)算出1這個(gè)位置,那么數(shù)組1這個(gè)位置,就必須儲(chǔ)存兩個(gè)數(shù)了。這時(shí),就叫哈希沖突。沖突之后就要按照順序來存放了。

如果數(shù)據(jù)的分布比較廣泛,而且儲(chǔ)存數(shù)據(jù)的數(shù)組長(zhǎng)度比較大。

那么哈希沖突就比較少。否則沖突是很高的。

10、iterator接口的作用

重要方法:

boolean hasNext():是用來判斷當(dāng)前游標(biāo)(迭代器)的后面是否存在元素,如果存在返回真,否則返回假
Object next():先返回當(dāng)前游標(biāo)右邊的元素,然后游標(biāo)后移一個(gè)位置
void remove():不推薦使用iterator的remove()方法,而是推薦使用容器自帶的remove方法。

實(shí)例程序:

package IT;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class App
{
  public static void main(String[] args)
  {
   HashMap<String, Double> hashMap = new HashMap<String,Double>();
   hashMap.put("zhangsan", 88.6);
   hashMap.put("lisi", 69.0);
   hashMap.put("wanqwu", 100.0);
   hashMap.put("lisi", 69.0);
   Set<String> keySet = hashMap.keySet();
   Iterator<String> iterator = keySet.iterator();
   while(iterator.hasNext())
   {
    String key = iterator.next();//獲取迭代器右邊的元素,同時(shí)右移
    System.out.println(key+hashMap.get(key));
   }
  }
}

思考題:

package IT;
import java.util.TreeSet;
public class App
{
  public static void main(String[] args)
  {
    TreeSet<Student> treeSet = new TreeSet<Student>();
    treeSet.add(new Student("zhangsan",98));
    treeSet.add(new Student("zhangsan",98));
    System.out.println(treeSet.size());
    System.out.println(treeSet);
    //本程序中并沒有重寫equals方法,但是treeSet將識(shí)別出兩個(gè)new Student("zhangsan",98)為相同的,因?yàn)閮?nèi)部數(shù)據(jù)結(jié)構(gòu)嗎?
    System.out.println(new Student("zhangsan",98).equals(new Student("zhangsan",98)));
  }
}
class Student implements Comparable<Object>
{
  public String name;
  public double score;
  public Student(String name,double score)
  {
   this.name = name;
   this.score = score;
  }
  public String toString()
  {
   return name + "\t" + score;
  }
 @Override
 public int compareTo(Object obj)
 {
  Student cc = (Student)obj;
  return (int) (this.score - cc.score);
 }
}

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Jmeter參數(shù)化實(shí)現(xiàn)方法及應(yīng)用實(shí)例

    Jmeter參數(shù)化實(shí)現(xiàn)方法及應(yīng)用實(shí)例

    這篇文章主要介紹了Jmeter參數(shù)化實(shí)現(xiàn)方法及應(yīng)用實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • IntelliJ?IDEA?2023版本創(chuàng)建Spring項(xiàng)目時(shí)Java只能選擇17或21的問題解決方法

    IntelliJ?IDEA?2023版本創(chuàng)建Spring項(xiàng)目時(shí)Java只能選擇17或21的問題解決方法

    spring-boot是一個(gè)基于Java的開源框架,用于快速構(gòu)建生產(chǎn)級(jí)別的應(yīng)用程序,這篇文章主要給大家介紹了關(guān)于IntelliJ?IDEA?2023版本創(chuàng)建Spring項(xiàng)目時(shí)Java只能選擇17或21的問題解決方法,需要的朋友可以參考下
    2024-07-07
  • JSR303校驗(yàn)注解和自定義校驗(yàn)注解的使用

    JSR303校驗(yàn)注解和自定義校驗(yàn)注解的使用

    這篇文章主要介紹了JSR303校驗(yàn)注解和自定義校驗(yàn)注解的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 基于java socket實(shí)現(xiàn) 聊天小程序

    基于java socket實(shí)現(xiàn) 聊天小程序

    這篇文章主要介紹了基于java socket實(shí)現(xiàn) 聊天小程序,代碼分為服務(wù)器和客戶端,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java分析講解序列化與字典功能的序列化

    Java分析講解序列化與字典功能的序列化

    在項(xiàng)目開發(fā)地過程中經(jīng)常會(huì)用到字典存儲(chǔ)某些可配置的值,但相應(yīng)的也會(huì)給開發(fā)帶來復(fù)雜度,比如數(shù)據(jù)庫(kù)存儲(chǔ)的值為字典值:0,1,2,3這種類型的值,但是前端頁面卻需要展示為 啟動(dòng)、禁用、刪除 等中文狀態(tài),下面我們來看兩種解決方案
    2022-06-06
  • java中functional interface的分類和使用詳解

    java中functional interface的分類和使用詳解

    這篇文章主要介紹了java中functional interface的分類和使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Java實(shí)現(xiàn)終止線程池中正在運(yùn)行的定時(shí)任務(wù)

    Java實(shí)現(xiàn)終止線程池中正在運(yùn)行的定時(shí)任務(wù)

    本篇文章給大家分享了JAVA中實(shí)現(xiàn)終止線程池中正在運(yùn)行的定時(shí)任務(wù)的具體步驟和方法,有需要的朋友跟著學(xué)習(xí)下。
    2018-05-05
  • Java中如何用Stream分組并求各組數(shù)量

    Java中如何用Stream分組并求各組數(shù)量

    這篇文章主要給大家介紹了關(guān)于Java中如何用Stream分組并求各組數(shù)量的相關(guān)資料,文中通過實(shí)例代碼,對(duì)大家學(xué)習(xí)或者Java具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • Java中WeakHashMap的弱鍵回收機(jī)制

    Java中WeakHashMap的弱鍵回收機(jī)制

    這篇文章主要介紹了Java中WeakHashMap的弱鍵回收機(jī)制,WeakHashMap繼承AbstractMap,實(shí)現(xiàn)了Map接口,和HashMap一樣,WeakHashMap也是一個(gè)散列表,它存儲(chǔ)的內(nèi)容也是鍵值對(duì)(key-value)映射,而且鍵和值都可以是null,需要的朋友可以參考下
    2023-09-09
  • Java File類的簡(jiǎn)單使用教程(創(chuàng)建、刪除、遍歷與判斷是否存在等)

    Java File類的簡(jiǎn)單使用教程(創(chuàng)建、刪除、遍歷與判斷是否存在等)

    這篇文章主要給大家介紹了關(guān)于Java File類的簡(jiǎn)單使用(創(chuàng)建、刪除、遍歷與判斷是否存在等)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12

最新評(píng)論