Iterable&Thread

总的要掌握:
1、创建对象
2、添加元素
3、取出某个元素    
4、遍历    
ArrayList
1、创建对象
2、添加元素
3、取出某个元素    
4、遍历 
public class ArrayListTest {
    public static void main(String[] args) {
        // 创建集合对象
        //ArrayList<String> list = new ArrayList<>();
        LinkedList<String> list = new LinkedList<>();
        // 添加元素
        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        // 从集合中取出某个元素
        // List集合有下标
        String firstElt = list.get(0);
        System.out.println(firstElt);
        // 遍历(下标方式)
        for(int i = 0; i < list.size(); i++){
            String elt = list.get(i);
            System.out.println(elt);
        }
        // 遍历(迭代器方式,这个是通用的,所有Collection都能用)
        Iterator<String> it = list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

        // while循环修改为for循环
        /*for(Iterator<String> it2 = list.iterator(); it2.hasNext(); ){
            System.out.println("====>" + it2.next());
        }*/

        // 遍历(foreach方式)
        for(String s : list){
            System.out.println(s);
        }
    }
}    
LinkedList
与ArrayList一样
1、创建对象
2、添加元素
3、取出某个元素    
4、遍历 
ublic class LinkedLIst {
    public static void main(String[] args) {
        LinkedList<Integer> ll = new LinkedList<>();
        //增加元素
        ll.add(1);
        ll.add(2);
        ll.add(3);
        ll.add(7);
        ll.add(5);
        System.out.println("取出某个元素");
        System.out.println(ll.get(2));
        System.out.println("遍历:for下标");
        for (int i = 0; i < ll.size(); i++) {
            System.out.println(i);
        }
        System.out.println("遍历:fore");
        for (Integer i :ll) {
            System.out.println(i);
        }
        System.out.println("遍历:迭代器");
        Iterator<Integer> it = ll.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
    }
}    
HashSet
1、创建对象
2、添加元素
3、取出某个元素 
4、测试HashSet集合的特点:无序不可重复。
public class HashSetTest {
    public static void main(String[] args) {
        // 创建集合对象
        HashSet<String> set = new HashSet<>();
        // 添加元素
        set.add("abc");
        set.add("def");
        set.add("king");
        // set集合中的元素不能通过下标取了。没有下标
        // 遍历集合(迭代器)
        Iterator<String> it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        // 遍历集合(foreach)
        for(String s : set){
            System.out.println(s);
        }

        set.add("king");
        set.add("king");
        set.add("king");
        System.out.println(set.size()); //3 (后面3个king都没有加进去。)

        set.add("1");
        set.add("10");
        set.add("2");

        for(String s : set){
            System.out.println("--->" + s);
        }
        // 创建Set集合,存储Student数据
        Set<Student> students = new HashSet<>();
        
        Student s1 = new Student(111, "zhangsan");
        Student s2 = new Student(222, "lisi");
        Student s3 = new Student(111, "zhangsan");
        students.add(s1);
        students.add(s2);
        students.add(s3);
        System.out.println(students.size()); // 2
        // 遍历
        for(Student stu : students){
            System.out.println(stu);
        }
    }
}
class Student {
    int no;
    String name;

    public Student() {
    }

    public Student(int no, String name) {
        this.no = no;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Student{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return no == student.no &&
                Objects.equals(name, student.name);
    }
    @Override
    public int hashCode() {
        return Objects.hash(no, name);
    }
}    
TreeSet
每个集合对象的创建(new1.2、向集合中添加元素
1.3、从集合中取出某个元素
1.4、遍历集合
1.5、测试TreeSet集合中的元素是可排序的。
1.6、测试TreeSet集合中存储的类型是自定义的。
1.7、测试实现Comparable接口的方式
1.8、测试实现Comparator接口的方式(最好测试以下匿名内部类的方式)
    
public class TreeSetTest {
    public static void main(String[] args) {
        TreeSet<String> ts = new TreeSet<>();
        ts.add("提莫队长");
        ts.add("阿叁");
        ts.add("阿肆");
        ts.add("阿伍");
        ts.add("阿肆");
        ts.add("阿肆");
        ts.add("阿肆");
        ts.add("阿肆");
        ts.add("阿陆");
        ts.add("阿柒");
        //遍历fore
        for (String s : ts) {
            System.out.println(s);
        }
        //迭代器
        Iterator<String> it = ts.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println("------实现接口方式--------");
        TreeSet<Vip> ts2 = new TreeSet<>();
        ts2.add(new Vip(1));
        ts2.add(new Vip(4));
        ts2.add(new Vip(3));
        ts2.add(new Vip(4));
        ts2.add(new Vip(2));
        //遍历
        for (Vip v : ts2) {
            System.out.println(v);
        }
        System.out.println("传入比较器,在形参new");
        //直接传
        //TreeSet<Customer> ts3 = new TreeSet<>(new Ccomparator());
        //使用匿名内部类方式,直接new一个Comparator比较器,实现其中方法
        TreeSet<Customer> ts3 = new TreeSet<>(new Comparator<Customer>() {
            @Override
            public int compare(Customer o1, Customer o2) {
                return o1.no - o2.no;
            }
        });
        ts3.add(new Customer(533));
        ts3.add(new Customer(555));
        ts3.add(new Customer(554));
        ts3.add(new Customer(56));
        ts3.add(new Customer(555));
        //遍历
        for (Customer c :ts3) {
            System.out.println(c);
        }
    }
}
//要实现排序,第一种方式,实现Comparable接口
class Vip implements Comparable<Vip>{
    int no;
    public Vip(int no){
        this.no = no;
    }
    @Override
    public String toString() {
        return "Vip{" +
                "no=" + no +
                '}';
    }
    @Override
    public int compareTo(Vip vip) {
        return this.no - vip.no;
    }
}
//第二种方式,创建一个比较器传进去
class Customer{
    int no;
    public Customer(int no){
        this.no = no;
    }
    @Override
    public String toString() {
        return "Customer{" +
                "no=" + no +
                '}';
    }
}
//比较器
class Ccomparator implements Comparator<Customer>{

    @Override
    public int compare(Customer o1, Customer o2) {
        return o1.no - o2.no;
    }
}    
HashMap
1、创建对象
2、添加元素
3、取出某个元素
public class HashMapTest {
    public static void main(String[] args) {
        //创建对象
        HashMap<Integer,String> hm = new HashMap<>();
        //添加元素
        hm.put(3,"sjg");
        hm.put(2,"sgaj");
        hm.put(1,"gl");
        hm.put(3,"adg");
        hm.put(4,"qqq");
        hm.put(2,"bsdg");
        //根据key取出某个元素
        System.out.println("取出健为1的元素为"+hm.get(1));
        //遍历元素,把key取出来遍历
        Set<Integer> set = hm.keySet();
        for (Integer i : set) {
            System.out.println(i+"="+hm.get(i));
        }
        System.out.println("---------------");
        //遍历元素,用Entry把Map集合转换成Set集合
        Set<Map.Entry<Integer,String>> notes = hm.entrySet();
        for (Map.Entry<Integer, String> entry: notes){
            System.out.println(entry.getKey()+"----->"+entry.getValue());
        }
    }
}
Properties
1、创建对象
2、添加元素
3、取出某个元素
    
public class PropertiesTest {
    public static void main(String[] args) {
        Properties pp = new Properties();
        pp.setProperty("123","啊?");
        pp.setProperty("789","啊?");
        pp.setProperty("789","呀?");
        pp.setProperty("456","啊?");
        pp.setProperty("456","啊?");
        pp.setProperty("456","哟?");
        pp.setProperty("321","吓?");
        pp.setProperty("666","切?");
        pp.setProperty("355","哦?");
        pp.setProperty("777","嗯?");
        //获取元素
        System.out.println(pp.get("456"));
    }
}    
线程
/*
关于Thread.sleep()方法的一个面试题:
 */
public class ThreadTest07 {
    public static void main(String[] args) {
        // 创建线程对象
        Thread t = new MyThread3();
        t.setName("t");
        t.start();

        // 调用sleep方法
        try {
            // 问题:这行代码会让线程t进入休眠状态吗?
            t.sleep(1000 * 5); // 在执行的时候还是会转换成:Thread.sleep(1000 * 5);
                                     // 这行代码的作用是:让当前线程进入休眠,也就是说main线程进入休眠。
                                     // 这样代码出现在main方法中,main线程睡眠。
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 5秒之后这里才会执行。
        System.out.println("hello World!");
    }
}

class MyThread3 extends Thread {
    public void run(){
        for(int i = 0; i < 10000; i++){
            System.out.println(Thread.currentThread().getName() + "--->" + i);
        }
    }
}
死锁
/*
死锁代码要会写。
一般面试官要求你会写。
只有会写的,才会在以后的开发中注意这个事儿。
因为死锁很难调试。
 */
public class DeadLock {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();

        // t1和t2两个线程共享o1,o2
        Thread t1 = new MyThread1(o1,o2);
        Thread t2 = new MyThread2(o1,o2);

        t1.start();
        t2.start();
    }
}

class MyThread1 extends Thread{
    Object o1;
    Object o2;
    public MyThread1(Object o1,Object o2){
        this.o1 = o1;
        this.o2 = o2;
    }
    public void run(){
        synchronized (o1){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o2){

            }
        }
    }
}

class MyThread2 extends Thread {
    Object o1;
    Object o2;
    public MyThread2(Object o1,Object o2){
        this.o1 = o1;
        this.o2 = o2;
    }
    public void run(){
        synchronized (o2){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o1){

            }
        }
    }
}
面试题
// 面试题:doOther方法执行的时候需要等待doSome方法的结束吗?
public class Exam01 {
    public static void main(String[] args) throws InterruptedException {
        MyClass mc = new MyClass();

        Thread t1 = new MyThread(mc);
        Thread t2 = new MyThread(mc);

        t1.setName("t1");
        t2.setName("t2");

        t1.start();
        Thread.sleep(1000); //这个睡眠的作用是:为了保证t1线程先执行。
        t2.start();
    }
}

class MyThread extends Thread {
    private MyClass mc;
    public MyThread(MyClass mc){
        this.mc = mc;
    }
    public void run(){
        if(Thread.currentThread().getName().equals("t1")){
            mc.doSome();
        }
        if(Thread.currentThread().getName().equals("t2")){
            mc.doOther();
        }
    }
}

class MyClass {
    public synchronized void doSome(){
        System.out.println("doSome begin");
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("doSome over");
    }
    public void doOther(){
        System.out.println("doOther begin");
        System.out.println("doOther over");
    }
}
//不需要,因为doOther()方法没有synchronized
// 面试题:doOther方法执行的时候需要等待doSome方法的结束吗?
public class Exam01 {
    public static void main(String[] args) throws InterruptedException {
        MyClass mc = new MyClass();

        Thread t1 = new MyThread(mc);
        Thread t2 = new MyThread(mc);

        t1.setName("t1");
        t2.setName("t2");

        t1.start();
        Thread.sleep(1000); //这个睡眠的作用是:为了保证t1线程先执行。
        t2.start();
    }
}

class MyThread extends Thread {
    private MyClass mc;
    public MyThread(MyClass mc){
        this.mc = mc;
    }
    public void run(){
        if(Thread.currentThread().getName().equals("t1")){
            mc.doSome();
        }
        if(Thread.currentThread().getName().equals("t2")){
            mc.doOther();
        }
    }
}

class MyClass {
    public synchronized void doSome(){
        System.out.println("doSome begin");
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("doSome over");
    }
    public synchronized void doOther(){
        System.out.println("doOther begin");
        System.out.println("doOther over");
    }
}
//需要
// 面试题:doOther方法执行的时候需要等待doSome方法的结束吗?
public class Exam01 {
    public static void main(String[] args) throws InterruptedException {
        MyClass mc1 = new MyClass();
        MyClass mc2 = new MyClass();

        Thread t1 = new MyThread(mc1);
        Thread t2 = new MyThread(mc2);

        t1.setName("t1");
        t2.setName("t2");

        t1.start();
        Thread.sleep(1000); //这个睡眠的作用是:为了保证t1线程先执行。
        t2.start();
    }
}

class MyThread extends Thread {
    private MyClass mc;
    public MyThread(MyClass mc){
        this.mc = mc;
    }
    public void run(){
        if(Thread.currentThread().getName().equals("t1")){
            mc.doSome();
        }
        if(Thread.currentThread().getName().equals("t2")){
            mc.doOther();
        }
    }
}

class MyClass {
    public synchronized void doSome(){
        System.out.println("doSome begin");
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("doSome over");
    }
    public synchronized void doOther(){
        System.out.println("doOther begin");
        System.out.println("doOther over");
    }
}
//不需要,因为MyClass对象是两个,两把锁。
// 面试题:doOther方法执行的时候需要等待doSome方法的结束吗?
public class Exam01 {
    public static void main(String[] args) throws InterruptedException {
        MyClass mc1 = new MyClass();
        MyClass mc2 = new MyClass();

        Thread t1 = new MyThread(mc1);
        Thread t2 = new MyThread(mc2);

        t1.setName("t1");
        t2.setName("t2");

        t1.start();
        Thread.sleep(1000); //这个睡眠的作用是:为了保证t1线程先执行。
        t2.start();
    }
}

class MyThread extends Thread {
    private MyClass mc;
    public MyThread(MyClass mc){
        this.mc = mc;
    }
    public void run(){
        if(Thread.currentThread().getName().equals("t1")){
            mc.doSome();
        }
        if(Thread.currentThread().getName().equals("t2")){
            mc.doOther();
        }
    }
}

class MyClass {
    // synchronized出现在静态方法上是找类锁。
    public synchronized static void doSome(){
        System.out.println("doSome begin");
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("doSome over");
    }
    public synchronized static void doOther(){
        System.out.println("doOther begin");
        System.out.println("doOther over");
    }
}
//需要,因为静态方法是类锁,不管创建了几个对象,类锁只有1把。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值