How to save list items on disk instead of memory in Java

前端 未结 7 2011
眼角桃花
眼角桃花 2020-12-09 06:50

I\'m looking for a data structure same as ArrayList in Java that saves items on disk instead of memory. Does java have such a data structure? Thanks

I want to have a

相关标签:
7条回答
  • 2020-12-09 07:14

    MapDB (mapdb.org) is a library that persists java objects to disk in various collections: Sets, Maps, Queues.

    It supports caching so you can have your most frequent items in memory.

    0 讨论(0)
  • 2020-12-09 07:20

    see https://dzone.com/articles/a-filebasedcollection-in-java-for-big-collections

    try(FileBasedCollection<Integer> fbc = new FileBasedCollection<>()) {
      for(int i = 1; i < 1000_000; i++) {
        fbc.add(i);
      }
      long sum = 0;
      try(FileBasedIterator<Integer> iter = fbc.iterator()) {
        while(iter.hasNext()) {
          Integer i = iter.next();
          sum += i;
        }
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      System.out.println("sum: " + sum);
    }
    
    0 讨论(0)
  • 2020-12-09 07:30

    You can yourself do it: Serialize the ArrayList to disk.

    Make sure that the contents of the list are serializable, i.e., the objects in the list should implement the Serializable interface.

    then
    to write to file:

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
    oos.writeObject(list);
    oos.flush();
    oos.close();
    

    To read from file:

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
    List<YourClass> list = ois.readObject();
    ois.close()
    
    0 讨论(0)
  • 2020-12-09 07:34

    Another way of saving stuff which has the advantage of being language and transport neutral is to use Google's Protocol Buffers.

    0 讨论(0)
  • 2020-12-09 07:35

    If you need to change data in this ArrayList often, changing disk image, then why not consider something like http://www.hibernate.org/ ? Well, it is much more complicated than ArrayList but also gives more featues.

    0 讨论(0)
  • 2020-12-09 07:35

    Check out http://jakarta.apache.org/jcs/. This provides a way to manage objects on disk and ram. Another possible solution may to use http://code.google.com/p/pcollections/

    0 讨论(0)
提交回复
热议问题