LIST 和SET hashmap hashtable

List 控制的是一个数组,那么可以有重复的数据。
在integer的list中,添加4个3的话,会有4个元素在list中。
而set是不允许有重复的数据,所以如果set中添加4个3,只会有1个3.
set的一个用处是,假设你要在一个名单里面查找又没有名字相同的,就可以用set,
如果set中没有该名字,就存进set。如果有的话,说明找到了重复的。

set一般常用的是hashset,查询和插入效率为o(1)。


  HashSet<string> hs = new HashSet<string>();
            hs.Add("");
            hs.Add("");
            hs.Add("");
常用方法:add 

Clear  清空集合中的所有元素

Contains 确定某元素是否在HashSet<T>中

Remove  从HashSet<T>对象中移除指定的元素  等等


List<String> person= new  ArrayList<>();
             person.add( "jackie" );    //索引为0  //.add(e)
             person.add( "peter" );     //索引为1
             person.add( "annie" );     //索引为2
             person.add( "martin" );    //索引为3
             person.add( "marry" );     //索引为4
             
             person.remove( 3 );    //.remove(index)
             person.remove( "marry" );      //.remove(Object o)
lis.set(1,"abc8");修改
lis.get(1);获取


 HashMap hs=new HashMap();       

 hs.put("name", "张三");       

 hs.put("sex", "男");       

 hs.put("age", "30");       

 hs.put("home", "河北");  


发布了26 篇原创文章 · 获赞 11 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/ganyouxian_java/article/details/72588523