Skillfully using two methods in Array List more than 3000,000 troops to achieve the first rank of general!

Posted by itsmani1 on Fri, 06 Sep 2019 11:58:26 +0200

A few days ago, I learned about several specific implementation classes in the Collection Collection Collection Tool class.

Today, I learned to master the sub-interface and implementation class of the Map root interface. Elements in a Map set can access their value according to the key of each element. If you access elements in a Set set, you can only access them according to the elements themselves (which is why elements in a Set set are not allowed to repeat).

Existing exercises can effectively grasp the methods of ArrayList and HashMap.
Topic:

Prepare an ArrayList that holds 3000000 (3 million) Hero objects with random names in hero-[4-bit random numbers]
hero-3229
hero-6232
hero-9365
...
Because the total is very large, almost every one has repetition. Find out all the objects named hero-5555.
Require two ways to find
1. Do not use HashMap, use for loop directly to find out, and count the time spent.
2. Find out the results and count the time spent with HashMap

The source code is as follows:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class T1 {
	public static void main(String[] args) {
		List<String> al=new ArrayList<>();
		int count=0;
		for(int i=1;i<=4000000;i++){
			int a=(int)(Math.random()*10000);
			if((a/1000!=0)){
				count++;
			String s="hero-"+a;
			al.add(s);
			if(count==3000000){
				break;
			}
			}
		}
//		System.out.println(count);
		System.out.println("----------ordinary For Loop lookup------------------");
		long a1=System.currentTimeMillis();
			Object o="hero-5555";
			int m=0,n=0;
			
		for(int i=1;i<al.size();i++){
			if(al.get(i).equals(o)){
				m++;
				n=i;
			}
		}
		System.out.println("Find success!");
		System.out.println("It is in ArrayList Among them:"+m+"second");
		System.out.println("The last subscript is:"+n);
			long b=System.currentTimeMillis();
			System.out.println("Time-consuming:"+(b-a1)+"Millisecond");
		System.out.println("----------use HashMap lookup------------------");		
//		System.out.println(al.size());
		long a2=System.currentTimeMillis();
		Map<Integer, String> hmp=new HashMap<>();
		for(int i=1;i<al.size();i++){
			hmp.put(i,al.get(i));	
		}
		int flag=0;
		int flag1=0;
		Object ob="hero-5555";
		for(int i=1;i<al.size();i++){
			if(hmp.get(i).equals(ob)){
				flag++;
				flag1=i;	
			}
			}System.out.println("Find success! Come together."+flag+"second");
			System.out.println("The last subscript is:"+flag1);
			long a3=System.currentTimeMillis();
			System.out.println("Time-consuming:"+(a3-a2)+"Millisecond");
		}
	}

Operating screenshots:

Procedure description:

There are a few places that I don't think have been handled properly. Here, I will explain the points needing attention in the program, and also tell you about the redundancy in my code.

  • Three million data in Array List are randomly generated. From random number math.random, we can see that random number multiplied by 10,000 can produce four-digit random numbers of 0-10,000. Of course, it is doomed to appear three, two, or even one-digit random numbers. Therefore, when I first met this topic, I did not want to expand directly. The big cycle is added to four million, and then a counter is added. If the random number is divided by 1000, it is four digits. Then every time such a number is generated, count ++. When count reaches three million, break jumps out of the cycle. Of course, the consequence is that the program is negative. Overload.

The first solution is to make a judgment when the number comes out randomly. As long as the number is less than 1000, add 1000 to all the numbers, so that all the numbers will be four-digit random numbers.
The second solution is to direct each random four-digit number, and then give the four-digit number a string connection operation, so that the number will be a subsequent four-digit number.

  • In this case, I think the program is still a bit problematic, because the result of HashMap search is much slower than the ordinary ArrayList method, which should be 10 to 20 milliseconds, or slightly different. But one thing to be sure is that ArrayList is impeccable in finding.

Like my _____________ If you have a better idea, please pay attention to me and hope to get your advice.~

If you are a god, please take me with you.

Topics: Java less