Java中的Treemap例子

时间:2020-02-23 14:35:38  来源:igfitidea点击:

Treemap类实现映射类似于HashMap。

关于Treemap的一些重要知识点:

  • Treemap实现映射界面并扩展HashMap类。
  • Treemap使用基于红色的黑树的NavigableMap实现。
  • Treemap是排序的collection,并将其元素存储在自然排序的键中。
  • 我们希望放入Triemap的键必须实现可调客界面,或者我们可以使用比较器进行自定义排序

例子:

package org.igi.theitroad;
 
import java.util.TreeMap;
 
public class TreeMapMain {
 
	public static void main(String args[])
	{
		//TreeMap with Country as key and capital as value
		//TreeMap stores elements in natural ordering of keys. 
		TreeMap<String,String> countryCapitalMap=new TreeMap<String,String>();
		countryCapitalMap.put("Netherlands","Amsterdam");
		countryCapitalMap.put("Japan","Tokyo");
		countryCapitalMap.put("France","Paris");
		countryCapitalMap.put("Russia","Moscow");
 
		System.out.println("-----------------------------");
		//Iterating TreeMap Using keySet() and for each loop
		System.out.println("Iterating TreeMap Using keySet() and for each loop");
		for (String countryKey:countryCapitalMap.keySet()) {
			System.out.println("Country:"+ countryKey +" and  Capital:"+countryCapitalMap.get(countryKey));
 
		}
		System.out.println("-----------------------------");
	}
 
}

运行上面的程序时,我们将获取以下输出:

----------------------------
Iterating TreeMap Using keySet() and for each loop
Country:France and  Capital:Paris
Country:Netherlands and  Capital:Amsterdam
Country:Japan and  Capital:Tokyo
Country:Russia and  Capital:Moscow
----------------------------

正如我们所看到的,它按键值(国家)的升序排序

如果我们想要自定义排序而不是自然排序,该怎么办:

如果我们想要自定义排序,则可以使用Treemap构造函数下面。
我们可以定义自己的比较器。

TreeMap countryCapitalMap=new TreeMap(Comparator comp);

示例:创建Country.java如下

package org.igi.theitroad;  
public class Country {  
 
	String name;  
	long population;  
 
	public Country(String name, long population) {  
		super();  
		this.name = name;  
		this.population = population;  
	}  
	public String getName() {  
		return name;  
	}  
	public void setName(String name) {  
		this.name = name;  
	}  
	public long getPopulation() {  
		return population;  
	}  
	public void setPopulation(long population) {  
		this.population = population;  
	}  
 
}

创建TriemapCompMain如下所示:

package org.igi.theitroad;
 
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeMap;
 
public class TreeMapCompMain {
 
	public static void main(String[] args)
	{
		Country Netherlands=new Country("Netherlands",1000);  
		Country japan=new Country("Japan",10000);  
 
		Country france=new Country("France",2000);  
		Country russia=new Country("Russia",20000);  
 
		Comparator comparator=new Comparator() {
 
			@Override
			public int compare(Country o1, Country o2) {
				return o2.getName().compareTo(o1.getName());
			}
		};
 
		System.out.println("Sorting TreeMap in reverse order of country name");
		TreeMap<Country, String> countryCapitalMap=new TreeMap<Country,String>(comparator);  
		countryCapitalMap.put(Netherlands,"Amsterdam");  
		countryCapitalMap.put(japan,"Tokyo");  
		countryCapitalMap.put(france,"Paris");  
		countryCapitalMap.put(russia,"Moscow");  
 
		Iterator countryCapitalIter=countryCapitalMap.keySet().iterator();//put debug point at this line  
		while(countryCapitalIter.hasNext())  
		{  
			Country countryObj=countryCapitalIter.next();  
			String capital=countryCapitalMap.get(countryObj);  
			System.out.println(countryObj.getName()+"----"+capital);  
		}  
	}  
 
}

运行上面的程序时,我们将得到以下输出:

Sorting TreeMap in reverse order of country name
Russia----Moscow
Japan----Tokyo
Netherlands----Amsterdam
France----Paris

我们可以将HashMap传递给Treemap的构造函数,以将其排列在键上

TreeMap countryCapitalMap=new TreeMap(Map hm);

通过将HashMap传递给Treemap的构造函数,我们可以对Treemap进行排序。

示例:创建Country.java如下。
它应该实现可比较的界面

package org.igi.theitroad;  
public class Country implements Comparable {  
 
	String name;  
	long population;  
 
	public Country(String name, long population) {  
		super();  
		this.name = name;  
		this.population = population;  
	}  
	public String getName() {  
		return name;  
	}  
	public void setName(String name) {  
		this.name = name;  
	}  
	public long getPopulation() {  
		return population;  
	}  
	public void setPopulation(long population) {  
		this.population = population;  
	}
	@Override
	public int compareTo(Object o) {
		Country country=(Country) o;
		return this.getName().compareTo(country.getName());
	}  
 
}

创建TReeMAPCOMPMAIN.java如下:

package org.igi.theitroad;
 
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;
 
public class TreeMapCompMain {
 
	public static void main(String[] args)
	{
		Country Netherlands=new Country("Netherlands",1000);  
		Country japan=new Country("Japan",10000);  
 
		Country france=new Country("France",2000);  
		Country russia=new Country("Russia",20000);  
 
		HashMap<Country, String> countryCapitalMap=new HashMap<Country,String>();  
		countryCapitalMap.put(Netherlands,"Amsterdam");  
		countryCapitalMap.put(japan,"Tokyo");  
		countryCapitalMap.put(france,"Paris");  
		countryCapitalMap.put(russia,"Moscow");  
 
		System.out.println("Sorting HashMap by passing it to TreeMap constructor");
		TreeMap<Country,String> sortedTreeMapCountryCapital=new  TreeMap<Country,String> (countryCapitalMap);
		Iterator countryCapitalIter=sortedTreeMapCountryCapital.keySet().iterator();//put debug point at this line  
		while(countryCapitalIter.hasNext())  
		{  
			Country countryObj=countryCapitalIter.next();  
			String capital=countryCapitalMap.get(countryObj);  
			System.out.println(countryObj.getName()+"----"+capital);  
		}  
	}  
 
}

运行程序时,我们将得到以下输出:

Sorting HashMap by passing it to TreeMap constructor
France----Paris
Netherlands----Amsterdam
Japan----Tokyo
Russia----Moscow