Separate a field from list and make a new list

pandas split column of lists into multiple rows
pandas split list column into multiple columns
pandas split one column into multiple columns
python list to columns
pandas split list into rows
pandas split list into multiple rows
split list inside dataframe
split list into dataframe columns

I have this :

 List<City> cities = new ArrayList<>();
    cities.add(new City("SF","USA"));
    cities.add(new City("Agra","India"));
    cities.add(new City("Mumbai","India"));
    cities.add(new City("NY","USA"));

Required Output:

List<"Some thing"> combined = new ArrayList();
combined is like this :
USA
SF
NY
India
Mumbai 
Agra 

I used this approach :

public class ListBreak {
public static void main(String[] args) {

    List<City> cities = new ArrayList<>();
    cities.add(new City("SF","USA"));
    cities.add(new City("Agra","India"));
    cities.add(new City("Mumbai","India"));
    cities.add(new City("NY","USA"));

    List<ABC> abcs = new ArrayList<>();
    HashMap<Country,List<City>> countryListHashMap= new HashMap<>();
    // way one
    for (City city:cities) {
         Country coutry= new Country(city.countryName);
        if(!countryListHashMap.containsKey(coutry))
        {
            countryListHashMap.put(coutry,new ArrayList<>());
        }
        List<City> list = countryListHashMap.get(coutry);
        list.add(city);
        countryListHashMap.put(coutry,list);
    }
    for (Map.Entry<Country,List<City>> entrySet:countryListHashMap.entrySet()) {
          abcs.add(entrySet.getKey());
          abcs.addAll(entrySet.getValue());
    }
    System.out.println(abcs);
}
static class City implements ABC{
    String cityName;
    String countryName;

    public City(String cityName, String countryName) {
        this.cityName = cityName;
        this.countryName = countryName;
    }
}
static class Country implements ABC{
    String countryName;

    public Country(String countryName) {
        this.countryName = countryName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Country)) return false;
        Country country = (Country) o;
        return countryName.equals(country.countryName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(countryName);
    }
}

}

Any better, cleaner, faster way

package clean.code;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class ListBreak {

    public static class City {

        private String cityName;
        private String countryName;

        public City(String cityName, String countryName) {
            this.setCityName(cityName);
            this.setCountryName(countryName);
        }

        public String getCityName() {
            return cityName;
        }

        public void setCityName(String cityName) {
            this.cityName = cityName;
        }

        public String getCountryName() {
            return countryName;
        }

        public void setCountryName(String countryName) {
            this.countryName = countryName;
        }

    }

    public static void main(String[] args) {

        List<City> cities = new ArrayList<>();

        cities.add(new City("SF", "USA"));
        cities.add(new City("Agra", "India"));
        cities.add(new City("Mumbai", "India"));
        cities.add(new City("NY", "USA"));

        cities.stream()
                .collect(Collectors.groupingBy(City::getCountryName, collectCityNames()))
                .forEach((countryName, cityNames) -> printCityNamesByCountryName(countryName, cityNames));
    }

    private static Collector<City, ?, List<String>> collectCityNames() {
        return Collectors.mapping(City::getCityName, Collectors.toList());
    }

    private static void printCityNamesByCountryName(String countryName, List<String> cityNames) {
        System.out.println(countryName);
        cityNames.forEach(System.out::println);
    }
}

Python, Using Slicing. We can slice the list at certain elements to create the columnar structure. Here we convert the given list into a new list where the� After you create the drop-down list, editing the entries is as easy as changing the cell data. In this example, the data is pulled from a cell that's located in a different worksheet. The instructions in this article apply to Excel 2019, 2016, 2013, and 2010 and Excel for Mac 2019, 2016, and 2011.

class City {
    String cityName;
    String countryName;

//Getter,Setter and Constructors
}

public class CandidateCode {
    public static void main(String[] args) {
        List<City> cities = new ArrayList<>();
        //stuffs
        final Map<String, List<String>> collect1 = cities.stream()
                .collect(Collectors.groupingBy(City::getCountryName, Collectors.mapping(City::getCityName, Collectors.toList())));
        for (Map.Entry str : collect1.entrySet()) {
            System.out.println(str.getKey());
            for (String s : collect1.get(str.getKey())) {
                System.out.println(s);
            }

        }

    }
}

Output:

USA
SF
NY
India
Agra
Mumbai

Python, Python | Convert column to separate elements in list of lists extracting a column from a list and then it can be added as new element using list� SharePoint custom forms: Build separate forms to create, show or edit the list item. Ankit Saraf , Senior Program Manager, PowerApps , Thursday, November 16, 2017 We announced the availability of Custom forms for SharePoint yesterday and today I wanted to show a simple example on how you can create separate forms to create, show or edit an item.

    class City{

        String cityName;
        String countryName;
        public String getCityName() {
            return cityName;
        }
        public void setCityName(String cityName) {
            this.cityName = cityName;
        }
        public String getCountryName() {
            return countryName;
        }
        public void setCountryName(String countryName) {
            this.countryName = countryName;
        }

        public City(String string, String string2) {
            this.cityName = string;
            this.countryName=string2;

        }

        //getter setter ns constructor stuffs

     }
    public class Test  {
        public static void main(String[] args) {
            List<City> cities = new ArrayList<>();
            cities.add(new City("SF","USA"));
            cities.add(new City("Agra","India"));
            cities.add(new City("Mumbai","India"));
            cities.add(new City("NY","USA"));
            List<String> result = new ArrayList<>();

            Map<String, List<City>> res = cities.stream().collect(Collectors.groupingBy(City::getCountryName));
            for(HashMap.Entry<String, List<City>> valuepair:res.entrySet())
            {
                List<City> value = valuepair.getValue();
                City countryName = value.get(0);
                String cname = countryName.getCountryName();
                result.add(cname);
                for (City count : value) {
                    result.add(count.getCityName());

                }

            }
            for (String string : result) {
                System.out.println(string);
            }





    }

}

Output

USA
SF
NY
India
Agra
Mumbai

Split Delimited List into Rows, Do the transformations. The Custom column for each order contains one or more attribute:value pairs, separated by a colon. Each pair is� So the result is List<Plus5> newList and you want to create a new Plus5 for every int in initialList: List<Plus5> newList = initialList.Select(i => new Plus5(i)).ToList(); If you want to micro-optimize(save memory): List<Plus5> newList = new List<Plus5>(initialList.Count); newList.AddRange(initialList.Select(i => new Plus5(i)));

Please try this.

Map<String, List<String>> countryListHashMap =
                cities.stream().collect(Collectors.groupingBy(City::getCountryName, Collectors.mapping(City::getCityName, Collectors.toList())));
countryListHashMap.forEach((k,v)-> {System.out.println(k);
            v.forEach(System.out::println);});

Output:

USA
SF
NY
India
Agra
Mumbai

Split a Field into Multiple Fields, Note: In some cases, if the split takes too long to generate new fields or The following is a list of additional notes you might need to know about splits and� Reverse concatenate and convert one cell (a comma serrated list) to a row/column list in Excel. Convert column list to comma separated list with CONCATENATE function. In Excel, CONCATENATE function can convert the column list to a list in a cell separated by commas. Please do as follow:

CSS Swag: Multi-Column Lists – A List Apart, It can also divide the total number of list items by the desired number of columns and To wrap a single list into columns, I use CSS to grab each item that begins a new To create multiple-column lists it isn't necessary to position every item,� Note: Once you select a field as a rule, you can’t select it again to add a new rule in the same list. What you can do is create a new, separate list. If all you need is that one rule to accurately select the contacts you need, you can move on to the Target Recipients instructions. To add more rules: 1.

split a list into two python Code Example, Get code examples like "split a list into two python" instantly right from your return 'The first child list cannot be bigger than its parent list' create a new column in pandas � Create a new RDD of int containing elements from� Give the list a range name. Now you're ready to create the validation list, as follows: Select E2:E15--these are the existing cells you want to fill using the validation control.

Solved: Splitting multi-element data structure list into s, How can I split the list into a table of index, description? I've tried using Split Column Create new Source >> Blank Query. 2. Go to Advanced Now you should have an extra column in your table that can be expanded by: Clicking inside the� The cell where the new list of unique values will be output to. Should be a blank column. Click the “Unique records only” checkbox. Click the OK button. The Advanced Filter will paste the values of the unique items starting in the cell specified in the “Copy to” range. Other Ways to Create a List of Unique Values?

Comments
  • its there already
  • Even if someone uses a comparator to sort your as per your India , Mumbai and Agra then cities of USA will fail the order.
  • If your doubt is not yet cleared then let us know.
  • it should be country name city name (above country) city name (above country) Country name city name (above country) city name (above country)
  • It can definitely be, earlier it was not mentioned in question, anyways I will try it.
  • That does 80% of the things, now you need to print in proper manner.
  • This should work. countryListHashMap.forEach((k,v) -> {System.out.println(k);v.forEach(System.out::println);});