java.lang.Long 无法转换为 java.util.Map 但我不会转换任何 long

问题描述 投票:0回答:1
private static Serializer serializer;
private static Map<Long, User> users = new HashMap<>();
// private static Map<Long, Admin> admins = new HashMap<>();
private static Map<Long, Movie> movies = new HashMap<>();
private static Map<Long, Rating> ratings = new HashMap<>();
private static Map<String, Movie> movietitle = new HashMap<>();
private static Map<String, Movie> moviedate = new HashMap<>();
private static Map<String, User> Login = new HashMap<>();
private static Map<String, Admin> AdminLogin = new HashMap<>();

// more lines of codes

@SuppressWarnings("unchecked")
public void load() throws Exception {
    serializer.read();
    users = (Map<Long, User>) serializer.pop();
    movies = (Map<Long, Movie>) serializer.pop();
    ratings = (Map<Long, Rating>) serializer.pop();
    AdminLogin = (Map<String, Admin>) serializer.pop();
    User.counter = (Long) serializer.pop();
    Movie.counter = (Long) serializer.pop();
}
// more lines of codes

错误

******Security framework of XStream not initialized, XStream is probably   
vulnerable.
****Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.util.Map
    at Recommendation.RecommendationAPI.load(RecommendationAPI.java:211)
    at Recommendation.Client.main(Client.java:34)**

正如你们所见,每当我尝试加载已在文件夹中创建的 XML 文件时,我都会收到错误。我可以创建没有错误的 XML 文件,但在加载文件时,正如您在上面看到的,我遇到了错误。我不知道为什么它说

Long
不能转换为
Map
如果我不转换任何只要第 211 行是

AdminLogin = (Map<String, Admin>) serializer.pop();

main中的错误基本上是我使用load()方法

//XMLSerializer
package utils;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Stack;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XMLSerializer implements Serializer
{
    private Stack<Object> stack = new Stack<>();
  private File file;

  public XMLSerializer(File file)
    {
        this.file = file;
    }
    @Override
    public void push(Object o)
    {
        stack.push(o);
    }

    @Override
    public Object pop()
    {
        return stack.pop();
    }

    @SuppressWarnings("unchecked")
    @Override
    public void read() throws Exception
    {
    ObjectInputStream inputStream = null;

    try
    {
      XStream xstream = new XStream(new DomDriver());
      inputStream = xstream.createObjectInputStream(new FileReader(file));
      stack = (Stack<Object>) inputStream.readObject();
    }
    finally
    {
      if (inputStream != null)
      {
        inputStream.close();
      }
    }       
    }

    @Override
    public void write() throws Exception
    {
        ObjectOutputStream outputStream = null;

    try
    {
      XStream xstream = new XStream(new DomDriver());
      outputStream = xstream.createObjectOutputStream(new FileWriter(file));
      outputStream.writeObject(stack);
    }
    finally
    {
      if (outputStream != null)
      {
        outputStream.close();
      }
    }

    }



    //Serializer
     package utils;

public interface Serializer {


    void push(Object o);
      Object pop();
    void write() throws Exception;
    void read() throws Exception;

}


//CSVLoader
package utils;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import values.Admin;
import values.Movie;
import values.Rating;
import values.User;
import edu.princeton.cs.introcs.In;

public class CSVLoader {

    public List<User> loadUsers(String filename) throws Exception {
        File usersFile = new File(filename);
        In inUsers = new In(usersFile);
        String delims = "[|]";
        List<User> users = new ArrayList<User>();
        while (!inUsers.isEmpty()) {
            String userDetails = inUsers.readLine();
            String[] userTokens = userDetails.split(delims);
            if (userTokens.length == 7) {
                String firstName = userTokens[1];
                String lastName = userTokens[2];
                int age = Integer.valueOf(userTokens[3]);
                String gender = userTokens[4];
                String occupation = userTokens[5];
                String zip = userTokens[6];
                String username = "";
                String password = "";
                users.add(new User(firstName, lastName, age, gender, occupation,zip,username,password));
            } else {
                throw new Exception("Invalid User length: " + userTokens.length);
            }
        }
        return users;
    }

    public List<Admin> loadAdmins(String filename) throws Exception {
        File AdminFile = new File(filename);
        In inAdmins = new In(AdminFile);
        String delims = "[|]";
        List<Admin> admins = new ArrayList<Admin>();
        while (!inAdmins.isEmpty()) {
            String adminDetails = inAdmins.readLine();
            String[] adminTokens = adminDetails.split(delims);
            if (adminTokens.length == 7) {
                String firstName = adminTokens[1];
                String lastName = adminTokens[2];
                int age = Integer.valueOf(adminTokens[3]);
                String gender = adminTokens[4];
                String username = adminTokens[5];
                String password = adminTokens[6];
                admins.add(new Admin(firstName, lastName, age, gender, username,password));
            } else {
                throw new Exception("Invalid User length: " + adminTokens.length);
            }
        }
        return admins;
    }

    public List<Movie> loadMovies(String filename) throws Exception {
            File movieFile = new File(filename);
            In inMovies= new In(movieFile);
            String delims = "[|]";
            List<Movie> movies = new ArrayList<Movie>();
            while (!inMovies.isEmpty()) {
                String movieDetails = inMovies.readLine();
                String[] movieTokens = movieDetails.split(delims);
                if (movieTokens.length == 23) {
                    String title = movieTokens[1];
                    String date = movieTokens[2];
                    String url = movieTokens[3];
                    movies.add(new Movie(title, date, url));
                } 
                else 
                {
                throw new Exception("Invalid Movie length: " + movieTokens.length);
                }
            }
            return movies;
        }

    public List<Rating> loadRatings(String filename) throws Exception {
        File ratingFile = new File(filename);
        In inRating = new In(ratingFile);
        String delims = "[|]";
        List<Rating> ratings = new ArrayList<Rating>();
        while (!inRating.isEmpty()) {
            String ratingDetails = inRating.readLine();
            String[] ratingTokens = ratingDetails.split(delims);
            if (ratingTokens.length == 4) {
                Long userid = Long.valueOf(ratingTokens[0]);
                Long itemid = Long.valueOf(ratingTokens[1]);
                int rating = Integer.valueOf(ratingTokens[2]);
                int timestamp = Integer.valueOf(ratingTokens[3]);
                ratings.add(new Rating(userid, itemid, rating,timestamp));
            } 
            else 
            {
            throw new Exception("Invalid Ratings length: " + ratingTokens.length);
            }
        }
        return ratings;
    }

}



 //XML File
<object-stream>
  <java.util.Stack serialization="custom">
    <unserializable-parents/>
    <vector>
      <default>
        <capacityIncrement>0</capacityIncrement>
        <elementCount>6</elementCount>
        <elementData>
          <map>
            <entry>
              <string>keden12</string>
              <values.Admin>
                <fname>Kamil</fname>
                <lname>Bigos</lname>
                <age>24</age>
                <gender>M</gender>
                <id>0</id>
                <username>keden12</username>
                <password>admin</password>
              </values.Admin>
            </entry>
          </map>
          <long>943</long>
          <long>1682</long>
          <map>
            <entry>
              <long>0</long>
              <values.User>
                <fname>Leonard</fname>
                <lname>Hernandez</lname>
                <age>24</age>
                <gender>M</gender>
                <occupation>technician</occupation>
                <zip>85711</zip>
                <id>0</id>
                <username></username>
                <password></password>
                <ratingsUser/>
              </values.User>
            </entry>
            <entry>
              <long>1</long>
              <values.User>
                <fname>Melody</fname>
                <lname>Roberson</lname>
                <age>53</age>
                <gender>F</gender>
                <occupation>other</occupation>
                <zip>94043</zip>
                <id>1</id>
                <username></username>
                <password></password>
                <ratingsUser/>
              </values.User>
            </entry>
            <entry>
              <long>2</long>
              <values.User>
                <fname>Gregory</fname>
                <lname>Newton</lname>
                <age>23</age>
                <gender>M</gender>
                <occupation>writer</occupation>
                <zip>32067</zip>
                <id>2</id>
                <username></username>
                <password></password>
                <ratingsUser/>
              </values.User>
            </entry>
            <entry>
              <long>3</long>
              <values.User>
                <fname>Oliver</fname>
                <lname>George</lname>
                <age>24</age>
                <gender>M</gender>
                <occupation>technician</occupation>
                <zip>43537</zip>
                <id>3</id>
                <username></username>
                <password></password>
                <ratingsUser/>
              </values.User>
            </entry>
           </map>
                  <map>
            <entry>
              <long>0</long>
              <values.Movie>
                <movieid>0</movieid>
                <title>Toy Story (1995)</title>
                <releasedate>01-Jan-1995</releasedate>
                <url>http://us.imdb.com/M/title-exact?Toy%20Story%20(1995)</url>
              </values.Movie>
            </entry>
            <entry>
              <long>1</long>
              <values.Movie>
                <movieid>1</movieid>
                <title>GoldenEye (1995)</title>
                <releasedate>01-Jan-1995</releasedate>
                <url>http://us.imdb.com/M/title-exact?GoldenEye%20(1995)</url>
              </values.Movie>
            </entry>
            <entry>
              <long>2</long>
              <values.Movie>
                <movieid>2</movieid>
                <title>Four Rooms (1995)</title>
                <releasedate>01-Jan-1995</releasedate>
                <url>http://us.imdb.com/M/title-exact?Four%20Rooms%20(1995)         </url>
              </values.Movie>
            </entry>
           </map>
         <map>
            <entry>
              <long>1</long>
              <values.Rating>
                <userid>1</userid>
                <itemid>94</itemid>
                <rating>-3</rating>
                <timestamp>875072956</timestamp>
                <usersRatings/>
              </values.Rating>
            </entry>
            <entry>
              <long>2</long>
              <values.Rating>
                <userid>2</userid>
                <itemid>296</itemid>
                <rating>1</rating>
                <timestamp>888550871</timestamp>
                <usersRatings/>
              </values.Rating>
            </entry>
            <entry>
              <long>3</long>
              <values.Rating>
                <userid>3</userid>
                <itemid>346</itemid>
                <rating>5</rating>
                <timestamp>889237455</timestamp>
                <usersRatings/>
              </values.Rating>
            </entry>
            <entry>
              <long>4</long>
              <values.Rating>
                <userid>4</userid>
                <itemid>301</itemid>
                <rating>5</rating>
                <timestamp>892002353</timestamp>
                <usersRatings/>
              </values.Rating>
            </entry>
           </map>
           <null/>
          <null/>
          <null/>
          <null/>
        </elementData>
      </default>
    </vector>
  </java.util.Stack>
</object-stream>
java xml hashmap xmlserializer xstream
1个回答
0
投票

堆栈保留顺序。 XML 示例的元素顺序为“map、map、map、long、long、map”(从下往上)。因此,对于第四个 .pop(),您将得到一个“long”(1682),但尝试将其转换为地图。

(我希望 TO 在发布后的六年内解决这个问题。)

© www.soinside.com 2019 - 2024. All rights reserved.