Chinaunix首页 | 论坛 | 博客
  • 博客访问: 50718
  • 博文数量: 24
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 335
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-09 17:06
文章分类
文章存档

2019年(24)

我的朋友

分类: Java

2019-11-06 14:07:07

Java集合

Java集合类可以说是我们在工作中运用最多、最频繁的类。相比于数组(Array)来说,集合类的长度可变,更加方便开发。

Java集合就像一个容器,可以存储任何类型的数据,也可以结合泛型来存储具体的类型对象。在程序运行时,Java集合可以动态的进行扩展,随着元素的增加而扩大。在Java中,集合类通常存在于java.util包中。

Java集合主要由2大体系构成,分别是Collection体系和Map体系,其中CollectionMap分别是2大体系中的顶层接口。

Collection主要有三个子接口,分别为List(列表)Set()Queue(队列)。其中,ListQueue中的元素有序可重复,而Set中的元素无序不可重复。

List中主要有ArrayListLinkedList两个实现类;Set中则是有HashSet实现类;而Queue是在JDK1.5后才出现的新集合,主要以外汇代理数组和链表两种形式存在。

Map同属于java.util包中,是集合的一部分,但与Collection是相互独立的,没有任何关系。Map中都是以key-value的形式存在,其中key必须唯一,主要有HashMapHashTabletreeMap三个实现类。

集合迭代

package com.cloud.list;

import java.util.ArrayList;

import java.util.List;

public class Demo1 {

   public static void main(String[] args) {

      List list=new ArrayList();

      list.add("Spring1");

      list.add("Spring2");

      list.add("Spring3");

      list.add("Spring4");

      for(int i=0;i

        System.out.println(list.get(i)+";");

      }

      List sublist=list.subList(1, 4);

      System.out.println(sublist);

   }

}

package com.cloud.list;

import java.util.ArrayList;

import java.util.List;

import java.util.ListIterator;

public class Demo2 {

         /**

          * @param args

          */

         public static void main(String[] args) {

                   // TODO Auto-generated method stub

                   List list=new ArrayList();

                   list.add("Spring1");

                   list.add("Spring2");

                   list.add("Spring3");

                   ListIterator it=list.listIterator();

                   it.next();

                   it.next();

                   it.set("Spring5");

                   /*

                   it.add("Spring4");

                  

                   while(it.hasNext()){

                            System.out.println(it.next());

                   }

                   */

                   System.out.println(list);

         }

}

正逆迭代

package com.cloud.list;

import java.util.ArrayList;

import java.util.List;

import java.util.ListIterator;

public class Demo3 {

         /**

          * @param args

          */

         public static void main(String[] args) {

                   // TODO Auto-generated method stub

                   List list=new ArrayList();

                   list.add("Spring1");

                   list.add("Spring2");

                   list.add("Spring3");

                   //一:for变量集合

                   for(int i=0;i

                            System.out.print(list.get(i)+";");

                   }

                   System.out.println();

                   //二:迭代器正序迭代

                   ListIterator it=list.listIterator();

                   while(it.hasNext()){

                            System.out.print(it.next()+";");

                   }

                   System.out.println();

                   //三:迭代器倒序迭代

                   while(it.hasPrevious()){

                            System.out.print(it.previous()+";");

                   }

         }

}

ArrayList集合

package com.cloud.list;

import java.util.ArrayList;

import java.util.Iterator;

/* ArrayList

 *  查询快,增删慢

 */

public class Demo4 {

   public static void main(String[] args) {

      ArrayList list=new ArrayList();

      list.add(new Book(1,"java编程1"));

      list.add(new Book(2,"java编程2"));

      list.add(new Book(3,"java编程3"));

      list.add(new Book(3,"java编程4"));

      System.out.println(list);

      ArrayList list2=clearRepeat(list);

      System.out.println(list2);

   }

   public static ArrayList clearRepeat(ArrayList list){

      //创建新的集合

      ArrayList newList=new ArrayList();

      //获取新的集合

      Iterator it= list.iterator();

      while(it.hasNext()){

        Book book=(Book) it.next();

        if(!newList.contains(book)){

           newList.add(book);

        }

      }

      return newList;

   }

}

class Book{

   int id;

   String name;

   public Book(int id,String name){

      this.id=id;

      this.name=name;

   }

   @Override

   public String toString() {

      // TODO Auto-generated method stub

      return "(编号:"+this.id+";名字:"+this.name+")";

   }

   @Override

   public boolean equals(Object obj) {

      Book book=(Book)obj;

      return this.id==book.id;

   }

   @Override

   public int hashCode() {

      // TODO Auto-generated method stub

      return super.hashCode();

   }

}

LinkedList集合

package com.cloud.list;

import java.util.Iterator;

import java.util.LinkedList;

public class Demo5 {

         /*

          LinkedList

           查询速度慢,增删快

          Linkedlist特有的方法:

         1:方法介绍

                     addFirst(E e)

                                     addLast(E e)

                                     getFirst()

                                     getLast()

                                     removeFirst()

                                     removeLast()

         2:数据结构

                                     1:栈 (1.6  : 主要是用于实现堆栈数据结构的存储方式。

                                               先进后出

                                               push()

                                               pop()

                                     2:队列(双端队列1.5): 主要是为了让你们可以使用LinkedList模拟队列数据结构的存储方式。

                                               先进先出

                                               offer()

                                               poll()                        

         3:返回逆序的迭代器对象     

                            descendingIterator()   返回逆序的迭代器对象

          */

         public static void main(String[] args) {

                   LinkedList list=new LinkedList();

                   list.add("Spring2");

                   list.add("Spring3");

                   list.add("Spring4");

//               list.addFirst("Spring1");

//               list.push("Spring");//把该元素放到集合的首位置

//               System.out.println("集合的首元素"+list.pop());//移除并返回集合中的第一个元素

//               list.offer("Spring0");//添加到末尾位置

//               System.out.println(list);

                  

                   //逆向输出的迭代器

                   Iterator it = list.descendingIterator();

                   while (it.hasNext()) {

                            System.out.println(it.next()); 

                   }

         }

}

模拟集合中的值栈

package com.cloud.list; 

import java.util.LinkedList; 

public class Demo6 {

         /*

         2:数据结构

                   1:栈 (1.6  : 主要是用于实现堆栈数据结构的存储方式。

                            先进后出

                            push()

                            pop()

                   2:队列(双端队列1.5): 主要是为了让你们可以使用LinkedList模拟队列数据结构的存储方式。

                            先进先出

                            offer()

                            poll()

         */

         public static void main(String[] args) {

                   System.out.println("======测试值栈=======");

                   StackList list=new StackList();

                   list.add("Spring1");

                   list.add("Spring2");

                   list.add("Spring3");

//               System.out.println(list.pop());//删除并返回Spring3

//               System.out.println(list.pop());//删除并返回Spring2

//               System.out.println(list.pop());//删除并返回Spring1

                  

                   /*list.size()大小在不断的变化,所以很难遍历完

                   for(int i=0;i

                            System.out.println(list.pop());

                   }

                   */

                   int size=list.size();

                   for(int i=0;i

                            System.out.println(list.pop());

                  }

                   System.out.println("======测试队列=======");

                   TeamList list2=new TeamList();

                   list2.add("Spring1");

                   list2.add("Spring2");

                   list2.add("Spring3");

                   int size2=list2.size();

                   for(int i=0;i

                            System.out.println(list2.remove());

                   }

         }

}

//模拟堆栈存储方式

class StackList{

         LinkedList list;

         public StackList(){

                   list=new LinkedList();

         }

         // 进栈

         public void add(Object o){

                   list.push(o);

         }

         //出栈

         public Object pop(){

                   return list.pop();

         }

         //大小

         public int size(){

                   return list.size();

         }

}

//模拟队列存储方式

class TeamList{

         LinkedList list;

         public TeamList(){

                   list=new LinkedList();

         }

         public void add(Object o){

                   list.offer(o);

         }

         public Object remove(){

                   return list.poll();

         }

         public int size(){

                   return list.size();

         }

模拟洗牌的案例

package com.cloud.day3;

import java.util.LinkedList;

import java.util.Random;

/*

 * 实现一副牌的洗牌功能

 */

class Poker{

   String color;

   String num;

   public Poker(String color,String num){

      this.color=color;

      this.num=num;

   }

   public String toString(){

      return "{"+color+num+"}";

   }

}

public class Demo1 {

   public static void main(String[] args) {

      LinkedList list=createPokers();

      System.out.println(list.size()+"张牌");

      washPokers(list);

      showPokers(list);

   }

   //获取一副牌

   public static LinkedList createPokers(){

      LinkedList list=new LinkedList();

      String [] color={"红桃","黑桃","方块","梅花"};

      String [] nums={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};

      for(int i=0;i

        for(int j=0;j

           list.add(new Poker(color[j],nums[i]));

        }

      }

      return list;

   }

   //显示一副牌

   public static void showPokers(LinkedList list){

      for(int i=0;i

        System.out.print(list.get(i));

        if(i%10==9){

           System.out.println();

        }

      }

   }

   //刷新一副牌

   public static void washPokers(LinkedList pokers){

      Random random=new Random();

      for(int i=0;i<20;i++){

        //随机产生两个索引值

        int index1=random.nextInt(pokers.size());

        int index2=random.nextInt(pokers.size());

        //根据索引取出两张牌,然后交换顺序

        Poker poker1=pokers.get(index1);

        Poker poker2=pokers.get(index2);

        pokers.set(index1, poker2);

        pokers.set(index2, poker1);

      }

   }

}

集合比较大小

package com.cloud.day3;

import java.util.LinkedList;

public class Demo2 {

   public static void main(String[] args) {

      LinkedList list=new LinkedList();

      list.add(new Person("Spring1",13));

      list.add(new Person("Spring2",12));

      list.add(new Person("Spring3",11));

      for(int i=0;i

        for(int j=i+1;j

           Person p1 = list.get(i);

           Person p2 = list.get(j);

           if(p1.age>p2.age){

              list.set(i, p2);

              list.set(j, p1);

           }

        }

      }

      System.out.println(list);

   }

}

class Person{

   String name;

   int age;

   public Person(String name,int age){

      this.name=name;

      this.age=age;

   }

   @Override

   public String toString() {

      return "[姓名:"+this.name+";年龄:"+this.age+"]";

   }

}

阅读(1353) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册