集合类处理范例

来源:互联网 发布:2016时时彩系统源码 编辑:程序博客网 时间:2024/04/29 11:07

 范例一:打印ArrayList中的元素
import java.util.*;
public class ArrayListTest
{
 public static void main(String args[])
 {
  ArrayList al = new ArrayList();
  al.add("zhangsan");
  al.add("lisi");
  al.add("wangwu");
 
  for(int i=0;i<al.size();i++)//使用for循环打印出al中的所有元素
  {
   System.out.println(al.get(i));
  }
 
  System.out.println(al);//调用ArrayList的toString()方法打印出al中的元素
 


 
 }
}

范例二:想ArrayList中添加自定义对象,并打印出来
import java.util.*;
public class ArrayListTest
{
 public static void main(String args[])
 {
  ArrayList al = new ArrayList();
 
  al.add(new Point(3,3));
  al.add(new Point(2,2));
  al.add(new Point(4,4));
 
  System.out.println(al);
 }
}

class Point
{
 int x,y;
 
 Point(int x,int y)
 {
  this.x = x;
  this.y = y;
 }
 
 public String toString()
 {
  return "x="+x+",y="+y;
 }
}


范例三:ArrayList的toArray()方法
import java.util.*;
public class ArrayListTest
{
 public static void main(String args[])
 {
  ArrayList al = new ArrayList();
  al.add(new Point(3,3));
  al.add(new Point(2,2));
  al.add(new Point(4,4));
 
  Object[] obj = al.toArray();
 
  for(int i=0;i<obj.length;i++)
  {
   System.out.println(obj[i]);
  }
 }
}

class Point
{
 int x,y;
 
 Point(int x,int y)
 {
  this.x = x;
  this.y = y;
 }
 
 public String toString()
 {
  return "x="+x+",y="+y;
 }
}

范例四:Arrays.asList()方法的用法范例,Arrays.asList()方法返回一个列表
import java.util.*;
public class ArrayListTest
{
 public static void main(String args[])
 {
  ArrayList al = new ArrayList();
 
  al.add(new Point(3,3));
  al.add(new Point(2,2));
  al.add(new Point(4,4));
 
  Object[] obj = al.toArray();
 
  List l=Arrays.asList(obj);
 
  System.out.println(l);
  //l.add(new Point(33,33));//error!not to add elements to this list;
  l.set(2,new Point(5,5));//right! can modify element
  System.out.println(l);
 }
}

class Point
{
 int x,y;
 
 Point(int x,int y)
 {
  this.x = x;
  this.y = y;
 }
 
 public String toString()
 {
  return "x="+x+",y="+y;
 }
}

范例五:迭代器的用法范例,Iterator;
import java.util.*;
public class ArrayListTest
{
 public static void main(String args[])
 {
  ArrayList al = new ArrayList();
 
  al.add(new Point(3,3));
  al.add(new Point(2,2));
  al.add(new Point(4,4));
 
  Iterator it = al.iterator();
  it.next();
  it.remove();
 
  while(it.hasNext())
  {
   System.out.println(it.next());
  }
 }
}

class Point
{
 int x,y;
 
 Point(int x,int y)
 {
  this.x = x;
  this.y = y;
 }
 
 public String toString()
 {
  return "x="+x+",y="+y;
 }
}
范例六:迭代器的用法范例
import java.util.*;
public class ArrayListTest
{
 public static void printElements(Collection c)
 {
  Iterator it = c.iterator();
 
  while(it.hasNext())
  {
   System.out.println(it.next());
  }
 }
 public static void main(String args[])
 {
  ArrayList al = new ArrayList();
 
  al.add(new Point(3,3));
  al.add(new Point(2,2));
  al.add(new Point(4,4));
 
  ArrayListTest.printElements(al);

 }
}

class Point
{
 int x,y;
 
 Point(int x,int y)
 {
  this.x = x;
  this.y = y;
 }
 
 public String toString()
 {
  return "x="+x+",y="+y;
 }
}

范例七,Collections类中的sort()和reverseOrder()用法范例
import java.util.*;
public class ArrayListTest
{
 public static void printElements(Collection c)
 {
  Iterator it = c.iterator();
 
  while(it.hasNext())
  {
   System.out.println(it.next());
  }
 }
 public static void main(String args[])
 {
  ArrayList al = new ArrayList();
  Student s1 = new Student(10,"zhangsan");
  Student s2 = new Student(10,"lis");
  Student s3 = new Student(17,"wangwu");
 
  al.add(s1);
  al.add(s2);
  al.add(s3);
 
  Collections.sort(al); 
  ArrayListTest.printElements(al);
  System.out.println("=======================");
 
  Collections.sort(al,new Student.StudentComparator());
  ArrayListTest.printElements(al);
  System.out.println("====================");
 
  Collections.sort(al,Collections.reverseOrder());
  ArrayListTest.printElements(al);

 }
}

class Student implements Comparable
{
 int age;
 String name;
 
 static class StudentComparator implements Comparator
 {
  public int compare(Object obj1,Object obj2)
  {
   Student s1 = (Student)obj1;
   Student s2 = (Student)obj2;
   int result = s1.age>s2.age ? 1:(s1.age==s2.age ? 0:-1);
  
   if(result==0)
   {
    result = s1.name.compareTo(s2.name);
   }
   return result;
  }
 }
 
 Student(int age,String name)
 {
  this.age = age;
  this.name = name;
 }
 
 public int compareTo(Object o)
 {
  Student s = (Student)o;
  return age > s.age ? 1 : (age == s.age ? 0 : -1);
 }
 
 public String toString()
 {
  return name+":"+age;
 }
}

 

范例八:利用LinkedList实现stack

栈(Stack)也是一种特殊的线性表,是一种后进先出(LIFO)的结构。
栈是限定仅在表尾进行插入和删除运算的线性表,表尾称为栈顶(top),表头称为栈底(bottom)。
栈的物理存储可以用顺序存储结构,也可以用链式存储结构。

import java.util.*;

public class StackTest
{
 private LinkedList l1 = new LinkedList();
 
 public void push(Object o)
 {
  l1.addFirst(o);
 }
 
 public Object peek()
    {
     return l1.getFirst();
    }
   
    public Object pop()
    {
   return l1.removeFirst();   
    }
   
    public boolean isEmpty()
    {
     return l1.isEmpty();
    }
   
 public static void main(String[] args)
 {
  StackTest st = new StackTest();
 
  st.push("one");
  st.push("two");
  st.push("three");
 
  System.out.println(st.pop());
  System.out.println(st.peek());
  System.out.println(st.pop());
  System.out.println(st.isEmpty());
  System.out.println(st.pop());
  System.out.println(st.isEmpty()); 
 }
}
/*运行结果:
three
two
two
false
one
true
Press any key to continue...
*/


范例九:利用LinkedList实现Quee

队列(Queue)是限定所有的插入只能在表的一端进行,而所有的删除都在表的另一端进行的线性表。
表中允许插入的一端称为队尾(Rear),允许删除的一端称为队头(Front)。
队列的操作是按先进先出(FIFO)的原则进行的。
队列的物理存储可以用顺序存储结构,也可以用链式存储结构。

import java.util.*;


public class QueueTest
{
 private LinkedList l1 = new LinkedList();
 
 public void put(Object o)
 {
  l1.addLast(o);
 }
 
 public Object get()
 {
  return l1.removeFirst();
 }
 
 public boolean isEmpty()
 {
  return l1.isEmpty();
 }
 
 public static void main(String[] args)
 {
  QueueTest qt = new QueueTest();
  qt.put("one");
  qt.put("two");
  qt.put("three");
 
  System.out.println(qt.get());
  System.out.println(qt.get());
  System.out.println(qt.isEmpty());
  System.out.println(qt.get());
  System.out.println(qt.isEmpty());
 }
}
/*
运行结果
one
two
false
three
true
Press any key to continue...
*/


范例十:HashSet 用法范例,
import java.util.*;

public class HashSetTest
{
 public static void main(String[] args)
 {
  HashSet hs = new HashSet();
  hs.add("one");
  hs.add("two");
  hs.add("three");
  hs.add("one");
 
  Iterator it = hs.iterator();
 
  while(it.hasNext())
  {
   System.out.println(it.next());
  }
 }
}
/*
运行结果
two
one
three
Press any key to continue...
*/
从上面的运行结果中我们可以看到,HashSet中不不允许有重复的元素的。

范例十一:HashSet的用法
import java.util.*;

public class HashSetTest
{
 public static void main(String[] args)
 {
  HashSet hs = new HashSet();
  hs.add(new Student(10,"zhangsan"));
  hs.add(new Student(20,"lis"));
  hs.add(new Student(30,"wangwu"));
  hs.add(new Student(10,"zhangsan")); 
  Iterator it = hs.iterator();
 
  while(it.hasNext())
  {
   System.out.println(it.next());
  }
 }
}

class Student
{
 int age;
 String name;
 Student(int age,String name)
 {
  this.age = age;
  this.name = name;
 }
 
 
 public String toString()
 {
  return name+":"+age;
 }
}
/*
运行结果
zhangsan:10
wangwu:30
lis:20
zhangsan:10
Press any key to continue...

可以看出HashSet中出现了重复元素,
这是因为我们用new 关键字产生了两个不同的对象,
因此他们的散列码是不相同的,所以回出现了相同的元素,那么如何解决这个重复的问题那请看下面的范例*/

范例十二:HashSet的用法范例
import java.util.*;

public class HashSetTest
{
 public static void main(String[] args)
 {
  HashSet hs = new HashSet();
  hs.add(new Student(10,"zhangsan"));
  hs.add(new Student(20,"lis"));
  hs.add(new Student(30,"wangwu"));
  hs.add(new Student(10,"zhangsan")); 
  Iterator it = hs.iterator();
 
  while(it.hasNext())
  {
   System.out.println(it.next());
  }
 }
}

class Student
{
 int age;
 String name;
 Student(int age,String name)
 {
  this.age = age;
  this.name = name;
 }
 
 public int hashCode()
 {
  return age*name.hashCode();
 }
 
 public boolean equals(Object o)
 {
  Student s = (Student) o;
  return  age==s.age && name==s.name;
 }
 
 public String toString()
 {
  return name+":"+age;
 }
}
/*wangwu:30
zhangsan:10
lis:20
Press any key to continue...


*/


范例十三:TreeSet使用范例
import java.util.*;

public class TreeSetTest
{
 public static void main(String[] args)
 {
  TreeSet ts = new TreeSet();
  ts.add("zhangsan");
  ts.add("lisi");
  ts.add("wangwu");
 
  Iterator it = ts.iterator();
 
  while(it.hasNext())
  {
   System.out.println(it.next());
  }
 }
}

运行结果
lisi
wangwu
zhangsan
Press any key to continue...

从上述结果可以知道TreeSet在输出时是按照升序排列的

范例十四:TreeSet使用范例,想TreeSet中添加自定义对象

import java.util.*;

public class TreeSetTest
{
 public static void main(String[] args)
 {
  TreeSet ts = new TreeSet(new Student.StudentComparator());
        ts.add(new Student("zhangsan",10));
        ts.add(new Student("lisi",5));
        ts.add(new Student("wangwu",30));
  ts.add(new Student("mary",30));
  Iterator it = ts.iterator();
 
  while(it.hasNext())
  {
   System.out.println(it.next());
  }
 }
}

class Student implements Comparable
{
 String name;
 int age;
 
 static class StudentComparator implements Comparator
 {
  public int compare(Object o1,Object o2)
  {
   Student s1 = (Student) o1;
   Student s2 = (Student) o2;
   int result = s1.age > s2.age ? 1 :(s1.age == s2.age ? 0 : -1);
  
   if(result == 0)
   {
    result = s1.name.compareTo(s2.name);
   }
   return result;
  }
 }
 Student(String name,int age)
 {
  this.name = name;
  this.age = age;
 }
 
 public int compareTo(Object o)
 {
  Student s = (Student)o;
 
  return this.age>s.age ? 1:(this.age==s.age ? 0 : -1);
 
 }
 
 public String toString()
 {
  return age+":"+name;
 
 }
}

运行结果
5:lisi
10:zhangsan
30:mary
30:wangwu
Press any key to continue...


范例十五:HashMap的用法范例
import java.util.*;

public class HashMapTest
{
 public static void printElement(Collection c)
 {
  Iterator it = c.iterator();
 
  while(it.hasNext())
  {
   System.out.println(it.next());
  }
 }
 public static void main(String[] args)
 {
  HashMap hm = new HashMap();
  hm.put("one","zhangsan");
  hm.put("two","lisi");
  hm.put("three","wangwu");
 
  System.out.println(hm.get("one"));
  System.out.println(hm.get("two"));
  System.out.println(hm.get("three"));
 
  Set s = hm.keySet();
        System.out.println("keys");
        printElement(s);
 
  System.out.println("values");
  Collection c = hm.values();
 
  printElement(c);
 
 
  Set ss = hm.entrySet();
 
  printElement(ss);
 
  Iterator it = ss.iterator();
 
  while(it.hasNext())
  {
   Map.Entry m = (Map.Entry) it.next();
   System.out.println(m.getKey()+":"+m.getValue());
  }
 }
}
/*
 *
 *
 *运行结果:
 *zhangsan
lisi
wangwu
keys
two
one
three
values
lisi
zhangsan
wangwu
two=lisi
one=zhangsan
three=wangwu
two:lisi
one:zhangsan
three:wangwu
Press any key to continue...
*/

范例十六:Property的用法范例---读取文件的配置信息
import java.util.*;
import java.io.*;

public class ProTest
{
 public static void main(String args[]) throws Exception
 {

  Properties pps = new Properties();
 
  pps.load(new FileInputStream("win.ini"));
 
  Enumeration e = pps.propertyNames();
 
  while(e.hasMoreElements())
  {
   String key = (String)e.nextElement();
   System.out.println(key+"="+pps.getProperty(key));
  }
 }
}
运行结果:
name3=wangwu;
name2=lisi;
name1=zhangsan
Press any key to continue...

原创粉丝点击