2

I need some help to filter an array with my function that I am getting data. I tried to create my own function to filter an array but for some reason it is not doing it. Can any one check my code of what I have done wrong. I need help to filter an array. I.E: when user types 2 letters to filter and show couple of list based of those letters. Thanks!

Here is my code:

        public ArrayList<String> cPyList() throws SQLException, NamingException {


            ArrayList<String> cPySearchList = new ArrayList<String>();
            CallableStatement ps = null;
            Connection conn = null;
            ResultSet rs = null;

            try {

                conn = DataUtility.getDataSource().getConnection();

                if (conn == null) {
                    throw new SQLException("Can't get database connection");
                }

                ps = conn.prepareCall(strCPy);
                ps.clearParameters();

                ArrayList list = new ArrayList<>();

                rs = ps.executeQuery();

                while (rs.next()) {

                    cPySearchList .add(rs.getString(1) + " (" + rs.getInt(2) + ")");


                } 
            } finally {

                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (conn != null) {
                    conn.close();
                }

            }
            return cPySearchList ;   


    }

   private static List<String> returnFilteredResult(List<String> lines, String filter) {
        List<String> result = new ArrayList<>();
        for (String line : lines) {
            if (!"cPySearchList".equals(line)) { 
                result.add(line);
            }
        }
        return result;
    } 
2
  • Basically, filter what the user search. maximum sub string 3 characters. Commented Apr 12, 2018 at 18:35
  • @BogdanLukiyanchuk lol. true. I was saying to myself why bcr copies answers.... Commented Apr 12, 2018 at 18:58

2 Answers 2

2

Your idea is right in general but for filtering you should use contains or startsWith.

private static List<String> returnFilteredResult(List<String> lines, String filter) {
    List<String> result = new ArrayList<>();
    for (String line : lines) {
        if (line.contains(filter)) {
            result.add(line);
        }
    }
    return result;
}

public ArrayList<String> cPyList(String filter) throws SQLException, NamingException {
    ArrayList<String> cPySearchList = new ArrayList<String>();
    CallableStatement ps = null;
    Connection conn = null;
    ResultSet rs = null;

    try {
        conn = DataUtility.getDataSource().getConnection();
        if (conn == null) {
            throw new SQLException("Can't get database connection");
        }
        ps = conn.prepareCall(strCPy);
        ps.clearParameters();
        ArrayList list = new ArrayList<>();
        rs = ps.executeQuery();

        while (rs.next()) {
            cPySearchList.add(rs.getString(1) + " (" + rs.getInt(2) + ")");
        }
    } finally {
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
    return returnFilteredResult(cPySearchList, filter);
}
Sign up to request clarification or add additional context in comments.

7 Comments

Got it. But, how can I filter based of user search results? Am I going to call anything inside my cPyList function?
Bill, replace “hello” with your parameter filter
you can call method returnFilteredResult(cPySearchList , "search")
i did this: List<String> result = returnFilteredResult(cPySearchList, "search"); for (String temp : result) { System.out.println(temp); }
but my filter does not gets called inside cPyList function?
|
2
  1. you aren't using the value of filter in your method. i think you want to do that
  2. use stream API

    private static List<String> returnFilteredResult(List<String> list,String filter){ return list.stream().filter(l-> l.contains(filter)).collect(Collectors.toList()); }

1 Comment

should I implement inside my cPyList function, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.