总的要掌握:
1、创建对象
2、添加元素
3、取出某个元素
4、遍历
ArrayList
1、创建对象
2、添加元素
3、取出某个元素
4、遍历
public class ArrayListTest {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("zhangsan");
list.add("lisi");
list.add("wangwu");
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);
}
Iterator<String> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
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");
Iterator<String> it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
for(String s : set){
System.out.println(s);
}
set.add("king");
set.add("king");
set.add("king");
System.out.println(set.size());
set.add("1");
set.add("10");
set.add("2");
for(String s : set){
System.out.println("--->" + s);
}
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());
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
每个集合对象的创建(new)
1.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("阿柒");
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 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);
}
}
}
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");
System.out.println("取出健为1的元素为"+hm.get(1));
Set<Integer> set = hm.keySet();
for (Integer i : set) {
System.out.println(i+"="+hm.get(i));
}
System.out.println("---------------");
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"));
}
}
线程
public class ThreadTest07 {
public static void main(String[] args) {
Thread t = new MyThread3();
t.setName("t");
t.start();
try {
t.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
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();
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){
}
}
}
}
面试题
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);
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");
}
}
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);
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");
}
}
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);
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");
}
}
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);
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 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");
}
}