容器的概念

来源:互联网 发布:淘宝网的营销模式是 编辑:程序博客网 时间:2024/05/17 05:19
容器概念
如果是两个是说明引用相同,如果是equels是相等
选中方法名称右键source选择override implement method
Set和List的存储方式
set中的数据对象没有顺序且不可以重复
list中的数据对象有顺序且可以重复
map接口的存储方式
键key 值value的方法
Iterator
所有实现了Collection接口的容器类都有一个iterator 方法用以返回一个实现了Iterator接口的对象
Iterator对象称作迭代器,用以方便的实现对容器内元素的遍历操作
Iterator接口定义了如下方法
boolean hasNext() //判断游标右边是否有元素
Object next() // 返回游标右边的元素并将游标移动到下一个位置
Void remove() //删除游标左面的元素,在执行完next之后该操作只能执行一次
Set接口
Set接口是Collection的子接口,Set接口没有提供额外的方法,但实现Set接口的容器类中的元素
是没有顺序的,而且不可以重复
Set容器类有HashSet TreeSet等
package com.container.test;
import java.util.Collection;
import java.util.HashSet;
public class HashSetTest {
 public static void main(String[] args) {
  Collection c =new HashSet<>();
  c.add("舒舒");
  c.add(new Integer(5));
  c.add(new Name("lsq","lsp"));
  c.add("舒舒");
  c.add(new Name("lsq","lsp"));
  System.out.println(c.size());
  System.out.println(c);
 }
}
class Name{
 private String firstName ,lastName;
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
 @Override
 public String toString() {
  return "Name [firstName=" + firstName + ", lastName=" + lastName + "]";
 }
 public Name(String firstName, String lastName) {
  super();
  this.firstName = firstName;
  this.lastName = lastName;
 }
}
List接口
List接口是Collection的子接口 实现List接口的容器类中的元素是有顺序的,而且可以重复
List容器中的元素都对应一个整数型的序号记载其容器中的位置 ,可以根据需要存取容器中的元素
List容器类有ArrayList,LinkedList等
Collections类

package com.container.test;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class LinkedListTest {
 public static void main(String[] args) {
  List list =new LinkedList();
  for (int i = 0; i <8; i++) {
   list.add(i+"l");
  }
  System.out.println(list);
  list.set(3, "44l");//set为替换掉了
  System.out.println(list);
  list.add(3,"55l");//往后边推
  System.out.println(list);
  System.out.println((String)list.get(3));
  System.out.println(list.indexOf("2l"));
  Collections.shuffle(list);
  System.out.println(list);
  Collections.sort(list);
  System.out.println(list);
  Collections.reverse(list);
  System.out.println(list);
  Collections.binarySearch(list, "2l");
  System.out.println(list);
 }
}
Comparable接口
所有可以排序的类都实现了java.lang.Comparable接口,Comparable接口只有一个方法
public int compareTo(Object obj)
返回0 表示 this == obj
返回正数表示 this>obj
返回负数表示 this<obj
ArrayList和LindedList之间的区别
 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。
 2. 对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。
 3. 对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据 
4.rrayList 采用的是数组形式来保存对象的,这种方式将对象放在连续的位置中,所以最大的缺点就是插入删除时非常麻烦
5.LinkedList 采用的将对象存放在独立的空间中,而且在每个空间中还保存下一个链接的索引 但是缺点就是查找非常麻烦 要丛第一个索引开始
Map接口
实现map接口的类用来存储键值对
map接口的实现类有HashMap和TreeMap
map类中存储的键值对通过键来标识,所以键不能够重复

package com.container.test;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class MapTest {
   public static void main(String[] args) {
 Map map =new HashMap();
 map.put("one", new Integer(1));
 //map.put("one",1);
 map.put("two", new Integer(2));
 map.put("three", new Integer(3));
 Map map1 =new TreeMap();
 map1.put("a", new Integer(1));
 map1.put("b", new Integer(2));
 System.out.println(map.size());
 System.out.println(map.containsKey("one"));
 System.out.println(map1.containsValue(new Integer(1)));
 if(map.containsKey("two")){
  int i =(Integer)map.get("one");
  System.out.println(i);
 }
 Map map2 =new HashMap(map);
 map2.putAll(map1);
 System.out.println(map2);
}
}
打包,解包
自动将基础类型转化为对象
自动将对象转化为基础类型
泛型的案例
package com.container.test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
public class TestFX {
   public static void main(String[] args) {
 Collection<String> c =new ArrayList<String>();
 c.add("li");
 c.add("shu");
 c.add("qing");
 for (int i = 0; i < c.size(); i++) {
  String name =((ArrayList<String>) c).get(i);
  System.out.println(name);
 }
 Collection<String> h =new HashSet<String>();
 h.add("li");
 h.add("shu");
 h.add("peng");
 for (Iterator iterator = h.iterator(); iterator.hasNext();) {
  String brotherName = (String) iterator.next();
  System.out.println(brotherName);
 } }}
class MyName implements Comparable<MyName>{
   int age ;
 public int compareTo(MyName o) {
  if(this.age>o.age){
   return 1;
  }
  else if(this.age<o.age){
   return -1;
  }
  else{
   return 0;
  }}}

package com.container.test;
import java.util.HashMap;
import java.util.Map;
public class MapTwoTest {
 public static void main(String[] args) {
    Map<String, Integer> map =new HashMap<String,Integer>();
    map.put("one", 5);
    map.put("two", 6);
    map.put("three",7);
    System.out.println(map.size());
    System.out.println(map.containsKey("one"));
      if(map.containsKey("two")){
      int im =map.get("two");
      System.out.println(im);}
 }
}
ArrayList和Vector的区别
1 Vector的方法都是同步的(Synchronized),是线程安全的(thread-safe),而ArrayList的方法不是,由于线程的同步必然要影响性能,因此,ArrayList的性能比Vector好。
2 当Vector或ArrayList中的元素超过它的初始大小时,Vector会将它的容量翻倍,而ArrayList只增加50%的大小,这样,ArrayList就有利于节约内存空间。

HashMap和HashTable有什么区别
总结:
hashmap线程不安全允许有null的键和值效率高一点、方法不是Synchronize的要提供外同步有containsvalue和containsKey方法HashMap 是Java1.2 引进的Map interface 的一个实现HashMap是Hashtable的轻量级实现hashtable线程安全不允许有null的键和值效率稍低、方法是是Synchronize的有contains方法方法、Hashtable 继承于Dictionary 类Hashtable 比HashMap 要旧