java容器:
有两个接口容器Collection、Map连个接口,Collection下有Set和List连个接口。
Set中的数据对象没有顺序且不重复,List中的数据对象有顺序器不可以重复。他们存放的是按单个数据存放的。Map存放是键值对key和value。
Set下有HashSet类,List接口有LinkedList和ArrayList两个类,map下有HashMap类。
(一)容器类简单使用事例代码:
import java.util.*;
public class TestCollection {
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c=new ArrayList();
c.add("hello");
c.add(new Name("f1","l1"));
c.add(new Integer(100));
System.out.println(c.size());
System.out.println(c);
c.remove(new Name("f1","l1"));
System.out.println(c);
}
}
class Name
{
private String firstName,lastName;
public Name(String firstName,String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String fullName()
{
return firstName+" "+lastName;
}
public boolean equals(Object obj)
{
if (obj instanceof Name)
{
Name name=(Name)obj;
return (firstName.equals(name.firstName)&&lastName.equals(name.lastName));
}
return super.equals(obj);
}
public int hashCode()
{
return firstName.hashCode();
}
}
注意要想删除在即写的的类必须在类中重写equals和hashCode。
(二)Iterator的使用:
Colletion接口的容器类都有一个Iterator方法用以返回一个实现了Iterator接口的对象。
Iterator对象称作为迭代器,用以方便的实现对容器元素的遍历操作。
代码事例:
import java.util.*;
public class TestIterator {
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c=new HashSet();
c.add(new Name("f1","l1"));
c.add(new Name("f2","l2"));
c.add(new Name("f3","l3"));
c.add(new Name("f4jhkhk","l4jkklj"));
Iterator i=c.iterator();
while(i.hasNext())
{
Name n=(Name)i.next();
System.out.println(n.getFirstName());
}
for(Iterator ii=c.iterator();ii.hasNext();)
{
Name n=(Name)ii.next();
if(n.getFirstName().length()<3)
{
ii.remove();
c.remove(n);
}
}
System.out.println();
Iterator i2=c.iterator();
while(i2.hasNext())
{
Name n=(Name)i2.next();
System.out.println(n.getFirstName());
}
}
}
(三)for增强:
public class StrongFor {
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9,0};
for(int i:arr)
{
System.out.println(i);
}
}
}
(四)Set容器事例代码:
public class TestSet {
public static void main(String[] args) {
// TODO Auto-generated method stub
Set s=new HashSet();
s.add(new Name("a","a"));
s.add(new Name("b","b"));
s.add(new Name("c","c"));
s.add(new Name("a","a"));//相同元素不会添加
s.add(new Name("e","e"));
s.add(new Name("b","b"));//相同元素不会添加
Iterator i=s.iterator();
while(i.hasNext())
{
Name n=(Name)i.next();
System.out.println(n.fullName());
}
}
}
事例代码二:
import java.util.*;
public class TestSet2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Set s1 = new HashSet();
Set s2 = new HashSet();
s1.add("a");
s1.add("b");
s1.add("c");
s2.add("d");
s2.add("a");
s2.add("b");
Set sn = new HashSet(s1);
sn.retainAll(s2);//与s2的交集
System.out.println(sn);
Set su = new HashSet();
su.add(s2);//重复的不添加
System.out.println(su);
}
}
(五)List容器事例:
事例代码一:
import java.util.*;
public class TestList {
public static void main(String[] args) {
// TODO Auto-generated method stub
List l1=new LinkedList();
for(int i=0;i<6;i++)
{
l1.add("a"+i);
}
System.out.println(l1);
l1.add(3,"sdasdas");
System.out.println(l1);
l1.set(6, "kpiiioio");
System.out.println(l1);
System.out.println(l1.get(4));
System.out.println(l1.indexOf("a4"));
l1.remove(1);
System.out.println(l1);
}
}
事例代码二:
import java.util.*;
public class TestList2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List l1=new LinkedList();
List l2=new LinkedList();
for(int i=0;i<6;i++)
{
l1.add("a"+i);
}
System.out.println(l1);
Collections.shuffle(l1);//随机排列
System.out.println(l1);
Collections.reverse(l1);//逆序排列
System.out.println(l1);
Collections.sort(l1);//排序
System.out.println(l1);
System.out.println(Collections.binarySearch(l1, "a5"));
}
}
(六)Comparable 接口(两个类之间比较大小)
所有可以“排序”的类都可以实现了java.lang.Comparable接口,java.lang.Comparable接口中只有一个方法public int comlareTo(Object obj);
事例代码:
class Name implements Comparable
{
@Override
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
Name n=(Name)arg0;
int lastCmp=lastName.compareTo(n.lastName);
return (lastCmp!=0?lastCmp:firstName.compareTo(n.firstName));
}
private String firstName,lastName;
public Name(String firstName,String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String fullName()
{
return firstName+" "+lastName;
}
public boolean equals(Object obj)
{
if (obj instanceof Name)
{
Name name=(Name)obj;
return (firstName.equals(name.firstName)&&lastName.equals(name.lastName));
}
return super.equals(obj);
}
public int hashCode()
{
return firstName.hashCode();
}
}
(七)如何选择诗句结构
Array读快改慢
Linked改快读慢
Hash两者之间
(八)Map接口
实现Map接口的类用来存储键-值对
Map接口的实现类有HashMap和TreeMap等。
Map类中存储的键值对通过键-值来标识,所以键值不能重复。
事例代码:
import java.util.*;
public class TestMap {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map m1=new HashMap();
Map m2=new TreeMap();
m1.put("one", new Integer(1));
m1.put("two", new Integer(2));
m1.put("three", new Integer(3));
m2.put("A", new Integer(1));
m2.put("B", new Integer(2));
System.out.println(m1.size());
System.out.println(m1.containsKey("one"));
System.out.println(m2.containsValue(new Integer(1)));
if(m1.containsKey("two"))
{
int i=((Integer)m1.get("two")).intValue();
System.out.println(i);
}
Map m3=new HashMap(m1);
m3.putAll(m2);
System.out.println(m3);
}
}
(九)自动打包、解包
打包:自动将基础的类型转换为对象
解包:自动将对象转换为基础类型
代码事例:
import java.util.*;
public class TestBox {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map m1=new HashMap();
Map m2=new TreeMap();
//打包
//m1.put("one", new Integer(1));
m1.put("one", 1);
//m1.put("two", new Integer(2));
m1.put("two", 2);
//m1.put("three", new Integer(3));
m1.put("three", 3);
//m2.put("A", new Integer(1));
m2.put("A", 1);
// m2.put("B", new Integer(2));
m2.put("B", 2);
//m2.put("B", 3434);
System.out.println(m1.size());
System.out.println(m1.containsKey("one"));
// System.out.println(m2.containsValue(new Integer(1)));
System.out.println(m2.containsValue(1));
if(m1.containsKey("two"))
{
//解包
//int i=((Integer)m1.get("two")).intValue();
int i=(Integer)m1.get("two");
System.out.println(i);
}
Map m3=new HashMap(m1);
m3.putAll(m2);
System.out.println(m3);
}
}
(十)泛型
代码事例:
import java.util.*;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> l1=new ArrayList<String>();
l1.add("sd");
l1.add("dfsdty");
l1.add("fsdfsd");
l1.add("rgfgdfhgdf");
System.out.println(l1);
for(int i=0;i<l1.size();i++)
{
System.out.println(l1.get(i));
}
System.out.println();
Collection<String> c=new HashSet<String>();
c.add("sds");
c.add("dfd");
c.add("bn");
c.add("piu");
c.add("dfsd");
for(Iterator<String> i=c.iterator();i.hasNext();)
{
System.out.println(i.next());
}
}
}
class MyName implements Comparable<MyName>
{
int age;
@Override
public int compareTo(MyName mn) {
// TODO Auto-generated method stub
if(this.age>mn.age)
return 1;
else
if(this.age<age)
return -1;
else
return 0;
}
}