hashCode與equals Set 集合排序

package ZHANG.API;
import java.util.Enumeration;
import java.util.Hashtable;


public class MyKey {

	private String name = null;
	private int age = 0;
	
	public MyKey(String string, int i) {
		name = string;
		age = i;
	}

	public int hashCode() {
		return name.hashCode()+age;
	}

	public boolean equals(Object obj) {
		if(obj instanceof MyKey){
			MyKey objtem = (MyKey)obj;
			if(name.equals(objtem.name) && age==objtem.age){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	public String toString(){
		return name +","+ age;
	}

	public static void main(String[] args) {
		Hashtable ht = new Hashtable();
		ht.put(new MyKey("yw",44), new Integer(1));
		ht.put(new MyKey("yws",23), new Integer(4));
		ht.put(new MyKey("yw",23), new Integer(1));
		
		Enumeration e = ht.keys();
		while(e.hasMoreElements()){
			MyKey key = (MyKey) e.nextElement();
			System.out.println(key+"->"+ht.get(key));
		}
	}

}

 

//   Set ....與 集合排序java

public class TestFiles {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*File file = new File("d:\\javatest");
		ToSearchFiles(file);*/
		
		ArrayList<String> set = ReadFile2("d:\\javatest", "large");
		Iterator<String> iter = set.iterator();
		while(iter.hasNext()){
			System.out.println(iter.next());
		}
	}

	private static void ToSearchFiles(File file) {
		if (file.isFile()) {
			try {
				if (file.getName().indexOf("eeeeee") > -1) {
					System.out.println(file.getName() + "f");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File f : files) {
				System.out.println(files.length);
				ToSearchFiles(f);
				System.out.println(f.getPath());
			}
		}
	}
	
	public static TreeSet<String> ReadFile(String path){
		TreeSet<String> list = new TreeSet<String>(new Comparator<String>() {
			public int compare(String o1, String o2) {
				return o2.compareTo(o1);
			}
		});
		File fd = new File(path);
		if(fd.exists()){
			File[] fs = fd.listFiles();
			for(File f: fs){
				if(f.isFile()){
					if(f.getName().substring(f.getName().lastIndexOf("_")+1).equals("small.jpg")){
						list.add(f.getAbsolutePath());
					}
				}
			}
		}
		return list;
	}
	public static ArrayList<String> ReadFile2(String path, String type){
		ArrayList list = new ArrayList();
		File fd = new File(path);
		if(fd.exists()){
			File[] fs = fd.listFiles();
			for(File f: fs){
				if(f.isFile()){
					String fileName = f.getName();
					if(fileName.endsWith("l.jpg") && type.equals("default")){
						list.add(f.getAbsolutePath());
					} else if(!fileName.endsWith("l.jpg") && fileName.endsWith(".jpg") && type.equals("large")) {
						list.add(f.getAbsolutePath());
					}
				}
			}
		}
		Collections.sort(list);
		//Collections.reverse(list);
		return list;
	}
相關文章
相關標籤/搜索