コンテナ内の要素のソートSet,Map,List

4020 ワード


package collectionSort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Map.Entry;

/*
 * Map :       
 * Set :                  
 * List:                  
 * 
 */

public class HashMapSort {
    private Map<String , Double> hashMap ;
    private Set<String> set;
    private List<String> list;
    
    public HashMapSort(){
	this.hashMap = new HashMap<String,Double>();
	this.hashMap.put("1", 2.0);
	this.hashMap.put("2", 1.0);
	this.hashMap.put("3", 3.0);
	this.hashMap.put("4", 5.0);
	this.hashMap.put("5", 33.0);
	this.hashMap.put("6", 344.0);
	this.hashMap.put("7", 36.0);
	this.hashMap.put("8", 33.0);
	this.hashMap.put("9", 322.0);
	
	
	this.set = new HashSet<String>();
	this.set.add("");
	this.set.add("#");
	this.set.add("##");
	this.set.add("###");
	this.set.add("####");
	this.set.add("#####");
	this.set.add("######");
	this.set.add("#######");
	this.set.add("########");
	
	
	this.list = new ArrayList<String>();
	this.list.add("");
	this.list.add("#");
	this.list.add("##");
	this.list.add("###");
	this.list.add("####");
	this.list.add("#####");
	this.list.add("######");
	this.list.add("#######");
	this.list.add("########");
	
    }
    
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
	HashMapSort hashMapSort = new HashMapSort();
	Set<Entry<String,Double>> entrySet = hashMapSort.hashMap.entrySet();
	Set<Entry<String,Double>> treeSet = new TreeSet(new Comparator<Entry<String,Double>>(){

	    public int compare( Entry<String , Double> o1, Entry<String,Double> o2) {
		return (int) (o2.getValue()-o1.getValue());
	    }
	    
	});
	
	
	for(Entry<String , Double> entry: entrySet){
	    treeSet.add(entry);
	}
	
	
	System.out.println(treeSet);
	
	
	System.out.println("+++++++++++++++++++++++++++++++++++++++   +++++++++++++++++++++++++++++++++++");
	Set<String> treeSet1 = new TreeSet(new Comparator<String>(){

	    public int compare(String o1, String o2) {
		
		return o1.length()-o2.length();
	    }
	    
	});
	
	for(String s: hashMapSort.set){
	    treeSet1.add(s);
	}
	System.out.println(treeSet1);
	
	System.out.println("+++++++++++++++++++++++++++++++++++++++   +++++++++++++++++++++++++++++++++++");
	
	
	Collections.sort(hashMapSort.list,new Comparator<String>(){

	    public int compare(String o1, String o2) {
		return o2.length() - o1.length();
	    }
	    
	});
	
	System.out.println(hashMapSort.list);
    }
}



実行結果:
参照
[6=344.0, 9=322.0, 7=36.0, 5=33.0, 4=5.0, 3=3.0, 1=2.0, 2=1.0]
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[, #, ##, ###, ####, #####, ######, #######, ########]
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[########, #######, ######, #####, ####, ###, ##, #, ]