About two list deep traversal

In the past two days, I suddenly have an idea about two lists full of objects, in which ist1 is going to take the value of the object in list2 with the same id.

It turns out that the method of deep traversal has been used to obtain values, but I always feel that this method is very slow. Once the amount of data is too large, the number of traversals will increase geometrically.

Today, I suddenly thought that the method of Map can be used to get the value, and then I tested the speed of deep traversal and the value of Map.

 

package demo.mapLisr;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import demo.model.student;

public class demo {
	public static void main(String[] args) {
		demo d = new demo();
		List<student> sl = new ArrayList<>();
		for (int i = 0; i < 10000; i++) {
			String a = i + "";
			student ss = new student();
			ss.setId(Long.parseLong(a));
			//随机两个汉字
			ss.setName(demo.getRandomJianHan(2));
			sl.add(ss);
		}
		List<student> sl2 = new ArrayList<>();
		for (int i = 0; i < 1000; i++) {
			String a = i + "";
			student ss = new student();
			ss.setId(Long.parseLong(a));
			java.util.Random random = new java.util.Random();
			String b = random.nextInt(10) + "";
			ss.setSex(b);
			sl2.add(ss);
		}
		long sa = System.nanoTime();
		// d.list(sl, sl2);
		d.map(sl, sl2);
		long aaa = System.nanoTime();
		System.out.println(aaa - sa);
		// System.out.println(sl2);
		// System.out.println(sl);
	}

	private void map(List<student> sl, List<student> sl2) {
		Map<String, Object> map = new HashMap<>();
		for (int i = 0; i < sl2.size(); i++) {
			student student = sl2.get(i);
			map.put(student.getId().toString(), student);
		}
		for (int i = 0; i < sl.size(); i++) {
			student ss = sl.get(i);
			String string = ss.getId().toString();
			student object = (student) map.get(string);
			if (object != null) {
				ss.setSex(object.getSex());
			}

		}
	}

	private void list(List<student> sl, List<student> sl2) {
		for (int i = 0; i < sl.size(); i++) {
			student student = sl.get(i);
			for (int j = 0; j < sl2.size(); j++) {
				if (student.getId().longValue() == sl2.get(j).getId()) {
					student.setSex(sl2.get(j).getSex());
				}
			}
		}
	}

	public static String getRandomJianHan(int len) {
		String ret = "";
		for (int i = 0; i < len; i++) {
			String str = null;
			int hightPos, lowPos; // 定义高低位
			Random random = new Random();
			hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值
			lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值
			byte[] b = new byte[2];
			b[0] = (new Integer(hightPos).byteValue());
			b[1] = (new Integer(lowPos).byteValue());
			try {
				str = new String(b, "GBk"); // 转成中文
			} catch (UnsupportedEncodingException ex) {
				ex.printStackTrace();
			}
			ret += str;
		}
		return ret;
	}
}

Then I tested the speed problem. First I tested a list of 1000 pieces of data, another 100

Map value speed is about 2W nanoseconds

The speed of deep traversal of List is about 4W nanoseconds

Then test a list of 1W data and another 1,000 data

The value speed of Map is about 900W nanoseconds

The speed of List is about 8000W nanoseconds

But if the amount of data is not large

For example, if the data in a List is less than 5, then using deep traversal is much faster than the value of map.

5 is basically the same.

So it depends on the situation.

In other words, if you have a better way, please let me know.

 

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324120639&siteId=291194637