Spark性能优化(3)——优化数据结构

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012292754/article/details/86251530

1 优化数据结构

  • 优先使用数据和字符串,而不是集合类。也就是优先使用 Array, 而不是 ArrayList,LinkedList,HashMap等集合;
比如,List<Integer> list = new ArrayList<Integer>(), 将其替换为 int[] array = new int[]。
这样 array  既比 List 少了额外信息的存储开销,还能使用原始数据类型(int)来存储数据,
比List 中 Integer 这种包装类型存储数据,要节省内存
通常企业级应用中的做法,对于 HashMap,List 这种数据,统一用 String 拼接 成特殊格式的字符串,
比如 Map<Integer,Person> persons = new HashMap(),
可以优化为,特殊字符串格式: id,name,adderss,
  • 避免使用多层嵌套的对象结构,比如说,public class Teacher{private List<Student> students = new ArrayList<Student>()} ,就是不好的例子,因为 Teacher 类的内部又嵌套了大量的小 Student 对象。对于这个例子,可以用特殊的字符串来进行数据的存储,比如用 json 字符串来存储数据,{"teacherId" : 1, "teacherName" : "Mike", students : [ { "studentsId" : 1, "studentName" : "tom"} ]}
  • 尽量使用 int 代替 String .因为虽然 String 比 ArrayList ,HashMap 等数据结构高效,占用内存量少。但是还有有额外信息的消耗。比如之前用 String 表示 id ,那么现在可以用数字类型的 int ,来进行替代。

猜你喜欢

转载自blog.csdn.net/u012292754/article/details/86251530