第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

java 類集(Collection)筆記

標(biāo)簽:
Java

1.类集Collection
1.1基本概念:类集就是一个动态的对象数组,是对一些实现好的数据结构进行了包装。
1.2主要接口
1)List:是Collection的子接口,对Collection进行了大量的扩充。允许内容重复。
2)Set:是Collection的子接口,没有对Collection进行扩充。不允许内容重复。
3)Map:是存放一对值得最大父接口,以ke-value的形式存放。
4)Iterator:集合的输出接口,只能进行从前到后的单项输出。

2.List
2.1常用子类:ArrayList

1)实例化

List<String> allList = new ArrayList<String>();

2)扩展方法

//在指定位置增加元素
allList.add("qwer");
allList.add(0,"Hello");
//放回指定位置元素
allList.get(0);
//删除指定位置的元素
allList.remove(0);
//替换指定位置的元素
allList.set(0,"Hello");
 //是否为空
allList.isEmpty();  
//字符串是否存在 
System.out.println(allList.contains("Hello")?"\"Hello\"存在":\"Hello\"不存在");    
//集合截取
allList.subList(2,3) ; 
//返回元素所在位置
allList.indexOf("Hello") ;

3)输出内容

for(int i=0;i<allList.size();i++){
    System.out.print(allList(i)+",");
}

4)转换为对象数组

String str[] = allList.toArray(new String[]{});

3.Set
3.1常用子类
1)HashSet:散列存放,不能重复,没有顺序。
2)TreeSet:对输入放入数据进行有序排列。
3.2实例

Set<String> allSet = new HashSet<String>();
SorteSet<String> sorteset = new TreeSet<String>();

3.3TreeSet指定规则排序

public class TreeSetComparable {
    public static void main(String args[]){
        Set<Person> allSet = new TreeSet<Person>();
        allSet.add(new Person("张三",25));
        allSet.add(new Person("李四",24));
        allSet.add(new Person("王五",29));
        System.out.println(allSet);
    }
}
//定义Person类,实现比较器
class Person implements Comparable<Person>{
    private String name;
    private Integer number;
    public Person(String name,Integer number){
        this.name=name;
        this.number=number;
    }
//覆写toString方法
    public String toSting(){
        return "姓名:"+this.name+"; 年龄:"+this.number;
    }
//覆写compareTo方法
    public int compareTo(Person person){
        if (this.number>person.number){
            return 1;
        }
        else if (this.number<person.number){
            return -1;
        }
        else {
            return 0;
        }
    }
}
4.Iterator

Iteration接口:迭代输出。
输入实例

import java.util.Iterator;
import java.util.Set; // Collection 的子接口
import java.util.HashSet; // Set 的实现类
class Test{
    public static void main(String args[]){
      System.out.println("<------- Iterator【迭代器】 讲解--------->");
      //Iterator <-- Collection <-- Set <-- HashSet
      //                        <-- List <-- ArrayList
      //Iterator【迭代器】 :hasNext(); next();
      //调用 Set 对象的 Iterator 方法,会生成一个迭代器对象,该对象用于遍历整个 Set
      Set<Integer> itSet = new HashSet<Integer>();
      itSet.add(1111);
      itSet.add(2222);
      itSet.add(3333);
      itSet.add(1234);
      Iterator<Integer> it = itSet.iterator();
      while(it.hasNext()){
          Integer i = it.next();
          System.out.println(i);
      }
    }
}

ListIterator:双向迭代输出。

foreach

输出格式

for(类 对象:集合){
//集合操作
}

Enumeration:最早的迭代输出接口。

Map(键值对(key-value))

HashMap:无序存放,Key不允许重复。
增加和取得内容

Map<String,String> map = new HashMap<String>();
map.put("qq","ww");
String val = map.get("qq");
String key = map.get("ww");

判断指定内容是否存在

map.containsKey("qq");    //返回 true or false
map.containsVaule("ww")

输出全部Key或者Value

Set<String> keys = map.Keyset();    //Set<String> values =  map.value();
Iterator iter = keys.iterator();
While (iter.hashNext()){
    String str = iter.next();
    System.out.println();
}

HashTable:旧操作类。

TreeMap:可以排序的Map集合,按照集合的Key排序,Key不允许重复。

WeakHashMap:弱引用的Map集合,当集合的某些内容不再使用时清楚掉无用的内容,使用gc进行回收。

IdentityHashMap:Key可以重复的Map集合。

Map输出

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class MapDome01 {
    public static void main(String[] args){
        Map<String,String> map = new HashMap<String,String>();
        map.put("慕课","idcbgp.cn");
        map.put("百度","www.baidu.com");
        map.put("谷歌","www.google");
//        ---------Iteration输出
//        Set<Map.Entry<String,String>> str = map.entrySet();
//        Iterator<Map.Entry<String,String>> it = str.iterator();
//        while (it.hasNext()){
//            Map.Entry<String,String> qq = it.next();
//            System.out.println("名称---》"+qq.getKey()+
//            "网址---》" + qq.getValue());

//        }
//        -----foreach输出
        for (Map.Entry<String,String> str:map.entrySet()){
            System.out.println("名称---》"+str.getKey()+ "网址---》" + str.getValue());
        }
    }
}

Map使用非系统类作为Key值

import java.util.Map;
import java.util.HashMap;

class Person1{
    private String name;
    private int age;
    public Person1(String name,int age){
        this.name = name;
        this.age = age;
    }
    public String toString(){
        return "名字:" + this.name + " 年龄:" + this.age;
    }
}
public class MapDome01 {
    public static void main(String[] args){
        Person1 per = new Person1("袁艺",23);
        Map<String,Person1> map = new HashMap<String,Person1>();
        map.put("yuanyi",per);
        System.out.println(map.get("yuanyi"));

    }
}
集合工具类:Collections

1)为集合增加内容

List<String> all = new ArrayList();
Collection.addAll(all,"11","22","33");

2)反转集合中的内容

Collection.reverse(all);    //内容反转保存

3)检索内容

int point = Collection.binarySearch(all,"11");    //返回1
int point = Collection.binarySearch(all,"44");     //返回-1   

4)替换集合中的内容

Collection.replaceAll(all,"11","00");    //输出结果 00 22 33

5)集合排序

Collection.sort(all);

6)交换指定位置的内容

Collection.swap(all,0,2);    //输出33 22 11
其他集合类

Stack:栈是采取先进后出的数据存储方式。

Propertes:属性类。

1)设置和取得属性

Properties pro = new Properties
pro.setProperties("BJ","Beijing");
pro.setProperties("SH",Shanghai");

2)将属性保存在普通文件中

File file = new File("D:"+File.separator+"area.properties");    //设置属性文件的储存的路径
pro.stor(new FileOutputStream(file),"Area Info");    //保存属性到普通文件中,并设置注释内容

3)从属性文件内容中读取内容
pro.load(new FileInPutStream(file));

类集框架的基础结构

點(diǎn)擊查看更多內(nèi)容
3人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
軟件測(cè)試工程師
手記
粉絲
172
獲贊與收藏
905

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消