15.TreeSet对元素的配需方式,让元素自身具备比较性,元素就需要实现Comparable接口,覆盖CompareTo(Object obj)方法。如果元素本身不具备比较方法,则可以定义一个类实现Comparable接口,传递给TreeSet作为参数。示例:
class HashDemo implements Comparable{
private String name;
private int ID;
public HashDemo(String name, int ID) {
this.name = name;
this.ID = ID;
}
@Override
public int hashCode() {
return this.name.hashCode() + this.ID;
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (!(obj instanceof HashDemo))
throw new ClassCastException("类型错误");
HashDemo objDemo = (HashDemo) obj;
return this.name.equals(objDemo.name)
&& this.ID == objDemo.ID;
}
@Override
public String toString() {
return this.name + "\t"
+ ID + "\t"
+ this.getClass().getName();
}
@Override
public int compareTo(Object o) {
HashDemo hd = (HashDemo)o;
if(this.name.compareTo(hd.name)==0)
return this.ID-hd.ID;
else
return this.name.compareTo(hd.name);
}
}
public class Demo {
public static void main(String[] args) {
HashDemo h1
= new HashDemo("caizhanqi", 2012241001);
HashDemo h2
= new HashDemo("chenweirong", 2012241002);
HashDemo h3
= new HashDemo("chenzheng", 2012241003);
HashDemo h4
= new HashDemo("cuijunbiao", 2012241005);
Set<HashDemo> ts = new TreeSet<HashDemo>();
ts.add(h2);
ts.add(h1);
ts.add(h4);
ts.add(h3);
ts.add(h1);
ts.add(h4);
for(Iterator<HashDemo>
it = ts.iterator();it.hasNext();){
System.out.println(it.next());
}
}
}
16.示例传递比较方法:
class MyComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
HashDemo hd1 = (HashDemo) o1;
HashDemo hd2 = (HashDemo) o2;
if(hd1.getID()-hd2.getID()==0)
return hd1.getName().compareTo(hd2.getName());
else
return hd1.getID()-hd2.getID();
}
}
public class Demo {
public static void main(String[] args) {
HashDemo h1
= new HashDemo("caizhanqi", 2012241001);
HashDemo h11
= new HashDemo("sss", 2012241002);
HashDemo h2
= new HashDemo("chenweirong", 2012241002);
HashDemo h3
= new HashDemo("chenzheng", 2012241003);
HashDemo h4
= new HashDemo("cuijunbiao", 2012241005);
//Set<HashDemo> ts = new TreeSet<HashDemo>();
Set<HashDemo> ts
= new TreeSet<HashDemo>(new MyComparator());
ts.add(h2);
ts.add(h1);
ts.add(h4);
ts.add(h3);
ts.add(h11);
ts.add(h4);
for (Iterator<HashDemo>
it = ts.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}
}
17.Map:Key-Value
|–Hashtable:底层是哈希表,不允许null键null值,线程同步,效率低。
|–HashMap:底层是哈希表,允许null键null值,线程不同步,效率高。
|–TreeMap:底层是二叉树,线程不同步,可以用于给Map集合中的key进行排序。
18.Map示例:
HashDemo h1 = new HashDemo("caizhanqi", 2012241001);
HashDemo h11 = new HashDemo("sss", 2012241002);
HashDemo h2 = new HashDemo("chenweirong", 2012241002);
HashDemo h3 = new HashDemo("chenzheng", 2012241003);
HashDemo h4 = new HashDemo("cuijunbiao", 2012241005);
Map<HashDemo, Integer> map
= new HashMap<HashDemo, Integer>();
// 添加
map.put(h1, 1);
map.put(h2, 2);
map.put(h11, 1);
map.put(h3, 3);
map.put(h4, 4);
show(map);
// 删除
map.remove(h11);
// map.clear();
show(map);
// 判断
System.out.println(map.containsKey(h2));
System.out.println(map.containsValue(22));
// 获取
System.out.println(map.get(h2));
show(map);
System.out.println(map.size());
//迭代输出
Set<HashDemo> keySet = map.keySet();
for(Iterator<HashDemo> it = keySet.iterator();it.hasNext();){
HashDemo key = it.next();
Integer value = map.get(key);
System.out.println(key+"="+value);
}
//另一种输出
Set<Map.Entry<HashDemo, Integer>> mapEntry = map.entrySet();
for (Iterator<Map.Entry<HashDemo, Integer>>
it = mapEntry.iterator(); it.hasNext();) {
Map.Entry<HashDemo, Integer> en = it.next();
HashDemo hd = en.getKey();
Integer num = en.getValue();
System.out.println(hd + "=" + num);
}
//输出所有值
Collection<Integer> values = map.values();
for(Iterator<Integer> it = values.iterator();it.hasNext();)
System.out.println(it.next());
19.输出字符的个数:
// 输出字符个数
String s = "fjdsnfslmzhvkahvzzzaafspgf";
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < s.length(); i++) {
String c = "" + s.charAt(i);
if (map.containsKey(c)) {
Integer value = map.get(c);
value++;
map.remove(c);
map.put(c, value);
continue;
}
map.put(c, 1);
}
for (it = map.entrySet()
.iterator(); it.hasNext();) {
Map.Entry<String, Integer> entrySet = it.next();
System.out.print(entrySet.getKey()
+"("+entrySet.getValue()+")");
}
20.Collections:集合框架的工具类,排序示例:
class myComByLength implements Comparator {
@Override
public int compare(Object o1, Object o2) {
String s1 = (String) o1;
String s2 = (String) o2;
if(s1.length()-s2.length()==0)
return s1.compareTo(s2);
else
return s1.length()-s2.length();
}
}
public class newDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("1");
list.add("121");
list.add("112");
list.add("113");
list.add("1111");
list.add("132");
list.add("14");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
//Collections.sort(list, new myComByLength());
//System.out.println(list);
System.out.println(Collections.binarySearch(list, "121"));
mySort(list, new myComByLength());
System.out.println(list);
System.out.println(Collections.binarySearch(list, "112", new myComByLength()));
System.out.println("Max="
+Collections.max(list,new myComByLength()));
System.out.println("Min="
+Collections.min(list,new myComByLength()));
System.out.println("Max="
+Collections.max(list,null));
System.out.println("Max="
+Collections.min(list,null));
}
public static <T> void mySort(List<T> list,Comparator<? super T> myComp){
for (int i = 0; i < list.size()-1; i++) {
for (int j = i+1; j < list.size(); j++) {
if(myComp.compare(list.get(i), list.get(j))>0)
Collections.swap(list, i, j);
}
}
}
}
21.顺序反转,元素全替换,随机置换元素位置。
List<String> list = new ArrayList<String>();
list.add("1");
list.add("121");
list.add("112");
list.add("113");
list.add("1111");
list.add("132");
list.add("14");
Collections.sort(list);
System.out.println(list);
Collections.sort(list, Collections.reverseOrder());
System.out.println(list);
Collections.sort(list, new myComByLength());
System.out.println(list);
Collections.sort(list, Collections.reverseOrder(new myComByLength()));
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
Collections.replaceAll(list, "1", "1111111111");
System.out.println(list);
Collections.shuffle(list);
System.out.println(list);
22.线程同步安全的方法:
class myList extends ArrayList{
private List<String> list;
public List<String> myList(List<String> list) {
return this.list;
}
@Override
public boolean add(Object obj) {
synchronized(this){
return list.add((String) obj);
}
}
@Override
public boolean remove(Object o) {
synchronized(this){
return list.remove((String)o);
}
}
}
public class newDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list = new myList().myList(list);
Collections.synchronizedList(list);
}
}
23.Arrays:集合框架的工具类,将数组作为集合操作。PS:数组长度是固定的,不能更改,否则会报异常UnsupportedOperationException,而数组转集合,如果数组元素是对象,直接将数组中的元素进行集合存储,如果是基础数据类型,则将数组本身作为元素存储。
Integer[] arr = {1,2,3,4,5,6};
System.out.println(Arrays.toString(arr));
//将数组转换为集合
List<Integer> list = Arrays.asList(arr);
System.out.println(list);
//基础数据类型数组
int[] arrInt = {1,2,3};
List<int[]> listInt = Arrays.asList(arrInt);
System.out.println(listInt);
System.out.println(listInt.get(0)[2]);
//集合转数组
List<String> listDemo = new ArrayList<String>();
listDemo.add("1");
listDemo.add("1");
listDemo.add("2");
listDemo.add("3");
listDemo.add("4");
listDemo.add("11");
Object[] str1 = listDemo.toArray();
System.out.println(Arrays.toString(str1));
String[] str2 = listDemo.toArray(new String[listDemo.size()]);
System.out.println(Arrays.toString(str2));