2022.1.25
Map
-
About how to deal with duplicate data input and error information input
while(i<3){ String id = input.next(); if(GoodMap.containsKey(id)){ continue; } String name = input.next(); double value =0; try{ value=input.nextDouble(); }catch(InputMismatchException e){ input.next(); continue; } Goods good = new Goods(id,name,value); GoodMap.put(id,good); i++; }
- Note that if the input information is wrong, add input Next(), because the wrong input information is still stored in the buffer and will be read incorrectly by the next cycle, it needs to be written in the framework
2022.1.26
sort
Comparator interface
-
About the Comparator interface, this interface is usually placed in the sorting method as a parameter. At first, I also found it awkward. Later, I thought about it. This is mainly because in java, a single function cannot exist, so we designed such an interface so that we can call functions indirectly through classes
public class test { public static void main(String[] args) { Cat a= new Cat("b",311,"fjkdjfdkd"); Cat b= new Cat("a",322,"fjkdfjdk"); Cat c= new Cat("c",313,"fjakdfj"); ArrayList<Cat> d = new ArrayList<Cat>(); d.add(a); d.add(b); d.add(c); System.out.println("front"); for(Cat e : d){ System.out.println(e.toString()); } System.out.println("after"); Collections.sort(d,new zimu()); for(Cat e : d){ System.out.println(e.toString()); } } }
public class zimu implements Comparator<Cat> { @Override public int compare(Cat a,Cat b){ return a.getName().compareTo(b.getName()); } }
- Note that we implement the compare method instead of the compareTo method in the process of implementing the interface with classes
- Then, in the process of sorting, pay attention to new an object instance, otherwise the internal methods cannot be accessed
Comparable interface
-
How to use it? Just implement the Comparable interface in the class you want to implement
public class Goods implements Comparable<Goods>{ private String id; private String name; private double price; public Goods(String id, String name, double price){ this.id=id; this.name=name; this.price=price; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(String id) { this.id = id; } public String getId() { return id; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "id='" + id + '\'' + ", name='" + name + '\'' + ", price=" + price ; } @Override public int compareTo(Goods o) { return this.price>o.price ? 1 : -1; } }
public class two { public static void main(String[] args){ Goods one = new Goods("3124","iphone",31232131); Goods two = new Goods("313","tel",314); Goods three = new Goods("411","fks",43143); ArrayList<Goods> list = new ArrayList<Goods>(); list.add(one); list.add(two); list.add(three); System.out.println("first"); for(Goods good : list){ System.out.println(good); } System.out.println("after"); Collections.sort(list); for(Goods good : list){ System.out.println(good); } } }
In fact, I don't think these two are particularly difficult. One is to write the judgment method outside, and the other is to write the judgment method inside the class. It's not difficult to understand. Just pay attention to your own class in < > after the Comparable interface
other
-
int n= new Double(89.7-34).intValue(); System.out.println(n);
double n = Double.parseDouble("3213"); System.out.println(n);
int n = Integer.parseInt("432432"); System.out.println(n);
int n = Integer.valueOf("4343"); System.out.println(n);
The following is the source code
//First call parseInt to obtain the int value, and then encapsulate it into an Integer object. Pay attention to the encapsulated logic and cache public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); } public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); } //Direct conversion to get int value public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); }
It can be seen that parseInt directly outputs the original data, and valueOf encapsulates it again;
Description
This method is used to get the primitive data type of a certain String. parseXxx() is a static method and can have one argument or two.
Syntax
Following are all the variants of this method −
static int parseInt(String s) static int parseInt(String s, int radix)
Parameters
Here is the detail of parameters −
- s − This is a string representation of decimal.
- radix − This would be used to convert String s into integer.
Return Value
- parseInt(String s) − This returns an integer (decimal only).
- parseInt(int i) − This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.
Here, radix is the number under the decimal system that represents the previous string, and then it is uniformly converted into decimal system
2022.1.27
generic paradigm
-
How to call subclass containers with generics in the method
public class one { public void test(List<? extends father> a){ for(father e : a){ e.function(); } } }
public class Main { public static void main(String[] args) throws Exception { List<sonOne> lis1 = new ArrayList<sonOne>(); sonOne one1= new sonOne(); sonOne one2 = new sonOne(); lis1.add(one1);lis1.add(one2); List<sonTwo> lis2 = new ArrayList<sonTwo>(); sonTwo two1= new sonTwo(); sonTwo two2= new sonTwo(); lis2.add(two1);lis2.add(two2); List<sonThree> lis3 = new ArrayList<sonThree>(); sonThree three1= new sonThree(); sonThree three2= new sonThree(); lis3.add(three1);lis3.add(three2); one test1 = new one(); test1.test(lis1); test1.test(lis2); test1.test(lis3); } } abstract class father{ private int a=314,b=4343; public abstract void function(); } class sonOne extends father{ public void function(){ System.out.println("one"); } } class sonTwo extends father{ public void function(){ System.out.println("two"); } } class sonThree extends father{ public void function(){ System.out.println("three"); } }
Here, the for loop should be an upward transformation after the elements in the list are taken out, and because of method rewriting, the parent class uses the method of the child class
other
-
Note that when we write a class, we should understand clearly. This means that what we want to write is a class file, so you don't want to add a class when you don't plan to write a class. For example, when you write a constructor, just do it directly
public Class name(){}
-
Once the parent variable is set to private, it cannot be directly passed through super Variable names are accessed directly. It can be understood that when you use the constructor, the instance of a class really exists. If the constructor of a subclass has the help of the parent class, the permission access of that part of the parent class is different from that of another part of the subclass, and super plays a role of a built-in parent class
-
Note that the class in CompareTo (class variable) is the name of the class to which it belongs
2022.1.28
generic paradigm
-
Custom generic class
public class two <E>{ private E a; public two(E a) { this.a = a; } public E getA() { return a; } public static void main(String[] args){ two<Integer> one = new two<Integer>(313); System.out.println("a = "+one.getA()); two<Float> two = new two<Float>(313.0f); System.out.println("two = "+two.getA()); two three = new two(434324); System.out.println("three = "+three.getA()); } }
Where e represents the pending data type, and remember that the first line must be followed, otherwise it will be invalid. Then note that E may not exist in the custom generic class
public class two<E> { private int a; public two(int a) { this.a = a; } public int getA() { return a; } public static void main(String[] args){ two<Integer> one = new two<Integer>(313); System.out.println("a = "+one.getA()); two<Float> two = new two<Float>(313); System.out.println("two = "+two.getA()); two three = new two(434324); System.out.println("three = "+three.getA()); } }
Then, if you want to pass in multiple unclear classes in advance, you can write so as to make the input more free
public class two<E,y> { private E a ,b; public two(E a) { this.a = a; } public E getA() { return a; } public static void main(String[] args){ two<Integer,Float> one = new two<Integer,Float>(313); System.out.println("a = "+one.getA()); two<Float,Integer> two = new two<Float,Integer>(313); System.out.println("two = "+two.getA()); two three = new two(434324); System.out.println("three = "+three.getA()); } }
-
About generic custom methods
public class two<E,y> { public<t> void print(t a){ System.out.println(a); } public static void main(String[] args){ two one = new two(); one.print(3132); one.print(2133.432f); one.print("hello world"); } }
If you want to constrain t in the method, you can write it like this
public class two<E,y> { public<t extends Number> void print(t a){ System.out.println(a); } public static void main(String[] args){ two one = new two(); one.print(3132); one.print(2133.432f); one.print("hello world"); } }
Normally, your ninth line should be wrong and cannot be compiled
thread
Thread
-
Let's first understand what asynchronous execution is and what synchronous execution is
Synchronization, or synchronization, means "connection", or "dependency" to some extent. In other words, two synchronization tasks must understand each other, and one task must be executed in a way that depends on the other task, such as waiting until the other task is completed.
Asynchrony means that they are completely independent. Neither of them can consider the other in any way, whether in initialization or execution
To put it simply, synchronization can only be executed after the above execution, while asynchrony can also be executed after the above execution
-
Then let's meet start and run
. start() will cause the run() method to be called. The content in the run() method is called the thread body, which is the work that this thread needs to perform.
Using start() to start the thread realizes the real sense of starting the thread. At this time, there will be the effect of asynchronous execution, that is, the randomness described in the creation and start of the thread.
If you use run() to start a thread, it will not execute asynchronously, but synchronously, which will not achieve the meaning of using threadsCopyright notice: This article is the original article of CSDN blogger "smileTimLi", which follows the CC 4.0 BY-SA copyright agreement. For reprint, please attach the original source link and this notice.
Original link: https://blog.csdn.net/qq_35275233/article/details/87896475
other
-
Looking at the generic class, I suddenly thought of an irrelevant thing, that is, the constructor of the class. When I looked at effective java in advance, I saw him say it's best not to use the constructor, but to use the name. I haven't figured out why in advance, because I thought he said the variable name. Now I think it's because the object can be returned according to the different order of the constructor, the number of parameters and the type of parameters, But then you have to remember the parameter table
-
1. Shortcut keys for adding multiline comments at once
First select the area to be annotated, and then
ctrl + / this is a multi line code line comment, with one comment symbol per line
ctrl+shift + / this is a multi line code comment. In a block, there are comment symbols only at the beginning and end
2. Cancel multiline comment shortcut
How to add shortcut keys? Cancel in the same way,
If ctrl + / adds a comment, ctrl + / cancels the comment
ctrl+shift + / to add a comment, ctrl+shift + / to cancel the comment
-
I don't know if anyone, like me, bought Lenovo computers. F1-F12 has default options. If you want to not use the default keyboard settings, you can fn+esc, and then I have a Logitech keyboard... F1-F12 also has default options The solution to this problem is to press fn... And keep pressing fn until the operation is completed There is another kind of software, but I'm lazy
2022.1.29
thread
-
start method
package xiancheng; public class one extends Thread{ public void run() { for(int i= 0;i<321;i++) System.out.println("jsakdjk"); } public static void main(String[] args){ System.out.println("main is one "); one first = new one(); first.start(); //first.start(); System.out.println("main is two"); } }
-
sleep method
public class one extends Thread{ private String a; public one (String a) { this.a = a; } public void run() { for(int i= 0;i<21;i++){ System.out.println(a+i); try { Thread.sleep(6766878); }catch (InterruptedException e){ e.printStackTrace(); } } } public static void main(String[] args){ one first = new one("this is one"); one second = new one("this is two"); first.start(); //first.start(); } }
At first, I didn't add parentheses. I finished it directly. I was stunned
Then this can be used to refresh the work
-
join method
public class one extends Thread{ private String a; public one (String a) { this.a = a; } public void run() { for(int i= 0;i<21;i++){ System.out.println(a+i); try { Thread.sleep(1000); }catch (InterruptedException e){ e.printStackTrace(); } } } public static void main(String[] args){ one first = new one("this is one"); one second = new one("this is two"); first.start(); try{ first.join(10000); }catch (InterruptedException e){ e.printStackTrace(); } second.start(); //first.start(); } }
Using the join method, you can directly give priority to running threads. If there are no parameters in (), the next thread will not run until the thread runs. If there are parameters, you can run the time within the parameters, and then compete freely for the use right of cpu
-
thread priority
-
Priority constant (range: 1 to 10, error report if exceeded). Note that the lower priority cannot guarantee which thread must execute first
-
Method to get and set thread priority
package xiancheng; public class one extends Thread{ private String a; public one (String a) { this.a = a; } public void run() { for(int i= 0;i<21;i++){ System.out.println(a+i); try { Thread.sleep(500); }catch (InterruptedException e){ e.printStackTrace(); } } } public static void main(String[] args){ one first = new one("this is one"); one second = new one("this is two"); int mainPriority =Thread.currentThread().getPriority(); System.out.println("The priority of the main thread is:"+mainPriority); first.setPriority(MIN_PRIORITY); second.setPriority(MAX_PRIORITY); first.start(); try{ first.join(10000); }catch (InterruptedException e){ e.printStackTrace(); } System.out.println("The priority of the main thread is:"+mainPriority); second.start(); System.out.println("first The priority of is:"+first.getPriority()); System.out.println("second The priority of is:"+second.getPriority()); } }
-
2022.1.30
on java 8 reading notes (excerpt)
-
- Sometimes we will distinguish between the two, and define the type as the interface, while the class is the concrete implementation of the interface.
- What requests an object can accept is determined by its "interface", and the class to which the object belongs defines these interfaces.
- Interface defines the requests you can send to this object. In addition, there must be some code to respond to these requests. This code, coupled with hidden data, is called implementation.
thread
-
synchronization
-
Let's take a look at what happens if there is a thread interrupt
public class Test { public static void main(String[] args) { two a = new two("jafsdkl",10000); Get get = new Get(a); Pay pay = new Pay(a); get.start(); pay.start(); try{ get.join(); }catch(Exception e){ e.printStackTrace(); } try{ pay.join(); }catch(Exception e){ e.printStackTrace(); } System.out.println(a); } }
package xiancheng; public class two { private String name; private int account; public two(String name, int account) { this.name = name; this.account = account; } public void setName(String name) { this.name = name; } public String getName() { return name; } public int getAccount() { return account; } public void setAccount(int account) { this.account = account; } public void pay(){ int account = getAccount(); account-=100; try { Thread.sleep(212); }catch (InterruptedException e) { e.printStackTrace(); } System.out.println("now the account is "+account); this.account = account; } public void get(){ int account = getAccount(); account +=500; try{ Thread.sleep(212); }catch(InterruptedException e){ e.printStackTrace(); } this.account = account; System.out.println("now the account is "+account); } @Override public String toString() { return "two{" + "name='" + name + '\'' + ", account=" + account + '}'; } } class Pay extends Thread{ private two a; public Pay(two a) { this.a = a; } public void run() { a.pay(); } } class Get extends Thread{ private two a; public Get(two a){ this.a=a; } public void run(){ a.get(); } }
-
.println("now the account is "+account);
}
@Override public String toString() { return "two{" + "name='" + name + '\'' + ", account=" + account + '}'; } } class Pay extends Thread{ private two a; public Pay(two a) { this.a = a; } public void run() { a.pay(); } } class Get extends Thread{ private two a; public Get(two a){ this.a=a; } public void run(){ a.get(); } } ``` Copy the above code to your computer,I believe you will find the problem,On my computer,If everything is normal,The final output should be 10400,But if I use thread Simulate preemptive thread execution,I got 10500 and 9900.