美文网首页
JAVA集合应用

JAVA集合应用

作者: _Ely | 来源:发表于2018-06-10 13:53 被阅读13次

ArrayList使用


public class Notice {

private int id;
private String title;
private String creater;
private Date createTime;
//public Notice(int id, String title, String creater, Date createTime)
    //get &setter 
    //后面同理,构造函数+getter/setter方法+重写toString

}
/**

  • list示例
  • 公告需求管理
  • @author ely

*/
public class NoticeTest {

public static void main(String[] args) {
    //创建对象,生成三条公告
    Notice notice1 = new Notice(1, "欢迎来到这里", "管理员", new Date());
    Notice notice2 = new Notice(2, "该交作业了", "老师", new Date());
    Notice notice3 = new Notice(3, "考勤通知", "老师", new Date());
    
    //添加公告
    ArrayList noticeList = new ArrayList();
    noticeList.add(notice1);
    noticeList.add(notice2);
    noticeList.add(notice3);
    
    //显示公告
    System.out.println("--------公告的内容为:----------------");
    for(int i=0;i<noticeList.size();i++){
        System.out.println("公告"+(i+1)+":"+((Notice)(noticeList.get(i))).getTitle());
    }
    
    //在第一条公告后面添加一调新的公告
    Notice notice4= new Notice(4,"在线编辑器可以使用","管理员",new Date());
    noticeList.add(1, notice4);

    //删除
    noticeList.remove(2);
    
    //修改
    //修改第二条公告
    notice4.setTitle("JAVA在线编辑器可以使用啦");
    noticeList.set(1, notice4);
}

}

HashSet使用


public class Cat {

private String name;
private int month;
private String species;

}

/**

  • set示例
  • 宠物猫信息管理
  • @author ely

*/
public class CatTest {

public static void main(String[] args){
    Cat cat1 = new Cat("huahua",12,"american cat");
    Cat cat2 = new Cat("fanfan",3,"China cat");
    
//hashset添加数据
Set set = new HashSet();
set.add(cat1);
set.add(cat2);
Iterator it = set.iterator();
while(it.hasNext())
{
    System.out.println(it.next());
}
    

//当插入重复元素,记得重写hashcode和equals方法
System.out.println("-----------------------------------------");
System.out.println("after add chongfu content");
Cat cat3 = new Cat("huahua",12,"american cat");
set.add(cat3);
it=set.iterator();
while(it.hasNext())
{
    System.out.println(it.next());
}

//查询
//直接查找对象(object)
System.out.println("---------------------------------");
System.out.println("-------use object to query cat---------");
if(set.contains(cat3))
    {System.out.println("cat3 find !");
System.out.println(cat3);}
else {
    System.out.println("not find!");
}
//对于查找对象,如果我们通过对象的属性查找
System.out.println("---------------------------------");
System.out.println("-------use object to query cat---------");
boolean flag = false;
it = set.iterator();
while(it.hasNext()){
    Cat c =(Cat)it.next();
    if(c.getName().equals("huahua")){
        flag=true;//find "huahua"
        break;
    }
}
if(flag)
    System.out.println("find cat named hauhua");
else
    System.out.println("can't find cat named huahua");
}

}

HashMap


/**

  • hashmap示例
  • 在字典中添加内容并且显示
  • @author ely

*/

public class DictionaryDemo {

public static void main(String args[]){
    Map<String,String> animal = new HashMap<String,String>();
    System.out.println("---input 3 words and derscription----");
    Scanner scanner = new Scanner(System.in);
    //添加数据
    int i=0;
    while(i<3){
        System.out.println("input the key:(word)");
        String key= scanner.next();
        System.out.println("input the value(description)");
        String value = scanner.next();
        animal.put(key, value);
        i++;
    }
    //打印输出value
    System.out.println("--------------------------------");
    System.out.println("使用迭代器输出value");
    Iterator<String> it = animal.values().iterator();
    while(it.hasNext()){
        System.out.print(it.next()+"  ");
    }
    
    //打印输出key和value的值
    //通过entryset方法
    System.out.println("--------------------------------");
    System.out.println("使用entrySet输出key和value");
    Set<Entry<String,String>> entryset = animal.entrySet();
    for(Entry<String,String> entry :entryset)
    {
        System.out.println(entry.getKey()+"-"+entry.getValue());
    }
    
    
    //通过key找到value
    //使用keySet方法
    System.out.println("--------------------------------");
    System.out.println("input the word you want search");
    String strSearch = scanner.next();
    //1.取得keyset
    Set<String> keySet = animal.keySet();
    //2.遍历keySet
    for(String key:keySet){
        if(strSearch.equals(key)){
            System.out.println("find it !"+"key-value:"+key+"-"+animal.get(key));
            break;
        }
            
    }
    
    
}

}


public class Goods {

private String id;
private String name;
private double price;

}

public class GoodsTest {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    Map<String,Goods> goodsMap= new HashMap<String,Goods>();
    System.out.println("input 3 goods information:");
    int i=0;
    while(i<3){
        System.out.println("请输出第"+(i+1)+"条信息");
        System.out.println("商品编号:");
        String goodsId =scanner.next();
        System.out.println("商品名称:");
        String goodsName = scanner.next();
        System.out.println("商品价格:");
        double goodsPrice = scanner.nextDouble();
        
        Goods goods = new Goods(goodsId,goodsName,goodsPrice);
        //添加到hashmap中
        goodsMap.put(goodsId, goods);
        i++;
    }
    //遍历map,输出商品信息
    System.out.println("输出商品信息为:");
    Iterator<Goods> itgoods = goodsMap.values().iterator();
    while(itgoods.hasNext()){
        System.out.println(itgoods.next());
    }

}

}

LinkedList


/**

  • linkedList
  • eg1:使用linkedlist对字符串进行管理
  • @author ely

*/
public class LinkedListDemo1_forString {

public static void main(String[] args){
    LinkedList<String> list = new LinkedList<String>();
    //链表添加数据
    list.add("apple");
    list.add("pear");
    //数据添加到链表的开始
    list.addFirst("bannana");
    //数据添加到链表末尾
    list.addLast("grape");
    //指定位置添加数据
    list.add(2, "orange");
    //显示链表所有数据
    System.out.println(list);
    //判断链表是否包含指定元素
    boolean flag= list.contains("grape");
    if(flag){
        System.out.println("grape find !");
    }else{
        System.out.println("not find grape");
    }
    
    System.out.println(list.get(3));
    System.out.println(list.getFirst());
    System.out.println(list.getLast());
}

}


public class Student {

private String stuNum;
private String stuName;
private int age;

}

/**

  • linkedList
  • eg1:使用linkedlist对自定义对象(student)进行管理
  • @author ely

*/

public class LinkedListDemo2_forObject {

public static void main(String[] args) {

    LinkedList<Student> stuList = new LinkedList<Student>();
    Student stu1 = new Student("001","Mike",18);
    Student stu2 = new Student("002","jack",20);
    Student stu3 = new Student("003","mary",19);
    //将学生添加到链表使用push完成
    //push & pop就是针对queue进行添加和取出操作的
    stuList.push(stu1);
    stuList.push(stu2);
    stuList.push(stu3);
    System.out.println("链表为:"+stuList);
    System.out.println("弹出的元素为:"+stuList.pop());
    System.out.println("调用pop后的链表为"+stuList);
    
    //使用peek方法获取并不移除元素
    System.out.println("调用peek方法的数据为:"+stuList.peek());
    System.out.println("调用peek方法后的链表为:"+stuList);
    
    //调用poll方法
    System.out.println("调用poll方法输出元素"+stuList.poll());
    //调用poll方法将获取元素的同时从链表中删除了元素
    System.out.println("调用poll方法后的链表为:"+stuList);

}

}

相关文章

  • Java8-流-简介

    几乎每个java应用程序都会制造和处理集合,要是没有集合,还能做什么呢?尽管集合对于几乎任何一个java应用都是不...

  • JAVA集合应用

    ArrayList使用 public class Notice { }/** list示例 公告需求管理 @aut...

  • 【Java 8实战笔记】引入流

    引入流 集合是Java中使用最多的API。几乎每个Java应用程序都会制造和处理集合。但集合的操作却远远算不上完美...

  • JDK 源码解析 —— 集合框架

    转载自:Java集合框架实例 1- 介绍 集合是程序和语言的基本思想。应用程序通常都会应用到集合,例如雇员的信息,...

  • Java集合的应用

    /*创建一个学生数组,存储三个学生对象并遍历 * 1.写一个学生类 * 2.创建学生数组 * 3. 创建学生对象 ...

  • 一篇文章,全面解读Android面试知识点

    Java Java基础 Java集合框架 Java集合——ArrayList Java集合——LinkedList...

  • 2018-11-04

    java学习笔记(五) 这篇简单的介绍流 集合是 Java 中使用最多的 API 。几乎每个 Java 应用都会制...

  • Java集合框架

    一.集合框架主要接口 二. 集合框架对数据结构的应用 1.简介  在Java语言中,Java的设计者对常用的的数据...

  • 收藏夹

    博文 Java 集合:Java 集合学习指南 Java 集合:Java 集合源码剖析 HashMap:HashMa...

  • Java 集合框架_开篇

    Java 集合框架系列 Java 集合框架_开篇Java 集合框架_ListJava 集合框架_ArrayList...

网友评论

      本文标题:JAVA集合应用

      本文链接:https://www.haomeiwen.com/subject/bnrxeftx.html