java集合

例子1

package com.atguigu.java2;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 21:11
 */
public class CollectionTest {

    @Test
    public void test1(){
        Collection coll = new ArrayList();

        //add(Object e):将元素e添加到coll中
        coll.add("AA");
        coll.add("BB");
        coll.add(123);
        coll.add(new Date());

        //size():获取添加的元素的个数
        System.out.println(coll.size());

        //addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中
        Collection coll1 = new ArrayList();
        coll1.add(456);
        coll1.add("cc");
        coll.addAll(coll1);

        System.out.println(coll.size());
        System.out.println(coll);


        //isEmpty():判断当前集合是否为空
        System.out.println(coll.isEmpty());

    }

}

例子2

package com.atguigu;

import com.atguigu.java.Person;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 *
 * 向collection接口的实现类的对象中添加数据obj时,要求obj所在类重写equals()
 *
 * @author shkstart
 * @create 2021-11-{DAY} 16:38
 */
public class java3 {

    @Test
    public  void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(false);

        coll.add(new Person("Jerry",20));


        //contains(Object obj):判断当前集合中是否包含obj
        //我们在判断时会调用obj对象所在类的equals()
        boolean contains = coll.contains(123);
        System.out.println(contains);
        System.out.println(coll.contains(new String("Tom")));
        System.out.println(coll.contains(new Person("Jerry",20)));


        //2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中
        Collection coll1 = Arrays.asList(123,456);
        System.out.println(coll.containsAll(coll1));
    }

    @Test
    public void test2(){
        //3.remove(Object obj):从当前集合中移除obj元素
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        coll.remove(123);
        System.out.println(coll);

        coll.remove(new Person("Jerry",20));
        System.out.println(coll);

        //4.removeAll(Collection coll1):从当前集合中移除coll1中所有元素
        Collection coll1 = Arrays.asList(123,4567);
        coll.removeAll(coll1);
        System.out.println(coll);

    }

    @Test
    public  void test3(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

//        //5.retainAll(Collection coll1):交集,获取当前集合和coll1集合的交集,并返回给当前集合
//        Collection coll1 = Arrays.asList(123,456,789);
//        coll.retainAll(coll1);
//        System.out.println(coll);

        //6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素相同
        Collection coll1 = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        System.out.println(coll.equals(coll1));

    }

}

例子3

package com.atguigu;

import com.atguigu.java.Person;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 *
 * 向collection接口的实现类的对象中添加数据obj时,要求obj所在类重写equals()
 *
 * @author shkstart
 * @create 2021-11-{DAY} 16:38
 */
public class java3 {

    @Test
    public  void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(false);

        coll.add(new Person("Jerry",20));


        //contains(Object obj):判断当前集合中是否包含obj
        //我们在判断时会调用obj对象所在类的equals()
        boolean contains = coll.contains(123);
        System.out.println(contains);
        System.out.println(coll.contains(new String("Tom")));
        System.out.println(coll.contains(new Person("Jerry",20)));


        //2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中
        Collection coll1 = Arrays.asList(123,456);
        System.out.println(coll.containsAll(coll1));
    }

    @Test
    public void test2(){
        //3.remove(Object obj):从当前集合中移除obj元素
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        coll.remove(123);
        System.out.println(coll);

        coll.remove(new Person("Jerry",20));
        System.out.println(coll);

        //4.removeAll(Collection coll1):从当前集合中移除coll1中所有元素
        Collection coll1 = Arrays.asList(123,4567);
        coll.removeAll(coll1);
        System.out.println(coll);

    }

    @Test
    public  void test3(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

//        //5.retainAll(Collection coll1):交集,获取当前集合和coll1集合的交集,并返回给当前集合
//        Collection coll1 = Arrays.asList(123,456,789);
//        coll.retainAll(coll1);
//        System.out.println(coll);

        //6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素相同
        Collection coll1 = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        System.out.println(coll.equals(coll1));

    }

    @Test
    public void test4(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        //7.hashCode():返回当前对象的哈希值
        System.out.println(coll.hashCode());

        //8.集合-->数组:toArray()
        Object[] arr = coll.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

        //拓展:数组-->集合
        List<String> list = Arrays.asList(new String[]{"AA","BB","CC"});
        System.out.println(list);

        List<int[]> arr1 = Arrays.asList(new int[]{123, 456});
        System.out.println(arr1);       //这种情况下,arr1中只有一个元素


        List arr2 = Arrays.asList(new int[]{123, 456});
        System.out.println(arr2.size());//这种情况,还是一个元素

        List arr3 = Arrays.asList(new Integer[]{123, 456});
        System.out.println(arr3.size());     //这种情况是2个元素

        //iterator():返回Iterator接口的实例,用于遍历集合元素,放在InteratorTest.java中测试
        
    }

}

例子4

package com.atguigu.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 *
 * 集合元素的遍历操作,使用Iterator接口
 * @author shkstart
 * @create 2021-11-{DAY} 17:41
 */
public class IteratorTest {

    @Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        Iterator iterator = coll.iterator();

        //使用迭代器遍历collection
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }



    }

}

例子5

package com.atguigu.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 *
 * 集合元素的遍历操作,使用Iterator接口
 * 1.内部的方法:hasNext()和next()
 * 2.集合对象每次调用iterator()方法都得到一个全新的迭代器对象
 * 默认游标都在集合的第一个元素之前
 * 3.内部定义了remove(),可以在遍历的时候,删除集合中的元素,此方法不同于直接调用remove()
 * @author shkstart
 * @create 2021-11-{DAY} 17:41
 */
public class IteratorTest {

    @Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        Iterator iterator = coll.iterator();

        //使用迭代器遍历collection
        //hasNext():判断是否还有下一个元素
        while(iterator.hasNext()){
            //next():①指针下移 ②将下移以后集合位置上的元素返回
            System.out.println(iterator.next());
        }
    }

    @Test
    public void test3(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        //删除集合中"Tom"
        Iterator iterator = coll.iterator();

        //使用迭代器遍历collection
        //hasNext():判断是否还有下一个元素
        while(iterator.hasNext()){
            Object obj = iterator.next();
            if("Tom".equals(obj)){
                iterator.remove();
            }
        }
        iterator = coll.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }

}

例子6

package com.atguigu.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:17
 */
public class ForTest {

    @Test
    public void test1(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry",20));
        coll.add(new String("Tom"));
        coll.add(false);

        //for(集合元素的类型 局部变量:集合对象)
        //内部仍然调用迭代器
        for(Object obj:coll){
            System.out.println(obj);
        }
    }

    @Test
    public void test2(){
        int[] arr = new int[]{1,2,3,4,5,6};
        //for(数组元素的类型 局部变量:数组对象)
        for(int i:arr){
            System.out.println(i);
        }
    }
}

例子7

package com.atguigu.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:05
 */
public class ListTest {

    @Test
    public void test2(){
        ArrayList list = new ArrayList();
        list.add(123);
        list.add(456);
        list.add("AA");
        list.add(new Person("Tom",12));
        list.add(456);

        //int indexof(Object obj):返回obj在当前集合中首次出现的位置
        int index = list.indexOf(456);
        System.out.println(index);

        //int LastIndexOf(Object obj):返回obj在当前集合中末次出现的位置
        System.out.println(list.lastIndexOf(456));

        //Object remove(int index):移除指定index位置的元素,并返回此元素
        Object obj = list.remove(0);
        System.out.println(obj);
        System.out.println(list);

        //Object set(int index,Object ele):设置指定index位置的元素为ele
        list.set(1, "cc");
        System.out.println(list);

        //list sublist(int fromIndex,int toIndex):返回从fromIndex到toIndex位置的左闭右开的子集合
        List subList = list.subList(2,4);
        System.out.println(subList);
        System.out.println(list);

    }

    @Test
    public void test1(){
        ArrayList list = new ArrayList();
        list.add(123);
        list.add(456);
        list.add("AA");
        list.add(new Person("Tom",12));
        list.add(456);

        System.out.println(list);

        //void add(int index,Object ele):在index位置插入ele元素
        list.add(1,"BB");
        System.out.println(list);

        //boolean addAll(int index,Collection eles):从index位置开始将eles中的所有元素添加到后面
        List list1 = Arrays.asList(1,2,3);
       // list.addAll(list1);  //9;
        list.add(list1);
        System.out.println(list.size());


        //Object get(int index):获取指定index位置的元素
        System.out.println(list.get(0));



    }

}

例子8

package com.atguigu.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 18:05
 */
public class ListTest {

    @Test
    public void test3(){
        ArrayList list = new ArrayList();
        list.add(123);
        list.add(456);
        list.add("AA");

        //方式1:Iterator迭代器方式
        Iterator iterator = list.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

        System.out.println("*****************");

        //方式2:
        for(Object obj:list){
            System.out.println(obj);
        }

        System.out.println("*********************");

        //方式3:普通for循环
        for(int i=0; i<list.size(); i++){
            System.out.println(list.get(i));
        }



    }



}

例子9

package com.atguigu.java1;

import org.junit.Test;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:04
 */
public class SetTest {

    @Test
    public void test1(){
        Set set = new HashSet();
        set.add(456);
        set.add(123);
        set.add("AA");
        set.add("CC");
        set.add(new User("Tom",12));
        set.add(129);
        set.add(123);
        set.add(new User("Tom",12));

        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

    }


}

例子10

package com.atguigu.java1;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:28
 */
public class User implements Comparable{
    private String name;
    private  int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //按照姓名的从大到小排列

    public int compareTo(Object o){
        if(o instanceof  User){
            User user = (User) o;
            int compare = -this.name.compareTo(user.name);
            if(compare!=0){
                return  compare;
            }else{
                return Integer.compare(this.age,user.age);
            }
        }else{
            throw new RuntimeException("输入的类型不匹配");
        }
    }

}

package com.atguigu.java1;

import org.junit.Test;

import java.util.Iterator;
import java.util.TreeSet;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 20:38
 */
public class TreeSetTest {

    @Test
    public void test1(){
        TreeSet Set = new TreeSet();

        Set.add(new User("TOm",12));
        Set.add(new User("Jerry",32));
        Set.add(new User("Jim",2));
        Set.add(new User("Mike",65));
        Set.add(new User("Jack",39));

        Iterator iterator = Set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }



    }

}

例子11

package com.atguigu.java1;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 20:38
 */
public class TreeSetTest {

    @Test
    public void test2(){
        Comparator com = new Comparator() {
            public int compare(Object o1,Object o2){
                if(o1 instanceof User && o2 instanceof  User){
                    User u1 = (User) o1;
                    User u2 = (User) o2;
                    return  Integer.compare(u1.getAge(),u2.getAge());
                }else{
                    throw new RuntimeException("输入的数据类型不匹配");
                }
            }
        };

        TreeSet set = new TreeSet(com);

        set.add(new User("TOm",12));
        set.add(new User("Jerry",32));
        set.add(new User("Jim",2));
        set.add(new User("Mike",65));
        set.add(new User("Jack",39));


        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }



}

例子12

package com.atguigu.java;

import org.junit.Test;

import java.util.*;

/**
 * @author shkstart
 * @create 2021-12-{DAY} 15:33
 */
public class MapTest {



    @Test
    public void test5(){
        Map map = new HashMap();
        map.put("AA",123);
        map.put(45,123);
        map.put("BB",56);

        Set set = map.keySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

        Collection values = map.values();
        for(Object obj:values){
            System.out.println(obj);
        }

        //遍历所有key-value

        Set entrySet = map.entrySet();
        Iterator iterator1 = entrySet.iterator();
        while(iterator1.hasNext()){
            Object obj = iterator1.next();
            Map.Entry entry = (Map.Entry) obj;
            System.out.println(entry.getKey() + "------->" + entry.getValue());
        }

        Set keySet = map.keySet();
        Iterator iterator2 = keySet.iterator();
        while(iterator2.hasNext()){
            Object key = iterator2.next();
            Object value = map.get(key);
            System.out.println(key + "------->" + value);
        }

    }

    @Test
    public void test4(){
        Map map = new HashMap();
        map.put("AA",123);
        map.put(45,123);
        map.put("BB",56);

        System.out.println(map.get(45));

        boolean isExist = map.containsKey("BB");
        System.out.println(isExist);
        isExist = map.containsValue(123);
        System.out.println(isExist);

        map.clear();

        System.out.println(map.isEmpty());
    }

    @Test
    public void test3(){
        Map map = new HashMap();
        map.put("AA",123);
        map.put(45,123);
        map.put("BB",56);
        map.put("AA",87);

        System.out.println(map);

        Map map1 = new HashMap();
        map1.put("CC",123);
        map1.put("DD",123);

        map.putAll(map1);

        System.out.println(map);

        Object value = map.remove("CC");
        System.out.println(value);
        System.out.println(map);

        map.clear();
        System.out.println(map);

        System.out.println(map.size());

    }

    @Test
    public void test2(){
        Map map = new HashMap();
        map = new LinkedHashMap();
        map.put(123,"AA");
        map.put(345,"BB");
        map.put(12,"cc");

        System.out.println(map);
    }
}

例子13

Properties

name=Tom王少锋
password=abc123

PropertiesTest

package com.atguigu.java;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author shkstart
 * @create 2021-12-{DAY} 17:37
 */
public class PropertiesTest {

    public static void main(String[] args) throws Exception {
        FileInputStream fis = null;
        try {
            Properties pros = new Properties();
            fis = new FileInputStream("jdbc.properties");
            pros.load(fis);     //加载对应的流文件

            String name = pros.getProperty("name");
            String password = pros.getProperty("password");

            System.out.println(name);
            System.out.println(password);
        } catch (IOException e) {
            e.printStackTrace();
        }

        fis.close();

    }

}

例子14

package com.atguigu.java;

import org.junit.Test;

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

/**
 * @author shkstart
 * @create 2021-12-{DAY} 16:46
 */
public class TreeMapTest {

    @Test
    public void test1(){
        TreeMap map = new TreeMap();
        User u1 = new User("Tom", 23);
        User u2 = new User("Jerry", 32);
        User u3 = new User("Jack", 20);
        User u4 = new User("Rose", 18);


        map.put(u1,18);
        map.put(u2,89);
        map.put(u3,76);
        map.put(u4,100);

        Set entrySet = map.entrySet();
        Iterator iterator1 = entrySet.iterator();
        while(iterator1.hasNext()){
            Object obj = iterator1.next();
            Map.Entry entry = (Map.Entry) obj;
            System.out.println(entry.getKey() + "------->" + entry.getValue());
        }

    }

}

package com.atguigu.java;

/**
 * @author shkstart
 * @create 2021-11-{DAY} 19:28
 */
public class User implements Comparable{
    private String name;
    private  int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //按照姓名的从大到小排列

    public int compareTo(Object o){
        if(o instanceof  User){
            User user = (User) o;
            int compare = -this.name.compareTo(user.name);
            if(compare!=0){
                return  compare;
            }else{
                return Integer.compare(this.age,user.age);
            }
        }else{
            throw new RuntimeException("输入的类型不匹配");
        }
    }

}

例子15

package com.atguigu.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Collections:操作Collection,map的工具类
 * @author shkstart
 * @create 2021-12-{DAY} 17:48
 */
public class CollectionsTest {

    @Test
    public void test2(){
        List list = new ArrayList();
        list.add(123);
        list.add(43);
        list.add(765);
        list.add(-97);
        list.add(0);

        //将list复制到另一个dest中
        List dest = Arrays.asList(new Object[list.size()]);
        Collections.copy(dest,list);
        System.out.println(dest);

        /*
        * Collection类中提供了多个synchronizedXxx()方法,
        * 该方法可使将指定的集合包装成线程同步的集合,从而可以解决
        * 多线程并发访问集合时的线程安全问题
        * */
        List list1 = Collections.synchronizedList(list);
        

    }

    @Test
    public void test1(){
        List list = new ArrayList();
        list.add(123);
        list.add(43);
        list.add(765);
        list.add(765);
        list.add(765);
        list.add(-97);
        list.add(0);

        System.out.println(list);
//        //翻转
//        Collections.reverse(list);
//        //随机处理
//        Collections.shuffle(list);
        //排序
//        Collections.sort(list);
        //交换指定位置上的数据
//        Collections.swap(list,1,2);
        //System.out.println(list);
        //找765出现了多少次
//        int frequency = Collections.frequency(list, 765);
//        System.out.println(frequency);



    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值