package com.htxx.redis;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;
import com.sun.xml.internal.ws.api.model.MEP;
public class JiHe {
void testList(){
//List继承与Collections接口
//特点:List集合中元素有序可重复
//---List格式为[a,b,c,d]
//ArrayList 实现一个数组,它的规模可变并且能像链表一样被访问。
List<String> pets=new ArrayList<String>();
pets.add("dog");
pets.add("cat");
pets.add("mouse");
for( String pet:pets){
System.out.println(pet);
//dog
//cat
//mouse
}
System.out.println(pets);//[dog, cat, mouse]
// LinkedList实现一个链表,提供最佳顺序存取,适合插入和移除元素。
List<String> name=new LinkedList<>();
name.add("zhangsan");
name.add("lisi");
name.add("wangwu");
System.out.println(name);//[zhangsan, lisi, wangwu]
/** * ArrayList和LinkedList的区别: * 主要在于ArrayList是基于索引(index)的数据结构,在查询方面可根据index查询,速度快. * LinkedList以指针相连,在插入删除方面比较有优势 * */
}
void testMap(){
//Map中key不允许重复
Map<String, String> map=new HashMap<String, String>();
map.put("name", "sasa");
map.put("gender", "man");
System.out.println(map.get("name"));//sasa
map.put("name", "sa");
System.out.println(map.get("name"));//sa
System.out.println(map.toString());//{gender=man, name=sa}
}
void testSet(){
//set继承于Collection接口 //集合中的元素不按特定方式排序,只是简单的把对象加入集合中,就像往口袋里放东西。 //对Set中成员的访问和操作是通过Set中对象的引用进行的,所以集中不能有重复对象 //HashSet扩展AbstractSet并且实现SET接口。它创建一个类集, // 该类集使用散列表进行存储,而散列表则通过使用称之为散列法的机制来存储信息, //在散列中,一个关键字的信息内容被用来确定唯一的一个值,成为散列码, // 而散列码被用来当做与关键字相连 的数据的存储下标。关键字到其散列码的转换是 //自动执行的。程序代码也不能直接索引散列表。 //散列法的优点是:即使对于大的集合,它允许一些操作,如add,contains,remove //size 等方法的运行时间保持不变。
Set<String> color=new HashSet<>();
color.add("red");
color.add("black");
color.add("blue");
color.add("white");
color.add("white");
System.out.println(color);//[red, blue, white, black]
//TreeSet 将放入其中的元素按序存放。
Set<String> color1=new TreeSet<>();
color1.add("red");
color1.add("black");
color1.add("blue");
color1.add("white");
color1.add("white");
System.out.println(color1);//[black, blue, red, white]
}
void testIterator(){
/** * 集合容器(如:List、Set、Map等)本身提供了处理元素置入和取出的方式, * 但是单一选取元素的方法很受限制。 * 所以我们要用Iterator去选取容器中的元素,它将容器转换成一个序列。 */
Set<String> color=new HashSet<>();
color.add("red");
color.add("black");
color.add("blue");
color.add("white");
color.add("white");
Iterator iterator=color.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
//red
//blue
//white
//black
}
}
public static void main(String[] args) {
JiHe jiHe=new JiHe();
}
}
还没有评论,来说两句吧...