程序员旅行·在路上

Java_集合

2019-08-16  本文已影响0人  书虫大王X

1.知识点:

1.集合
2.lambda表达式
3.自己创建的代码块

2.知识点的运用:

1.集合的简介:

结构图

集合分为Collection抽象接口和Map接口,其中Collection接口使用的较多。
Collection接口又分为list接口和set集合:

2.Collection接口方法:

 Collection<String> t1 = new ArrayList();
 t1.add("Jack");
 t1.add("Merry");
 t1.remove("Jack");
 System.out.println(t1);
Collection<String> t1 = new ArrayList();
 t1.add("Jack");
 t1.add("Merry");
 System.out.println(t1.size());
Collection<String> t1 = new ArrayList();
 t1.add("Jack");
 t1.add("Merry");
 if (t1.contains("Merry")){
 System.out.println("有merry");
 }else{
 System.out.println("没有merry");
 }
Collection<String> t1 = new ArrayList();
 t1.add("Jack");
 t1.add("Merry");
 if(t1.isEmpty()){
 System.out.println("是空的");
 }
Collection<String> t1 = new ArrayList();
 t1.add("Jack");
 t1.add("Merry");
 Collection<String> t2 = new ArrayList<>();
 t2.add("Merry");
 t2.add("Jack");
 t2.add("Rose");

 if (t1.equals(t2)){
 System.out.println("两个集合相同");
 } else{
 System.out.println("两个集合不相同");
 }
Collection<String> t1 = new ArrayList();
 t1.add("Jack");
 t1.add("Merry");
 t1.clear();
 System.out.println(t1);

3.Collection集合的遍历:

// hasNext 判断是否有元素
 // next 获取下⼀个对象
 // remove 删除当前遍历过后的对象

Collection<String> t2 = new ArrayList<>();
 t2.add("Merry");
 t2.add("Jack");
 t2.add("Rose");
 Iterator iterator = t2.iterator();
 while (iterator.hasNext()){
 System.out.println(iterator.next());
 }
Collection<String> t2 = new ArrayList<>();
 t2.add("Merry");
 t2.add("Jack");
 t2.add("Rose");
for (String obj: t2){
 System.out.println(obj);
 }

4.ArrayList集合:ArrayList实现了Collection接口,Collection有的方法ArrayList都有,并且在其基础上还有更多的方法。( 集合⾥⾯只能存放对象)

ArrayList<Integer> score = new ArrayList<>();
//在末尾添加
 score.add(1);
 score.add(2);
 score.add(3);
//在指定位置插⼊
 score.add(0,4);
 System.out.println(score);
ArrayList<Integer> score = new ArrayList<>();
 score.add(1);
 score.add(2);
 score.add(3);
 score.get(1);
ArrayList<Integer> score = new ArrayList<>();
 score.add(1);
 score.add(2);
 score.add(3);
 score.set(0,0);
 System.out.println(score);
ArrayList<Integer> score = new ArrayList<>();
 score.add(1);
 score.add(2);
 score.add(3);
//删除指定位置的元素
 score.remove(0);
//删除指定的对象
 score.remove((Integer)2); 
 //删除所有 清空
 score.clear();
 System.out.println(score);
ArrayList<Integer> score = new ArrayList<>();
 score.add(1);
 score.add(2);

ArrayList<Integer> a2 = new ArrayList<>();
 a2.add(3);

 score.addAll(a2);
ArrayList<Integer> score = new ArrayList<>();
 score.add(1);
 score.add(2);
 ArrayList<Integer> a3 = new ArrayList<>();
 a3.add(1);
 a3.add(2);
 score.retainAll(a3); 
 ArrayList<Integer> a4 = new ArrayList<>();
 a4.add(1);
 a4.add(2);
 a4.add(1);
//第⼀次出现的位置
 System.out.println(a4.indexOf(1));
//最后⼀次出现的位置
 System.out.println(a4.lastIndexOf(1));
 Integer[] objects = new Integer[a4.size()];
 a4.toArray(objects);
 for (Integer i: objects){
 System.out.println(i);
 }

5.Set集合:
Set集合的基本方法,与list接口的基本一样,但Set里面元素不能重复,如果重复添加不进去;其内部使用HashMap来实现,键值对中键key不能重复。内部是通过计算每个key的hash值,是在对象的地址的基础上得到的值;Object类有一个方法public native int hashCode();用来返回hash值;切记重写类的equals方法一定要重写hashCode方法,hashCode和equals详解。Object类还有一个方法protected native Object clone() throws CloneNotSupportedException;用于复制对象。

排序方法一:
使用匿名类调用Comparator方法:

 //主函数中
TreeSet<Person> treeSet = new TreeSet<>(new Comparator<Person>() {
            @Override
            public int compare(Person person, Person t1) {
                if (person.age != t1.age) {
                    return person.age - t1.age;
                } else {
                    return person.name.compareTo(t1.name);
                }
            }
        });
        Person p1 = new Person("小王",20);
        Person p2 = new Person("小张",10);
        Person p3 = new Person("小王",30);
        treeSet.add(p1);
        treeSet.add(p2);
        treeSet.add(p3);
        //加到集合中的对象就会被排序

class  Person implements Comparable{
    String name;
    int age;
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }

方法二:
使用lambda表达式(实现compareTo方法)

      //主函数中
       TreeSet<Person> treeSet = new TreeSet<>((Person p1,Person p2) ->p1.compareTo(p2));
        Person p1 = new Person("小王",20);
        Person p2 = new Person("小张",10);
        Person p3 = new Person("小王",30);
        treeSet.add(p1);
        treeSet.add(p2);
        treeSet.add(p3);

class  Person implements Comparable{
    String name;
    int age;
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }

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

    @Override
    public int compareTo(Object o) {
        //判断o对象的类型
        if (o instanceof  Person){
            Person o1 = (Person)o;
            //比较的方法
            if (this.age != o1.age){
                return this.age - o1.age;
            }else {
                return this.name.compareTo(o1.name);
            }
        }else {
            return -1;
        }
    }
}

方法三:

6.包装类:
byte char int long float double boolean属于基本的数据类型,
Byte Char Integer Long Float Double Boolean属于包装类,在系统运行时,会按照自己的需求,自动地在对应的基本数据类型和包装类之间进行转换。
7.Lambda表达式的使用:

public class MyClass {
    public static void main(String[] args){
        //方法一
        Arraty1 ac = new Arraty1();
        int[] num = {1,2,3,4,5,6};
        ykClass yk = new ykClass();
        ac.test(num,yk);

        //方法二:匿名方法
        Arraty1 ac = new Arraty1();
        int[] num = {1,2,3,4,5,6};
        ac.test(num, new Show() {
            @Override
            public void custonShow(int element) {
                System.out.println(element);
            }
        });


        //方法三: 使用Lambda表达式
        //如果参数是一个接口类对象,且接口里面只有一个方法
        //可以省略方法名
        Arraty1 ac = new Arraty1();
        int[] num = {1,2,3,4,5,6};
        ac.test(num,(int element) ->{
            System.out.println(element);
        });

        //Lambda表达式
        Arraty1 ac = new Arraty1();
        int[] num = {1,2,3,4,5,6};
        ac.test(num,element ->{
            System.out.println(element);
        });

        //Lambda表达式
        Arraty1 ac = new Arraty1();
        int[] num = {1,2,3,4,5,6};
        ac.test(num,element ->System.out.println(element));
     }
}


class Arraty1{
    //闭包 enclusure 把函数作为一个方法的参数
    public void test(int[] target,Show show){
        for (int elenment : target){
            show.custonShow(elenment);
        }
    }
}


interface Show{
    void custonShow(int element);
}

class ykClass implements Show{
    @Override
    public void custonShow(int element) {
        System.out.println(element);
    }
}

8.数组排序:

public class MyClass {
    public static void main(String[] args){

        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(2);
        arrayList.add(1);
        arrayList.add(4);
        arrayList.add(3);
        //方法一:系统方法 
        arrayList.sort(Comparator.comparingInt(Integer::intValue));
        //方法二:
        arrayList.sort(new ykCompare());
        //方法三:匿名调用
        arrayList.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer integer, Integer t1) {
                return integer - t1;
            }
        });
       //方法四:Lambda表达式
        arrayList.sort((Integer i1,Integer i2) ->{
            return i1 - i2;
        });
        System.out.println(arrayList);
    }
} 

class ykCompare implements Comparator{
    //系统默认提供的方法不能满足我们的要求时,需要自己重写方法
    @Override
    public int compare(Object o, Object t1) {
        int mo = (int)o;
        int mt1 = (int)t1;

        return mo - mt1;
    }
}

9.HashMap:
1)概念:

HashMap<String,Integer> score = new HashMap<>();
public V put(K key, V value)
public void putAll(Map<? extends K, ? extends V> m)
//添加对象:键值对
score.put("Chinese",90);
score.put("English",88);
score.put("Math",100);

//相同的情况,默认更改其值
score.put("Chinese",95);
System.out.println(score);

输出:{English=88, Chinese=95, Math=100}
public int size()
public Set<K> keySet()

HashMap<String,Integer> score = new HashMap<>();
//添加对象:键值对
score.put("Chinese",90);
score.put("English",88);
score.put("Math",100);
System.out.println(score.keySet());

输出:[English, Chinese, Math]
public Collection<V> values()

System.out.println(score.values());
public V get(Object obj)


System.out.println(score.get("Chinese"));
public Set<Entry<K, V>> entrySet()

System.out.println(score.entrySet());
输出:[English=88, Chinese=95, Math=100]
//1.通过遍历key得到每一个key对应的值
for (String key : score.keySet()) {
    //通过key得到值
    int value  = score.get(key);
    System.out.println("key:"+key+" "+"value;"+value);
}

输出:
key:English value;88
key:Chinese value;95
key:Math value;100
//2.通过entrySet 得到Entry对象的集合
//一个entry管理一个键值对 getKey getValue
Set<Map.Entry<String ,Integer>> entrys = score.entrySet();
for (Map.Entry entry : entrys) {
    //得到entry对应的key
    String key = (String)entry.getKey();
    //得到entry对应的value
    Integer value = (Integer)entry.getValue();
    System.out.println("key:"+key+" "+"value;"+value);
}

输出:
key:English value;88
key:Chinese value;95
key:Math value;100
public V replace(K key, V value)

score.replace("English",90);
System.out.println(score);

输出:{English=90, Math=100}
public boolean isEmpty()
public void clear();

10.Collections工具类:

List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);  
// 6  
System.out.println(java.util.Collections.min(intList));  
List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);  
// 99  
System.out.println(java.util.Collections.max(intList));  
List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);  
Collections.shuffle(intList);  
// 一次测试的结果  
// [6, 18, 33, 24, 99, 9]  
System.out.println(intList); 
// 生成一个由10个100组成的整数列表  
List<Integer> nCopiesList = Collections.nCopies(10, 100);  
//[100, 100, 100, 100, 100, 100, 100, 100, 100, 100]  
System.out.println(nCopiesList);  
List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);  
Collections.sort(intList);  
List<Integer> intList = Arrays.asList(33, 24, 18, 6, 9, 99);  
// 2  
System.out.println(Collections.binarySearch(intList, 18));  

List<String> testList = Arrays.asList("A", "B", "C", "D");  
int freq = Collections.frequency(testList, "A");  
// 1  
System.out.println(freq); 
int index = Collections.indexOfSubList(Arrays.asList("A", "B", "C"),  
Arrays.asList("B"));  
// Print 1  
System.out.println(index);  
int lastIndex = Collections.lastIndexOfSubList(  
        Arrays.asList("A", "B", "C", "B"), Arrays.asList("B"));  
// Print 3  
System.out.println(lastIndex);  
List<String> reverseCandidate = Arrays.asList("A", "B", "C");  
Collections.reverse(reverseCandidate);  
// [C, B, A]  
System.out.println(reverseCandidate);
上一篇下一篇

猜你喜欢

热点阅读