Java集合synchronizedSortedMap()方法



描述

Java Collections synchronizedSortedMap() 方法用於返回一個由指定排序對映支援的同步排序(執行緒安全)對映。

宣告

以下是java.util.Collections.synchronizedSortedMap() 方法的宣告。

public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m)

引數

m − 這是要“包裝”到同步排序對映中的排序對映。

返回值

  • 方法呼叫返回指定排序對映的同步排序檢視。

異常

從String, Integer型別的非同步排序對映獲取同步排序對映示例

以下示例演示了Java Collection synchronizedSortedMap(SortedMap) 方法的使用。我們建立了一個String和Integer型別的SortedMap物件。添加了一些條目,然後使用synchronizedSortedMap(SortedMap) 方法,我們檢索到了排序對映的同步排序版本並列印了該對映。

package com.tutorialspoint;

import java.util.Collections;
import java.util.TreeMap;
import java.util.SortedMap;

public class CollectionsDemo {
   public static void main(String[] args) {
      
      // create map
      SortedMap<String,Integer> map = new TreeMap<String,Integer>();

      // populate the map
      map.put("1",1); 
      map.put("2",2);
      map.put("3",3);

      // create a synchronized sorted map
      SortedMap<String,Integer> synmap = Collections.synchronizedSortedMap(map);

      System.out.println("Synchronized Sorted map is :"+synmap);
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果。

Synchronized Sorted map is :{1=1, 2=2, 3=3}

從String, String型別的非同步排序對映獲取同步排序對映示例

以下示例演示了Java Collection synchronizedSortedMap(SortedMap) 方法的使用。我們建立了一個String和String型別的SortedMap物件。添加了一些條目,然後使用synchronizedSortedMap(SortedMap) 方法,我們檢索到了排序對映的同步排序版本並列印了該對映。

package com.tutorialspoint;

import java.util.Collections;
import java.util.TreeMap;
import java.util.SortedMap;

public class CollectionsDemo {
   public static void main(String[] args) {
      
      // create map
      SortedMap<String,String> map = new TreeMap<String,String>();

      // populate the map
      map.put("1","TP"); 
      map.put("2","IS");
      map.put("3","BEST");

      // create a synchronized sorted map
      SortedMap<String,String> synmap = Collections.synchronizedSortedMap(map);

      System.out.println("Synchronized Sorted map is :"+synmap);
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果。

Synchronized Sorted map is :{1=TP, 2=IS, 3=BEST}

從String, Object型別的非同步排序對映獲取同步排序對映示例

以下示例演示了Java Collection synchronizedSortedMap(SortedMap) 方法的使用。我們建立了一個String和Student物件型別的SortedMap物件。添加了一些條目,然後使用synchronizedSortedMap(SortedMap) 方法,我們檢索到了排序對映的同步排序版本並列印了該對映。

package com.tutorialspoint;

import java.util.Collections;
import java.util.TreeMap;
import java.util.SortedMap;

public class CollectionsDemo {
   public static void main(String[] args) {
      
      // create map
      SortedMap<String,Student> map = new TreeMap<String,Student>();

      // populate the map
      map.put("1",new Student(1, "Julie")); 
      map.put("2",new Student(2, "Robert"));
      map.put("3",new Student(3, "Adam"));

      // create a synchronized sorted map
      SortedMap<String,Student> synmap = Collections.synchronizedSortedMap(map);

      System.out.println("Synchronized Sorted map is :"+synmap);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行上面的程式,這將產生以下結果。

Synchronized Sorted map is :{1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ]}
java_util_collections.htm
廣告