黑马程序员技术交流社区
标题:
TreeSet集合的排序问题(包含重复奥)?
[打印本页]
作者:
刘海源
时间:
2012-7-31 11:15
标题:
TreeSet集合的排序问题(包含重复奥)?
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapDemo {
public static void main(String[] args) {
// demo();
demo2();
// demo3();
}
@SuppressWarnings("unchecked")
public static void demo() {
Map map = new HashMap();
map.put("xt001", "zhangsan");
map.put("xt002", "lisi");
map.put("xt003", "wangwu");
System.out.println(map);
System.out.println(map.get("xt001"));
System.out.println(map.get("xt002"));
System.out.println(map.get("xt003"));
}
public static void demo2() {
Map<String, String> map = new HashMap<String, String>();
map.put("xt001", "zhangsan");
map.put("xt002", "lisi");
map.put("xt003", "wangwu");
System.out.println(map);
System.out.println("***********************************");
System.out.println(map.get("xt001"));
System.out.println(map.get("xt002"));
System.out.println(map.get("xt003"));
System.out.println("***********************************");
// Set<Map.Entry<K,V>> entrySet()
Set<Map.Entry<String, String>> set = map.entrySet();
Iterator<Map.Entry<String, String>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next(); // 接口对应的子类对象
System.out.println(entry.getKey() + " " + entry.getValue());
}
System.out.println("***********************************");
Set<String> keys = map.keySet();
Iterator<String> it2 = keys.iterator();
while (it2.hasNext()) {
String key = it2.next();
System.out.println(key + " " + map.get(key));
}
}
public static void demo3() {
Map<String, Student> map = new HashMap<String, Student>();
Student s1 = new Student();
s1.setName("zhangsan");
s1.setAge(20);
Student s2 = new Student();
s2.setName("lisi");
s2.setAge(30);
Student s3 = new Student();
s3.setName("wangwu");
s3.setAge(40);
map.put("xt1", s1);
map.put("xt2", s2);
map.put("xt3", s3);
System.out.println(map);
Student s = map.get("xt2");
System.out.println(s.getName() + " " + s.getAge());
System.out.println("************************************");
Set<Map.Entry<String, Student>> set = map.entrySet();
Iterator<Map.Entry<String, Student>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, Student> entry = it.next(); // 接口对应的子类对象
Student stu = entry.getValue();
System.out.println(entry.getKey() + " " + stu.getName() + " "
+ stu.getAge());
}
System.out.println("************************************");
// 留着自己写
}
}
想要对字符串数组中的元素进行长度排序但是的包含重复的,是否可以先用TreeSet集合来存储呢???
作者:
周坤
时间:
2012-7-31 14:45
TreeSet恐怕不行,感觉还是用list
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class zifuchuan
{
@SuppressWarnings("unchecked")
public static void main(String[]args)
{
String[] newString={"abc","z","abc","d"};
List list= Arrays.asList(newString);
System.out.println(list);
Collections.sort(list,new SortComparator());
System.out.println(list);
String[] String2=(String[]) list.toArray(new String [0]);
System.out.print("[");
for(int i=0;i<String2.length;i++)
{
System.out.print(String2[i]+" ");
}
System.out.println("]");
}
}
class SortComparator implements Comparator<String>
{
public int compare(String s1,String s2)
{
if (s1.length()>s2.length())
return 1;
if (s1.length()<s2.length())
return -1;
return s1.compareTo(s2);
}
}
复制代码
TreeSet恐怕不行,一般用这种方法
作者:
洪灿阳
时间:
2012-12-2 22:54
完全是可行的,只要定义一个类实现comparator接口,覆盖compare方法,然后将该类对象作为参数传递给treeset集合的构造方法就行了。下面是代码还有具体的我的思想讲解,你看看这样行不。
package cn.itcast.Test;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetDeom2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] strs={"dg","ol","aab","ab","ca","aba","aab","dg","dh","dg","ab"};
Set<String> set=new TreeSet<String>(new CompareByLength());
//set.add(e);
for(String str:strs){
set.add(str);
}
System.out.println("长度是"+strs.length);
int count=0;
Iterator<String> it = set.iterator();
while(it.hasNext()){
String value=it.next();
count++;
System.out.println("第"+count+"个是"+value);
}
}
}
[code]package cn.itcast.Test;
import java.util.Comparator;
public class CompareByLength implements Comparator {
@Override
public int compare(Object o1,Object o2) {
String s1=(String)o1;
String s2=(String)o2;
return s1.length()>s2.length()?1:s1.length()<s2.length()?-1:compareByName(s1,s2);
}
private int compareByName(String s1, String s2) {
int result=s1.compareTo(s2);
if(result==0)
result=-1;
/*set集合不储存相同元素由于compare的返回值是0,
* 集合内部就会自动不让这个相同元素存进去
但是如果把它的返回值改动的话,依然就可以存进去。
我定义为-1是由于进行比较的话如果比set中的元素下的话就会存到它的后面。*/
System.out.println("s1="+s1+" "+"s2="+s2+" "+result);
return result;
}
}
复制代码
[/code]
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2