Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1038769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:01:00+00:00 2026-05-16T15:01:00+00:00

I am working with CSV in java. I am having one problem or you

  • 0

I am working with CSV in java. I am having one problem or you can say I don’t know how
to do it 🙂

I have a CSV file that is as follow:

a,4,5,3,2
b,6,4,6,7
c,5,3,7,2
2d,1,4,5,9
4e,4,2,5,7
m4,7,5,3,6
.
.
.
xyz,1,6,4,8

I want to get all the rows from CSV which contains these following labels in first
column, I have all following first column labels in ArrayList

a
c
2d
m4
xyz

The result should be :

a,4,5,3,2
c,5,3,7,2
2d,1,4,5,9
m4,7,5,3,6
xyz,1,6,4,8

Thanks a alot!

P.S: My CSV contains thousands rows and column.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:01:01+00:00Added an answer on May 16, 2026 at 3:01 pm

    This what you need.

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class Main {
    
        final private static List<String> lines = Arrays.asList(
                "a,4,5,3,2",
                "b,6,4,6,7",
                "c,5,3,7,2",
                "2d,1,4,5,9",
                "4e,4,2,5,7",
                "m4,7,5,3,",
                "xyz,1,6,4,8");
        final private static List<String> labelsInFirstColumn = Arrays.asList(
                "a",
                "c",
                "2d",
                "m4",
                "xyz");
    
        public static void main(String[] args) {
    
            List<String[]> result = new ArrayList<String[]>();
    
            for (String line : lines) {
    
                String columns[] = line.split("[,]");
    
                if (labelsInFirstColumn.contains(columns[0])) {
    
                    result.add(columns);
    
                }
    
            }
    
            for (String[] selectedLine : result) {
                for (String column : selectedLine) {
                    System.out.print(column + " | ");
                }
                System.out.println();
            }
        }
    }
    

    This is the output:

    run:
    a | 4 | 5 | 3 | 2 | 
    c | 5 | 3 | 7 | 2 | 
    2d | 1 | 4 | 5 | 9 | 
    m4 | 7 | 5 | 3 | 
    xyz | 1 | 6 | 4 | 8 | 
    BUILD SUCCESSFUL (total time: 1 second)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CSV file that I'm working with, and all the fields are
I have a strange problem: I have a CSV file that I read correctly
Possible Duplicate: CSV parsing in Java - working example..? I have a list of
I've been working on this simple code that puts a CSV file into a
I have written a program which reads input from csv file and its working
I'm working with a CSV file in python, which will have ~100,000 rows when
I'm currently working on a project that involves a CSV file. I initially was
I'm working with large CSV. How can I take a random sample of rows—say,
I'm working with a CSV file which is exported from Excel. I have a
I have a very basic CSV file upload module working to bulk upload my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.