package java基础;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class TestCollection {
public static void main(String[] args) {
//Set 无序、无重复
Set<String> set = new HashSet<String>();//new一个HashSet用于存放String类型的对象
String a = new String("haha");
String b = new String("haha");
String c = new String("haha ccc");
set.add(a);
set.add(b);
set.add(c);
for(String s:set) {//该for循环使用jdk1.5的增强for循环,用于遍历数据很方便
System.out.println(s);//无顺序的
/**
* haha ccc
haha
*/
}
System.out.println(set.size());//打印2
//Set 无序、无重复
Fish f1 = new Fish("f1");
Fish f2 = new Fish("f1");
Set<Fish> set1 = new HashSet<Fish>();
set1.add(f1);
set1.add(f2);
for(Fish s:set1) {
System.out.println(s);//f1换行f1
}
System.out.println(set1.size());//打印2
//Set 无序、无重复 重复的对象存放不进去
smallFish f3 = new smallFish("smallFish");
smallFish f4 = new smallFish("smallFish");
Set<smallFish> set2 = new HashSet<smallFish>();
set2.add(f3);
set2.add(f4);
for(smallFish s:set2) {
System.out.println(s);//smallFish
}
System.out.println(set2.size());//打印1
System.out.println("//List 有序,有重复");
//List 有序,有重复
List<String> list = new ArrayList<String>();
list.add(a);
list.add(b);
list.add(c);
list.add(a);
for(String h:list) {//该for循环使用jdk1.5的增强for循环,用于遍历数据很方便
System.out.println(h);
/**
haha
haha
haha ccc
haha
*/
}
System.out.println("//测试toArray()");
//String[] array = (String[])list.toArray();//不能强制转换
Object[] array = list.toArray();
for(int i=0; i<array.length; i++) {//这里没有使用增强for循环,但是能访问到下标
System.out.println((String)array[i]);
/**
haha
haha
haha ccc
haha
*/
}
//Map的不存储重复的key,无序
System.out.println("//Map接口定义了存储key --value映射对的方法");
Fish f5 = new Fish("f5");
Fish f6 = new Fish("f5");
Fish f7 = new Fish("f7");
Map<String,Fish> map = new HashMap<String,Fish>();
map.put("1", f5);
map.put("1", f5);
map.put("1", f6);
map.put("2", f7);
//keySet方式迭代
for(String m:map.keySet()) {//该for循环使用jdk1.5的增强for循环,用于遍历数据很方便
System.out.println("key="+m);
System.out.println("value="+map.get(m));
/**
* //Map接口定义了存储key --value映射对的方法
key=2
value=f7
key=1
value=f5
*/
}
System.out.println(map.size());//2
System.out.println("//entrySet() 方式迭代");
/**
* entrySet() 方式迭代
返回此映射所包含的映射关系的 Set 视图。
*/
for(Entry<String, Fish> m :map.entrySet()) {
System.out.println(m);
/**
* 2=f7
1=f5
*/
}
System.out.println("//entrySet() 方式迭代 key value");
for(Entry<String, Fish> m :map.entrySet()) {
System.out.println(m.getKey()+"="+m.getValue());
/**
* 2=f7
1=f5
*/
}
System.out.println("//测试remove()");
map.remove("2");
for(Entry<String, Fish> m :map.entrySet()) {
System.out.println(m.getKey()+"="+m.getValue());
/**
1=f5
*/
}
System.out.println("//测试contains()");
boolean exists = map.containsKey("2");
if(!exists) {
map.put("2",new Fish("Lity"));
}
for(Entry<String, Fish> m :map.entrySet()) {
System.out.println(m.getKey()+"="+m.getValue());
/**
//测试contains()
2=Lity
1=f5
*/
}
System.out.println("//测试isEmpty()");
if(!map.isEmpty()){
System.out.println("如果不空,则打印大小:"+map.size());
}
System.out.println("//测试clear()");
if(!map.isEmpty()){
map.clear();
System.out.println("如果不空,则清空,clear(),再看看大小:"+map.size());
/**
//测试clear()
如果不空,则清空,clear(),再看看大小:0
*/
}
System.out.println("//测试Collections的基于List的算法");
List<String> l1 = new LinkedList<String>();
List<String> l2 = new LinkedList<String>();
for(int i=1; i<=9; i++) {
l1.add("a"+i);
}
System.out.println("直接打印");
System.out.println(l1);
System.out.println("随机排序算法");
Collections.shuffle(l1);
System.out.println(l1);
System.out.println("逆序算法");
Collections.reverse(l1);
System.out.println(l1);
l2.addAll(l1);
System.out.println("addAll()");
System.out.println(l2);
System.out.println("Collections排序算法");
Collections.sort(l2);//l2里的对象是String对象,String类实现了Comparable接口,所以可以排序
System.out.println(l2);
System.out.println("折半算法,查找");
int i = Collections.binarySearch(l2,"a5");
System.out.println("集合中的序号:"+i);
/**
* //测试Collections的基于List的算法
直接打印
[a1, a2, a3, a4, a5, a6, a7, a8, a9]
随机排序算法
[a2, a3, a9, a1, a6, a7, a4, a8, a5]
逆序算法
[a5, a8, a4, a7, a6, a1, a9, a3, a2]
addAll()
[a5, a8, a4, a7, a6, a1, a9, a3, a2]
Collections排序算法
[a1, a2, a3, a4, a5, a6, a7, a8, a9]
折半算法,查找
集合中的序号:4
*/
System.out.println("//Collections 对象排序");
Fish f8 = new Fish("a1");
Fish f9 = new Fish("a2");
LinkedList<Fish> linkedlist = new LinkedList<Fish>();
linkedlist.add(f9);
linkedlist.add(f8);
for(Fish f:linkedlist) {
System.out.println(f);
/**
* //Collectons 对象排序
a2
a1
*/
}
//Collections.sort(linkedlist);//error 直接排序是不可以的,Fish没有实现 Comparable接口
/*
* Bound mismatch: The generic method sort(List<T>) of type
* Collections is not applicable for the arguments (LinkedList<Fish>).
* The inferred type Fish is not a valid substitute for
* the bounded parameter <T extends Comparable<? super T>>
*/
/**
*Comparator 给Fish对象封装了一个比较器
*/
Comparator<Fish> cf = new Comparator<Fish>(){//泛型的应用
@Override
public int compare(Fish s1, Fish s2) {
try {
Integer.parseInt(s1.getName());
if (s1.getName() != s2.getName()) {//顺序:s1-s2 倒叙:s2-s1
return Integer.parseInt(s1.getName()) - Integer.parseInt(s2.getName());
}
} catch (NumberFormatException e) {
return s1.getName().compareTo(s2.getName());
}
return 0;
}
};
Collections.sort(linkedlist,cf);
System.out.println("Comparator 给Fish对象封装了一个比较器");
for(Fish f:linkedlist) {
System.out.println(f);
/**
* Comparator 给Fish对象封装了一个比较器
a1
a2
*/
}
}
}
class Fish {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Fish() {}
public Fish(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
class smallFish {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public smallFish() {}
public smallFish(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
//以下重写Override hashCode()/equals(),只要name相同,则smallFish相同
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
smallFish other = (smallFish) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
java基础13.2_集合
最新推荐文章于 2024-08-02 23:00:19 发布