java学习日记20230414-Set接口
Posted 旗木卡卡西丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java学习日记20230414-Set接口相关的知识,希望对你有一定的参考价值。
Set接口的常用方法和基本介绍
Set接口基本介绍:
- 无序(添加和取出的顺序不一致),没有索引;
- 不允许重复元素,所以最多包含一个null;
- JDK API中Set接口的实现类:AbstractSet,EnumSet,HashSet,TreeSet,LinkedHashSet,JobStateReasons等;
Set接口的常用方法:
和List接口一样,Set接口也是Collection的字接口因此常用方法和Collection接口一样;
Set接口的遍历方式:
1.可以使用迭代器;
2.通过增强for循环;
3.不能使用索引的方式来获取;
public class SetMethod
public static void main(String[] args)
//以set接口的实现类HashSet方法
//set接口对象不能存放重复的元素,可以添加一个set元素
//set接口对象存放的数据是无序(添加的顺序和取出的顺序不一致)
//取出的顺序虽然不是添加的顺序,但是固定的
Set set = new HashSet();
set.add("john");
set.add("lucy");
set.add("john");
set.add("jack");
set.add(null);
set.add(null);
set.add("marry");
System.out.println(set);
//遍历
Iterator iterator = set.stream().iterator();
while(iterator.hasNext())
Object o = iterator.next();
System.out.println(o);
for (Object o :set)
System.out.println(o);
//删除
set.remove("nuldd dl");
System.out.println(set);
HashSet的全面说明:
- HashSet实现了Set接口
- HashSet实际上是HashMap
- 可以存放一个null值
- 不保证元素是有序的,取决于hash后,再确定索引的结果
- 不能有重复元素/对象
模拟链表+数组
public class HashSet_ public static void main(String[] args) Set hashSet = new HashSet(); //在执行add方法后会返回一个boolean值,成功为true,失败为false boolean set1 = hashSet.add(null); boolean set2 = hashSet.add(null); System.out.println(set1); //dog对象不同 hashSet = new HashSet(); hashSet.add("lucy"); hashSet.add("lucy"); hashSet.add(new Dog("tom")); hashSet.add(new Dog("tom")); System.out.println(hashSet); //无法加入,*****判断add重复机制****** hashSet.add(new String("hsp")); hashSet.add(new String("hsp")); System.out.println(hashSet); //HashMap底层是数组+链表+红黑树 //模拟简单的数组+链表 Node[] table = new Node[16]; System.out.println("table="+table); Node john = new Node("john", null); Node jack = new Node("jack", null); table[2] = john; john.next = jack; System.out.println(table[2]); Node rose = new Node("rose", null); jack.next = rose; System.out.println(table[2]); Node lucy = new Node("lucy", null); table[3] = lucy; System.out.println(Arrays.toString(table)); class Dog private String name; public Dog(String name) this.name = name; @Override public String toString() return "Dog" + "name=\'" + name + \'\\\'\' + \'\'; class Node//结点,存储数据,可以指向下一个结点 Object item; Node next; public Node(Object item, Node next) this.item = item; this.next = next; @Override public String toString() return "Node" + "item=" + item + ", next=" + next + \'\';
新兵日记--java多线程学习 --如何创建线程
java 多线程的实现方法
可以通过继承Thread类和实现Runnable接口来实现,而Thread类实际上实现了Runnable接口 ,两种创建线程的方法性质是一样的,并没有什么本质区别
Thread类
1. 创建MyThread 类并继承Thread类
public class MyThread extends Thread
2. 在MyThread类中重写 Thread类的run()方法,值得一提的是Thread类的run()方法是实现了Runnable接口,所以继承Thread类和实现Runnable并没有本质区别
1 public class MyThread extends Thread 2 public void run() 3 //线程的执行代码 4 5
3. 创建MyThread 类的实例并调用Thread类的start()方法
1 public static void main(String[] args ) 2 MyThread myTh=new MyThread(); 3 myTh.start();//调用Thread类的start()方法 4
为什么不是myTh.run()而是 myTh.start() ?
因为在myTh.run()是由main主线程来执行myTh.run()方法。myTh.start()是由自己创建的线程执行
myTh.run() 直接由main()主线程执行run方法,依旧是单线程。
myTh.start() 告诉“线程规划器”该线程已经准备好了,等你来cpu来调用该线程对象的 run方法,使线程得到启动是,是异步执行。
以上是关于java学习日记20230414-Set接口的主要内容,如果未能解决你的问题,请参考以下文章